Skip to content

Commit

Permalink
Reuse connections when making API calls
Browse files Browse the repository at this point in the history
By default, the Python requests module will create a new session
object each time it is called. This means that each requests.get
call has a separate connection pool and cannot reuse connections
created by previous requests.

Here we create a class variable with a requests session, which is
used for all instances of the APIClient class.
  • Loading branch information
stevenjm committed May 9, 2018
1 parent 77f25ad commit 8a80f77
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Changelog

Here you find a full list of changes.

Version 2.0.1
-------------

- Reuse HTTP connections between requests

Version 2.0.0
-------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
from distutils.core import setup

VERSION = '2.0.0'
VERSION = '2.0.1'

setup(
name='usabilla-api',
Expand Down
4 changes: 3 additions & 1 deletion usabilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class APIClient(object):
host = 'data.usabilla.com'
host_protocol = 'https://'

session = requests.Session()

def __init__(self, client_key, secret_key):
"""Initialize an APIClient object."""
self.query_parameters = ''
Expand Down Expand Up @@ -237,7 +239,7 @@ def send_signed_request(self, scope):

# Send the request.
request_url = self.host + scope + '?' + canonical_querystring
r = requests.get(self.host_protocol + request_url, headers=headers)
r = self.session.get(self.host_protocol + request_url, headers=headers)
r.raise_for_status()

return r.json()
Expand Down

0 comments on commit 8a80f77

Please sign in to comment.