Code cleanup

This commit is contained in:
Ruben van Staveren 2024-05-07 17:12:23 +02:00
parent 878a2b6bbf
commit 4eb906f6d4
Signed by: ruben
GPG Key ID: 886F6BECD477A93F

View File

@ -15,12 +15,14 @@ app = FastAPI()
GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb' GEOLITE2_ASN_DB = '/usr/local/share/GeoIP/GeoLite2-ASN.mmdb'
GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb' GEOLITE2_CITY_DB = '/usr/local/share/GeoIP/GeoLite2-City.mmdb'
class IPAddressParam(BaseModel): class IPAddressParam(BaseModel):
''' '''
Payload entry as used in POST Payload entry as used in POST
''' '''
ip: Union[IPv6Address, IPv4Address] ip: Union[IPv6Address, IPv4Address]
class Locality(BaseModel): class Locality(BaseModel):
''' '''
Locality data Locality data
@ -30,6 +32,7 @@ class Locality(BaseModel):
continent: Optional[str] = None continent: Optional[str] = None
is_eu: bool = False is_eu: bool = False
class GeoLocation(BaseModel): class GeoLocation(BaseModel):
''' '''
Geolocation data model Geolocation data model
@ -40,8 +43,10 @@ class GeoLocation(BaseModel):
network: Optional[Union[IPv6Network, IPv4Network]] = None network: Optional[Union[IPv6Network, IPv4Network]] = None
locality: Locality = Locality() locality: Locality = Locality()
@app.post("/") @app.post("/")
async def root_post(ipaddresses: Annotated[list[IPAddressParam], async def root_post(ipaddresses: Annotated[
list[IPAddressParam],
Body(title="The IPAddresses to geolocate")], Body(title="The IPAddresses to geolocate")],
response: Response response: Response
) -> list[GeoLocation]: ) -> list[GeoLocation]:
@ -79,7 +84,8 @@ async def root_post(ipaddresses: Annotated[list[IPAddressParam],
@app.get("/{ipaddress}") @app.get("/{ipaddress}")
async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address], async def root_get(ipaddress: Annotated[
Union[IPv4Address, IPv6Address],
Path(title="The IPAddress to geolocate")], Path(title="The IPAddress to geolocate")],
response: Response response: Response
) -> GeoLocation: ) -> GeoLocation:
@ -93,6 +99,7 @@ async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address],
response.status_code = status.HTTP_404_NOT_FOUND response.status_code = status.HTTP_404_NOT_FOUND
return GeoLocation() return GeoLocation()
@app.get("/") @app.get("/")
def root_redirect(req: Request) -> RedirectResponse: def root_redirect(req: Request) -> RedirectResponse:
''' '''