From 209696fe7144b72c5b1e7a05624b29393d7cb5aa Mon Sep 17 00:00:00 2001 From: jarredSafegraph <91471796+jarredSafegraph@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:29:30 -0400 Subject: [PATCH] Added explicit on_exception for RequestException --- placekey/api.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/placekey/api.py b/placekey/api.py index 173aea1..fc69b92 100644 --- a/placekey/api.py +++ b/placekey/api.py @@ -3,6 +3,7 @@ import logging from json import JSONDecodeError +import backoff import requests from backoff import on_exception, fibo from ratelimit import limits, RateLimitException @@ -18,6 +19,12 @@ log.handlers = [console_log] +class GatewayTimeout(Exception): + def __init__(self, message, period_remaining): + super(GatewayTimeout, self).__init__(message) + self.period_remaining = period_remaining + + class PlacekeyAPI: """ PlacekeyAPI class @@ -286,16 +293,23 @@ def _get_request_function(self, url, calls, period, max_tries): :param max_tries: the maximum number of retries before giving up """ - @on_exception(fibo, RateLimitException, max_tries=max_tries) + @backoff.on_exception(backoff.fibo, (RateLimitException, requests.exceptions.RequestException), + max_tries=max_tries) @limits(calls=calls, period=period) def make_request(data): - response = requests.post( - url, headers=self.headers, - data=json.dumps(data).encode('utf-8') - ) + try: + response = requests.post( + url, headers=self.headers, + data=json.dumps(data).encode('utf-8') + ) + + if response.status_code == 429: + raise RateLimitException("Rate limit exceeded", 0) + elif response.status_code == 504: + raise requests.exceptions.RequestException("Gateway Timeout") - if response.status_code == 429 or response.status_code == 504: - raise RateLimitException("Rate limit exceeded", 0) - return response + return response + except requests.exceptions.RequestException as e: + raise e return make_request