Catch HTTP timeout and return cached data

Closes: #65
This commit is contained in:
Edward Betts 2023-11-10 11:59:53 +01:00
parent f60aab364b
commit 25bb52d88d
2 changed files with 12 additions and 7 deletions

View file

@ -1,3 +1,5 @@
"""Get details of upcoming space launches."""
import json
import os
import typing
@ -118,8 +120,8 @@ async def get_launches(rocket_dir: str, limit: int = 200) -> list[Summary]:
if not existing or (now - existing[0][0]).seconds > 3600: # one hour
try:
return await next_launch_api(rocket_dir, limit=limit)
except ValueError:
print("*** SpaceX next launch error ***")
except httpx.ReadTimeout:
pass
f = existing[0][1]

View file

@ -118,16 +118,19 @@ async def get_bristol_data(data_dir: str, uprn: str) -> BristolSchedule:
recent = datetime.strptime(recent_filename, f"%Y-%m-%d_%H:%M_{uprn}.json")
delta = now - recent
if existing and delta < timedelta(hours=ttl_hours):
def get_from_recent() -> BristolSchedule:
json_data = json.load(open(os.path.join(waste_dir, recent_filename)))
return typing.cast(BristolSchedule, json_data["data"])
now_str = now.strftime("%Y-%m-%d_%H:%M")
filename = f"{waste_dir}/{now_str}_{uprn}.json"
if existing and delta < timedelta(hours=ttl_hours):
return get_from_recent()
r = await get_bristol_gov_uk_data(uprn)
try:
r = await get_bristol_gov_uk_data(uprn)
except httpx.ReadTimeout:
return get_from_recent()
with open(filename, "wb") as out:
with open(f'{waste_dir}/{now.strftime("%Y-%m-%d_%H:%M")}_{uprn}.json', "wb") as out:
out.write(r.content)
return typing.cast(BristolSchedule, r.json()["data"])