11import datetime
22import logging
3+ from urllib .parse import urlsplit , urlunsplit
34
45import requests
56from django .conf import settings
@@ -20,30 +21,38 @@ def parse_date(date):
2021 return datetime .datetime .strptime (date .split ("T" )[0 ], RELIEFWEB_DATETIME_FORMAT )
2122
2223
24+ def _fallback_reliefweb_url (url ):
25+ parts = urlsplit (url )
26+ if parts .path .endswith ("/" ):
27+ return urlunsplit ((parts .scheme , parts .netloc , parts .path .rstrip ("/" ), parts .query , parts .fragment ))
28+ return url
29+
30+
31+ def _handle_reliefweb_gone (query_params , url , context , allow_fallback ):
32+ fallback_url = _fallback_reliefweb_url (url )
33+ if allow_fallback and fallback_url != url :
34+ logger .warning (
35+ "ReliefWeb API returned 410 Gone for %s. Retrying with fallback URL: %s" ,
36+ context ,
37+ fallback_url ,
38+ )
39+ return _post_reliefweb (query_params , fallback_url , context , allow_fallback = False )
40+ logger .warning (
41+ "ReliefWeb API returned 410 Gone for %s. Skipping %s prefetch. URL: %s" ,
42+ DISASTER_API ,
43+ context ,
44+ url ,
45+ )
46+ return None
47+
48+
2349def _post_reliefweb (query_params , url , context , * , allow_fallback = True ):
2450 try :
2551 response = requests .post (url , json = query_params )
52+ if response .status_code == 410 :
53+ return _handle_reliefweb_gone (query_params , url , context , allow_fallback )
2654 response .raise_for_status ()
2755 return response .json ()
28- except requests .HTTPError as exc :
29- status_code = exc .response .status_code if exc .response else None
30- 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 )
39- logger .warning (
40- "ReliefWeb API returned 410 Gone for %s. Skipping %s prefetch. URL: %s" ,
41- DISASTER_API ,
42- context ,
43- url ,
44- )
45- return None
46- raise
4756 except requests .RequestException :
4857 logger .exception ("ReliefWeb API request failed for %s. URL: %s" , context , url )
4958 raise
0 commit comments