Add Cache-Control headers

This commit is contained in:
Ruben van Staveren 2024-05-06 17:24:27 +02:00
parent 38416f103f
commit d090e90ee2
Signed by: ruben
GPG Key ID: 886F6BECD477A93F

View File

@ -42,7 +42,8 @@ class GeoLocation(BaseModel):
@app.post("/")
async def root_post(ipaddresses: Annotated[list[IPAddressParam],
Body(title="The IPAddresses to geolocate")]
Body(title="The IPAddresses to geolocate")],
response: Response
) -> list[GeoLocation]:
'''
Return GeoLocation item(s) for a list of IPAddressParam objects
@ -72,6 +73,8 @@ async def root_post(ipaddresses: Annotated[list[IPAddressParam],
ip=ipaddress.ip
)
)
if geolocations:
response.headers['Cache-Control'] = 'private, max-age=604800'
return geolocations
@ -85,6 +88,7 @@ async def root_get(ipaddress: Annotated[Union[IPv4Address,IPv6Address],
'''
locations = await root_post([IPAddressParam(ip=ipaddress)])
if locations:
response.headers['Cache-Control'] = 'private, max-age=604800'
return locations.pop()
response.status_code = status.HTTP_404_NOT_FOUND
return GeoLocation()