Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,24 @@ def ok_200(data: Any = None) -> JSONResponse:
status_code=status.HTTP_200_OK,
content={"data": data}
)

def not_implemented_501(msg: Optional[str] = None) -> HTTPException:
return HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail=HTTPStatus.NOT_IMPLEMENTED(1) if msg is None else msg,
)















15 changes: 14 additions & 1 deletion src/routes/geo/country.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends
import foss42.geo.country as co

from models.geo.country import CountryCodeModel
from models.responses import *

Expand All @@ -20,6 +21,8 @@ async def get_country_data(data: CountryCodeModel = Depends()):
try:
res = co.code_to_data(data.code)
return ok_200(res)
except ValueError as e:
raise not_found_404(str(e))
except Exception as e:
raise internal_error_500()

Expand All @@ -29,6 +32,8 @@ async def get_country_flag(data: CountryCodeModel = Depends()):
try:
res = co.code_to_flag(data.code)
return ok_200(res)
except ValueError as e:
raise not_found_404(str(e))
except Exception as e:
raise internal_error_500()

Expand All @@ -38,15 +43,18 @@ async def get_country_name(data: CountryCodeModel = Depends()):
try:
res = co.code_to_popular_name(data.code)
return ok_200(res)
except ValueError as e:
raise not_found_404(str(e))
except Exception as e:
raise internal_error_500()


@country_router.get("/officialname")
async def get_official_country_name(data: CountryCodeModel = Depends()):
try:
res = co.code_to_official_name(data.code)
return ok_200(res)
except ValueError as e:
raise not_found_404(str(e))
except Exception as e:
raise internal_error_500()

Expand All @@ -56,5 +64,10 @@ async def get_country_subdivisions(data: CountryCodeModel = Depends()):
try:
res = co.code_to_subdivision(data.code)
return ok_200(res)

except ValueError as e:
raise not_found_404(str(e))
except NotImplementedError as e:
raise not_implemented_501(str(e))
except Exception as e:
raise internal_error_500()