TOMTOM uses 403 instead of 429 for rate limiting

That has a great problem when we're dealing with legit 403 status for
example deactivated user, forbidden access, etc.

I've added a check for the HTTP header `X-Error-Detail-Header` in order
to distinguish between legit 403 and 429 error messages

Possible values for `X-Error-Detail-Header` in a 403 error:

  o Service Requires SSL : http is used instead of https (secure)

  o Invalid Referer : invalid 'Referer' header value is send
  to https://api.tomtom.com and allowed referer values are
  configured on specific API key

  o Account Over Queries Per Second Limit : rate limit exceeded

  o Account Inactive : incorrect API key/API key no longer valid
This commit is contained in:
Mario de Frutos 2018-10-24 17:38:23 +02:00
parent 17c993f6ef
commit 33f40bc945

View File

@ -6,6 +6,9 @@ from exceptions import TimeoutException
DEFAULT_RETRY_TIMEOUT = 60 DEFAULT_RETRY_TIMEOUT = 60
DEFAULT_QUERIES_PER_SECOND = 10 DEFAULT_QUERIES_PER_SECOND = 10
TOMTOM_403_RATE_LIMIT_HEADER = 'Account Over Queries Per Second Limit'
TOMTOM_DETAIL_HEADER = 'X-Error-Detail-Header'
def qps_retry(original_function=None, **options): def qps_retry(original_function=None, **options):
""" Query Per Second retry decorator """ Query Per Second retry decorator
@ -46,6 +49,8 @@ class QPSService:
response = getattr(e, 'response', None) response = getattr(e, 'response', None)
if response is not None: if response is not None:
if self._provider is not None and self._provider == 'tomtom' and (response.status_code == 403): if self._provider is not None and self._provider == 'tomtom' and (response.status_code == 403):
if response.headers.get(TOMTOM_DETAIL_HEADER) != TOMTOM_403_RATE_LIMIT_HEADER:
raise e
self.retry(start_time, attempt_number) self.retry(start_time, attempt_number)
elif response.status_code == 429: elif response.status_code == 429:
self.retry(start_time, attempt_number) self.retry(start_time, attempt_number)