-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform.py
37 lines (32 loc) · 1.01 KB
/
transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import requests
import os
def handler(data, log):
key = os.getenv("GEOAPI_KEY")
log.info("Event received: " + json.dumps(data))
enriched_location = enrich_location(key, data)
data.update(
{
"latitude": enriched_location["lat"],
"longitude": enriched_location["lon"],
"postcode": enriched_location["postcode"],
}
)
return data
def enrich_location(key, data):
response = requests.get(
f"https://api.geoapify.com/v1/geocode/search?text={data['address']}&apiKey={key}"
)
response_data = response.json()
if (
response_data
and "features" in response_data
and len(response_data["features"]) > 0
):
location_data = response_data["features"][0]["properties"]
return {
"lat": location_data.get("lat"),
"lon": location_data.get("lon"),
"postcode": location_data.get("postcode"),
}
return {"lat": None, "lon": None, "postcode": None}