Skip to content

Commit df0275f

Browse files
committed
Better error handling
1 parent dd479c7 commit df0275f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

databank/management/commands/ingest_acaps.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime as dt
33
import xmltodict
44
import pandas as pd
5+
import time
56
from dateutil.parser import parse
67

78
from django.core.management.base import BaseCommand
@@ -26,6 +27,7 @@ def handle(self, *args, **kwargs):
2627
"Authorization": "Token %s" % settings.ACAPS_API_TOKEN
2728
}
2829
)
30+
logger.info(f'Importing for country {name}')
2931
response_data = response.json()
3032
if 'results' in response_data and len(response_data['results']):
3133
df = pd.DataFrame.from_records(response_data["results"])
@@ -41,4 +43,5 @@ def handle(self, *args, **kwargs):
4143
'source': df_data[11],
4244
'source_date': df_data[12]
4345
}
44-
AcapsSeasonalCalender.objects.create(**dict_data)
46+
AcapsSeasonalCalender.objects.create(**dict_data)
47+
time.sleep(5)

databank/management/commands/ingest_worldbank.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,20 @@ def handle(self, *args, **kwargs):
4040
independent=True
4141
).exclude(iso3="COK"):
4242
country_iso3 = country.iso3
43+
logger.info(f'Importing country {country_iso3}')
4344
for indicator in world_bank_indicators:
4445
page = 1 # Reset the page for each indicator
4546
while True:
46-
response = requests.get(f'https://api.worldbank.org/v2/country/{country_iso3}/indicator/{indicator}?date={daterange}', params={
47+
try:
48+
response = requests.get(f'https://api.worldbank.org/v2/country/{country_iso3}/indicator/{indicator}?date={daterange}', params={
4749
'format': 'json',
4850
'source': 2,
4951
'per_page': 5000 - 1, # World Bank throws error on 5000
5052
'page': page,
51-
})
52-
response.raise_for_status()
53+
})
54+
except requests.exceptions.HTTPError as err:
55+
print(err.response.text)
56+
continue
5357
try:
5458
data_list = response.json()[1]
5559
if data_list is not None and len(data_list) > 0:

0 commit comments

Comments
 (0)