Skip to content

allow retries #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ The client constructor supports several options you may set to achieve the expec
<td><code>True</code></td>
<td>Enables gzip response content encoding.</td>
</tr>
<tr>
<td><code>max_retries</code></td>
<td><code>1</code></td>
<td>
Maximum number of retries after a request failure.
</td>
</tr>
<tr>
<td><code>max_rate_limit_retries</code></td>
<td><code>1</code></td>
Expand Down
27 changes: 18 additions & 9 deletions contentful/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import requests
import platform
from requests.adapters import HTTPAdapter
from urllib3.exceptions import ConnectTimeoutError
from re import sub
from .utils import ConfigurationException
from .utils import retry_request, string_class
from .errors import get_error, RateLimitExceededError, EntryNotFoundError
from .errors import get_error, RateLimitExceededError, EntryNotFoundError, ServerError
from .resource_builder import ResourceBuilder
from .content_type_cache import ContentTypeCache

Expand Down Expand Up @@ -58,6 +60,7 @@ class Client(object):
:param proxy_port: (optional) Port for Proxy, defaults to None.
:param proxy_username: (optional) Username for Proxy, defaults to None.
:param proxy_password: (optional) Password for Proxy, defaults to None.
:param max_retries: (optional) Maximum number of retries after timeout, defaults to 3.
:param max_rate_limit_retries: (optional) Maximum amount of retries
after RateLimitError, defaults to 1.
:param max_rate_limit_wait: (optional) Timeout (in seconds) for waiting
Expand Down Expand Up @@ -100,6 +103,7 @@ def __init__(
proxy_port=None,
proxy_username=None,
proxy_password=None,
max_retries=3,
max_rate_limit_retries=1,
max_rate_limit_wait=60,
max_include_resolution_depth=20,
Expand All @@ -125,6 +129,7 @@ def __init__(
self.proxy_port = proxy_port
self.proxy_username = proxy_username
self.proxy_password = proxy_password
self.max_retries = max_retries
self.max_rate_limit_retries = max_rate_limit_retries
self.max_rate_limit_wait = max_rate_limit_wait
self.max_include_resolution_depth = max_include_resolution_depth
Expand Down Expand Up @@ -540,14 +545,18 @@ def _http_get(self, url, query):
if self._has_proxy():
kwargs['proxies'] = self._proxy_parameters()

response = requests.get(
self._url(url),
**kwargs
)

if response.status_code == 429:
raise RateLimitExceededError(response)

with requests.Session() as s:
s.mount(self._url(url), HTTPAdapter(max_retries=self.max_retries))
try:
response = s.get(
self._url(url),
**kwargs
)
except ConnectTimeoutError:
response['status_code'] = 500

if response.status_code == 429:
raise RateLimitExceededError(response)
return response

def _get(self, url, query=None):
Expand Down
4 changes: 2 additions & 2 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from unittest import TestCase

from requests_mock import ANY

from urllib3.exceptions import MaxRetryError
from contentful.client import Client
from contentful.content_type_cache import ContentTypeCache
from contentful.errors import EntryNotFoundError
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_client_sync_with_environments(self):
client = Client('a22o2qgm356c', 'bfbc63cf745a037125dbcc64f716a9a0e9d091df1a79e84920b890f87a6e7ab9', environment='staging', content_type_cache=False)
sync = client.sync({'initial': True})

self.assertEquals(sync.items[0].environment.id, 'staging')
self.assertEqual(sync.items[0].environment.id, 'staging')

@vcr.use_cassette('fixtures/client/array_endpoints.yaml')
def test_client_creates_wrapped_arrays(self):
Expand Down