Catch and ignore space launch API errors
This commit is contained in:
parent
0c36591e2f
commit
d1898686e9
2 changed files with 11 additions and 3 deletions
|
|
@ -16,7 +16,7 @@ Summary = dict[str, typing.Any]
|
|||
ttl = 60 * 60 * 2 # two hours
|
||||
|
||||
|
||||
def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict:
|
||||
def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict | None:
|
||||
"""Get the next upcoming launches from the API."""
|
||||
now = datetime.now()
|
||||
filename = os.path.join(rocket_dir, now.strftime("%Y-%m-%d_%H:%M:%S.json"))
|
||||
|
|
@ -24,14 +24,19 @@ def next_launch_api_data(rocket_dir: str, limit: int = 200) -> StrDict:
|
|||
|
||||
params: dict[str, str | int] = {"limit": limit}
|
||||
r = requests.get(url, params=params)
|
||||
data: StrDict = r.json()
|
||||
try:
|
||||
data: StrDict = r.json()
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
return None
|
||||
open(filename, "w").write(r.text)
|
||||
return data
|
||||
|
||||
|
||||
def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch]:
|
||||
def next_launch_api(rocket_dir: str, limit: int = 200) -> list[Launch] | None:
|
||||
"""Get the next upcoming launches from the API."""
|
||||
data = next_launch_api_data(rocket_dir, limit)
|
||||
if not data:
|
||||
return None
|
||||
return [summarize_launch(launch) for launch in data["results"]]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue