Skip to content

Commit 74e8000

Browse files
committed
Fix ReliefWeb 410 handling and use JSON request body
1 parent 7516b59 commit 74e8000

1 file changed

Lines changed: 38 additions & 35 deletions

File tree

databank/management/commands/sources/RELIEFWEB.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import datetime
2-
import json
32
import logging
43

54
import requests
@@ -11,7 +10,7 @@
1110

1211
logger = logging.getLogger(__name__)
1312

14-
DISASTER_API = f"https://api.reliefweb.int/v1/disasters/?appname={settings.RELIEF_WEB_APP_NAME}"
13+
DISASTER_API = f"https://api.reliefweb.int/v1/disasters?appname={settings.RELIEF_WEB_APP_NAME}"
1514
RELIEFWEB_DATETIME_FORMAT = "%Y-%m-%d"
1615

1716

@@ -21,14 +20,22 @@ def parse_date(date):
2120
return datetime.datetime.strptime(date.split("T")[0], RELIEFWEB_DATETIME_FORMAT)
2221

2322

24-
def _post_reliefweb(query_params, url, context):
23+
def _post_reliefweb(query_params, url, context, *, allow_fallback=True):
2524
try:
26-
response = requests.post(url, data=query_params)
25+
response = requests.post(url, json=query_params)
2726
response.raise_for_status()
2827
return response.json()
2928
except requests.HTTPError as exc:
3029
status_code = exc.response.status_code if exc.response else None
3130
if status_code == 410:
31+
fallback_url = url.replace("/disasters/?", "/disasters?")
32+
if allow_fallback and fallback_url != url:
33+
logger.warning(
34+
"ReliefWeb API returned 410 Gone for %s. Retrying with fallback URL: %s",
35+
context,
36+
fallback_url,
37+
)
38+
return _post_reliefweb(query_params, fallback_url, context, allow_fallback=False)
3239
logger.warning(
3340
"ReliefWeb API returned 410 Gone for %s. Skipping %s prefetch. URL: %s",
3441
DISASTER_API,
@@ -46,22 +53,20 @@ def _post_reliefweb(query_params, url, context):
4653

4754

4855
def _crises_event_prefetch():
49-
query_params = json.dumps(
50-
{
51-
"limit": 1000,
52-
"filter": {
53-
"operator": "AND",
54-
"conditions": [
55-
{
56-
"field": "primary_type.code",
57-
"value": [type_code for type_code, _ in PastCrisesEvent.CHOICES],
58-
"operator": "OR",
59-
}
60-
],
61-
},
62-
"fields": {"include": ["date.created", "primary_country.iso3", "primary_type.code"]},
63-
}
64-
)
56+
query_params = {
57+
"limit": 1000,
58+
"filter": {
59+
"operator": "AND",
60+
"conditions": [
61+
{
62+
"field": "primary_type.code",
63+
"value": [type_code for type_code, _ in PastCrisesEvent.CHOICES],
64+
"operator": "OR",
65+
}
66+
],
67+
},
68+
"fields": {"include": ["date.created", "primary_country.iso3", "primary_type.code"]},
69+
}
6570

6671
url = DISASTER_API
6772
data = {}
@@ -95,21 +100,19 @@ def _crises_event_prefetch():
95100

96101

97102
def _epidemics_prefetch():
98-
query_params = json.dumps(
99-
{
100-
"limit": 1000,
101-
"filter": {
102-
"operator": "AND",
103-
"conditions": [
104-
{
105-
"field": "primary_type.code",
106-
"value": ["EP"],
107-
},
108-
],
109-
},
110-
"fields": {"include": ["name", "date.created", "primary_country.iso3"]},
111-
}
112-
)
103+
query_params = {
104+
"limit": 1000,
105+
"filter": {
106+
"operator": "AND",
107+
"conditions": [
108+
{
109+
"field": "primary_type.code",
110+
"value": ["EP"],
111+
},
112+
],
113+
},
114+
"fields": {"include": ["name", "date.created", "primary_country.iso3"]},
115+
}
113116

114117
url = DISASTER_API
115118
data = {}

0 commit comments

Comments
 (0)