From d79dc2107e1a566b8a7db0feeca4bd471e6e57f6 Mon Sep 17 00:00:00 2001 From: Tamara Janina Fingerlin <90063506+TJaniF@users.noreply.github.com> Date: Thu, 20 Jun 2024 19:44:40 +0200 Subject: [PATCH] Failsafe API in example DAG (#1667) * failsafe api in example DAG * correct docs link * correct docs link * Update airflow/include/exampledag.py Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> --------- Co-authored-by: Jed Cunningham <66968678+jedcunningham@users.noreply.github.com> --- airflow/include/exampledag.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/airflow/include/exampledag.py b/airflow/include/exampledag.py index 6e31cb033..8b08b7b40 100644 --- a/airflow/include/exampledag.py +++ b/airflow/include/exampledag.py @@ -48,9 +48,28 @@ def get_astronauts(**context) -> list[dict]: so they can be used in a downstream pipeline. The task returns a list of Astronauts to be used in the next task. """ - r = requests.get("http://api.open-notify.org/astros.json") - number_of_people_in_space = r.json()["number"] - list_of_people_in_space = r.json()["people"] + try: + r = requests.get("http://api.open-notify.org/astros.json") + r.raise_for_status() + number_of_people_in_space = r.json()["number"] + list_of_people_in_space = r.json()["people"] + except: + print("API currently not available, using hardcoded data instead.") + number_of_people_in_space = 12 + list_of_people_in_space = [ + {"craft": "ISS", "name": "Oleg Kononenko"}, + {"craft": "ISS", "name": "Nikolai Chub"}, + {"craft": "ISS", "name": "Tracy Caldwell Dyson"}, + {"craft": "ISS", "name": "Matthew Dominick"}, + {"craft": "ISS", "name": "Michael Barratt"}, + {"craft": "ISS", "name": "Jeanette Epps"}, + {"craft": "ISS", "name": "Alexander Grebenkin"}, + {"craft": "ISS", "name": "Butch Wilmore"}, + {"craft": "ISS", "name": "Sunita Williams"}, + {"craft": "Tiangong", "name": "Li Guangsu"}, + {"craft": "Tiangong", "name": "Li Cong"}, + {"craft": "Tiangong", "name": "Ye Guangfu"}, + ] context["ti"].xcom_push( key="number_of_people_in_space", value=number_of_people_in_space