Skip to content

Commit

Permalink
fix/sentry issue/handle-os-json-non-2xx-codes (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
feba-rajan authored Dec 16, 2024
1 parent 21ced2b commit e3656bd
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions application/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,38 @@ def get_os_oauth2_token():
logger.error("OS_CLIENT_KEY or OS_CLIENT_SECRET not set")
return "null"

result = requests.post(
"https://api.os.uk/oauth2/token/v1",
data={"grant_type": "client_credentials"},
headers={},
auth=(settings.OS_CLIENT_KEY, settings.OS_CLIENT_SECRET),
)
jsonResult = result.json()
if "Error" in jsonResult:
logger.error(jsonResult["Error"])
return "null"
else:
return jsonResult
max_retries = 3
for attempt in range(1, max_retries + 1):
try:
result = requests.post(
"https://api.os.uk/oauth2/token/v1",
data={"grant_type": "client_credentials"},
headers={},
auth=(settings.OS_CLIENT_KEY, settings.OS_CLIENT_SECRET),
)
jsonResult = result.json()
if result.status_code == 200:
return jsonResult

if 400 <= result.status_code < 600:
logger.warning(
f"Server error {result.status_code}. Retrying... ({attempt}/{max_retries})"
)
if attempt == max_retries:
raise ConnectionError(
f"Failed after {max_retries} attempts. Status: {result.status_code}, Error: {result.text}"
)
continue
raise Exception(
f"Unexpected response: {result.status_code}, Error: {result.text}"
)

except Exception as e:
logger.error(f"Request failed on attempt {attempt}: {e}")
if attempt == max_retries:
raise ConnectionError(f"Network error after {max_retries} retries: {e}")

return "null"


def format_date(date_str):
Expand Down

0 comments on commit e3656bd

Please sign in to comment.