Skip to content

Commit

Permalink
Added explicit on_exception for RequestException
Browse files Browse the repository at this point in the history
  • Loading branch information
jarredSafegraph committed Jun 18, 2024
1 parent 9ae15d6 commit 209696f
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions placekey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 209696f

Please sign in to comment.