Skip to content

Commit

Permalink
Merge pull request #293 from openedx/alangsto/add_error_handling_for_…
Browse files Browse the repository at this point in the history
…rate_limiting

fix: add rate-limiting retry for enterprise requests
  • Loading branch information
alangsto committed Mar 16, 2022
2 parents 22d05de + d868789 commit 2570f9d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unreleased
----------

=========================
[4.2.1] - 2022-03-15
---------------------
* Added error handling for rate limit exceptions

[4.2.0] - 2022-03-15
---------------------
* Removed currently broken admin url inclusion from enterprise-data.
Expand Down
2 changes: 1 addition & 1 deletion enterprise_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
Enterprise data api application. This Django app exposes API endpoints used by enterprises.
"""

__version__ = "4.2.0"
__version__ = "4.2.1"

default_app_config = "enterprise_data.apps.EnterpriseDataAppConfig" # pylint: disable=invalid-name
13 changes: 12 additions & 1 deletion enterprise_reporting/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from datetime import datetime
from functools import wraps
from urllib.parse import parse_qs, urlparse
import time

from edx_rest_api_client.client import EdxRestApiClient
from edx_rest_api_client.exceptions import HttpClientError


class EdxOAuth2APIClient:
Expand Down Expand Up @@ -129,7 +131,16 @@ def traverse_pagination(response, endpoint):
next_page = response.get('next')
while next_page:
querystring = parse_qs(urlparse(next_page).query, True)
response = endpoint.get(**querystring)
try:
response = endpoint.get(**querystring)
except HttpClientError as exc:
# If we get HTTP429, we should pause for a period of time before re-requesting one more time.
# This status code is expected because of rate limiting on the endpoint
if exc.response.status_code == 429:
time.sleep(5)
response = endpoint.get(**querystring)
else:
raise exc
results += response.get('results', [])
next_page = response.get('next')

Expand Down

0 comments on commit 2570f9d

Please sign in to comment.