-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (43 loc) · 1.49 KB
/
main.py
File metadata and controls
61 lines (43 loc) · 1.49 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import requests
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from exceptions import *
from utils import get_latlng_by_digipin
app = FastAPI()
@app.exception_handler(AppException)
async def app_exception_handler(request: Request, exc: AppException):
return JSONResponse(
status_code=exc.status_code,
media_type="application/json",
content={"success": False, "response": exc.message},
)
@app.get("/reverse/")
def missing_digipin():
raise InvalidDigiPinException
@app.get("/reverse/{digipin}")
def root(digipin: str) -> JSONResponse:
if digipin is None:
raise InvalidDigiPinException
langitude, latitude = get_latlng_by_digipin(digipin)
session = requests.Session()
url = f"{os.environ.get('NOMINATIM_BASE_URL')}/reverse"
headers = {"User-Agent": os.environ.get("USER_AGENT")}
params = {"lat": latitude, "lon": langitude, "zoom": 18, "format": "jsonv2"}
try:
data = session.get(url, params=params, headers=headers, timeout=2)
data.raise_for_status()
response = data.json()
except requests.exceptions.RequestException as err:
raise NominatimException()
except Exception as e:
raise AppException(f"Unexpected error: {str(e)}")
response["digipin"] = digipin
return JSONResponse(
status_code=200,
media_type="application/json",
content={
"success": True,
"response": response,
},
)