Skip to content

Commit 80a997b

Browse files
authored
Create __init__.py
Initial Commit
1 parent 47c3b69 commit 80a997b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

geocodefarm/__init__.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import urllib.parse
2+
import urllib.request
3+
import json
4+
5+
6+
class GeocodeFarmClient:
7+
def __init__(self, api_key: str):
8+
self.api_key = api_key
9+
self.user_agent = "GeocodeFarmSDK-Py/4.0"
10+
11+
def forward(self, address: str) -> dict:
12+
url = "https://api.geocode.farm/forward/"
13+
params = {"key": self.api_key, "addr": address}
14+
response = self._make_request(url, params)
15+
return self._handle_response(response, "forward")
16+
17+
def reverse(self, lat: float, lon: float) -> dict:
18+
url = "https://api.geocode.farm/reverse/"
19+
params = {"key": self.api_key, "lat": lat, "lon": lon}
20+
response = self._make_request(url, params)
21+
return self._handle_response(response, "reverse")
22+
23+
def _make_request(self, url: str, params: dict) -> dict:
24+
full_url = f"{url}?{urllib.parse.urlencode(params)}"
25+
req = urllib.request.Request(full_url, headers={"User-Agent": self.user_agent})
26+
27+
try:
28+
with urllib.request.urlopen(req, timeout=10) as response:
29+
http_status = response.getcode()
30+
data = json.loads(response.read().decode())
31+
return {"http_status": http_status, "data": data}
32+
except Exception as e:
33+
return {"http_status": 0, "error": str(e)}
34+
35+
def _handle_response(self, response: dict, query_type: str) -> dict:
36+
if "data" not in response or not isinstance(response["data"], dict):
37+
return {
38+
"success": False,
39+
"status_code": response.get("http_status", 0),
40+
"error": response.get("error", "Invalid response from server")
41+
}
42+
43+
data = response["data"]
44+
status = data.get("STATUS", {}).get("status", "FAILED")
45+
if status != "SUCCESS":
46+
return {
47+
"success": False,
48+
"status_code": response["http_status"],
49+
"error": f"API returned failure: {status}"
50+
}
51+
52+
result = {}
53+
if query_type == "reverse":
54+
result_data = data.get("RESULTS", {}).get("result", [{}])[0]
55+
result = {
56+
"house_number": result_data.get("house_number"),
57+
"street_name": result_data.get("street_name"),
58+
"locality": result_data.get("locality"),
59+
"admin_2": result_data.get("admin_2"),
60+
"admin_1": result_data.get("admin_1"),
61+
"country": result_data.get("country"),
62+
"postal_code": result_data.get("postal_code"),
63+
"formatted_address": result_data.get("formatted_address"),
64+
"latitude": result_data.get("latitude"),
65+
"longitude": result_data.get("longitude"),
66+
"accuracy": data.get("RESULTS", {}).get("result", {}).get("accuracy"),
67+
}
68+
else:
69+
result_data = data.get("RESULTS", {}).get("result", {})
70+
coords = result_data.get("coordinates", {})
71+
address = result_data.get("address", {})
72+
result = {
73+
"house_number": address.get("house_number"),
74+
"street_name": address.get("street_name"),
75+
"locality": address.get("locality"),
76+
"admin_2": address.get("admin_2"),
77+
"admin_1": address.get("admin_1"),
78+
"country": address.get("country"),
79+
"postal_code": address.get("postal_code"),
80+
"formatted_address": address.get("full_address"),
81+
"latitude": coords.get("lat"),
82+
"longitude": coords.get("lon"),
83+
"accuracy": result_data.get("accuracy"),
84+
}
85+
86+
return {
87+
"success": True,
88+
"status_code": response["http_status"],
89+
"lat": result["latitude"],
90+
"lon": result["longitude"],
91+
"accuracy": result["accuracy"],
92+
"full_address": result["formatted_address"],
93+
"result": result
94+
}

0 commit comments

Comments
 (0)