Skip to content

Commit

Permalink
Add timeouts to requests calls
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
Sam Park committed Apr 2, 2019
1 parent 73fe01d commit 10a90c7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion contentful/client.py
Expand Up @@ -52,6 +52,8 @@ class Client(object):
:param reuse_entries: (optional) Boolean determining wether to reuse
hydrated Entry and Asset objects within the same request when possible.
Defaults to False
:param timeout_s: (optional) Max time allowed for each API call, in seconds.
Defaults to 1s.
:param proxy_host: (optional) URL for Proxy, defaults to None.
:param proxy_port: (optional) Port for Proxy, defaults to None.
:param proxy_username: (optional) Username for Proxy, defaults to None.
Expand Down Expand Up @@ -93,6 +95,7 @@ def __init__(
raise_errors=True,
content_type_cache=True,
reuse_entries=False,
timeout_s=1,
proxy_host=None,
proxy_port=None,
proxy_username=None,
Expand All @@ -117,6 +120,7 @@ def __init__(
self.raise_errors = raise_errors
self.content_type_cache = content_type_cache
self.reuse_entries = reuse_entries
self.timeout_s = timeout_s
self.proxy_host = proxy_host
self.proxy_port = proxy_port
self.proxy_username = proxy_username
Expand Down Expand Up @@ -529,7 +533,8 @@ def _http_get(self, url, query):

kwargs = {
'params': query,
'headers': self._request_headers()
'headers': self._request_headers(),
'timeout': self.timeout_s
}

if self._has_proxy():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -6,6 +6,7 @@ coverage==4.3.4
flake8==3.3.0
tox==2.5.0
virtualenv==15.1.0
requests-mock==1.5.2

Sphinx==1.6.3
sphinxcontrib-websupport==1.0.1
21 changes: 21 additions & 0 deletions tests/client_test.py
@@ -1,11 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import requests_mock
import vcr
import re
from unittest import TestCase

from requests_mock import ANY

from contentful.client import Client
from contentful.content_type_cache import ContentTypeCache
from contentful.errors import EntryNotFoundError
from contentful.errors import HTTPError
from contentful.utils import ConfigurationException
from contentful.entry import Entry

Expand All @@ -32,6 +38,21 @@ def test_client_validations(self):
with self.assertRaises(ConfigurationException):
Client('foo', 'bar', api_version=None)

def test_uses_timeouts(self):
c = Client('cfexampleapi', 'b4c0n73n7fu1')
with requests_mock.mock() as m:
m.register_uri('GET', ANY, status_code=500)
self.assertRaises(HTTPError, c.entries)
self.assertEqual(m.call_count, 1)
self.assertEqual(m.request_history[0].timeout, 1)

c = Client('cfexampleapi', 'b4c0n73n7fu1', timeout_s=0.1231570235)
with requests_mock.mock() as m:
m.register_uri('GET', ANY, status_code=500)
self.assertRaises(HTTPError, c.entries)
self.assertEqual(m.call_count, 1)
self.assertEqual(m.request_history[0].timeout, c.timeout_s)

@vcr.use_cassette('fixtures/client/content_type_cache.yaml')
def test_client_creates_a_content_type_cache(self):
Client('cfexampleapi', 'b4c0n73n7fu1')
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -16,6 +16,7 @@ deps =
PyYAML
python-dateutil
vcrpy
requests-mock
requests1: requests==1.2.3
requests27: requests==2.7.0
requests26: requests==2.6.0
Expand Down

0 comments on commit 10a90c7

Please sign in to comment.