Skip to content

Commit

Permalink
Allow for custom requests sessions
Browse files Browse the repository at this point in the history
The requests module internally works with Session objects. These can be
customised in behaviour e.g. using retry adapters. This is extremely
useful for error handling because the user application doesn't even get
to see and need to handle errors until a configurable number of retries
with optional backoff has been tried. Unfortunately, the module-level
API of the requests module does not allow to supply such a customised
Session object. Instead it creates a new Session for each request and
needs to use it as a context handler to make sure it leaks no sockets -
see
https://github.com/psf/requests/blob/143150233162d609330941ec2aacde5ed4caa510/requests/api.py#L57
for details. The module-level API is a very thin shim which direcly
hands all requests to this per-request Session object.

This change switches the Cortex API layer to use a Session object for
requests directly and adds a parameter so users can provide their own
customised Session object.

As a side-effect, this switches the module to using a persistent Session
object that is re-used across requests. This should be more efficient in
that it can re-use an established connection and potentially connection
pooling.
  • Loading branch information
michaelweiser committed Oct 20, 2020
1 parent e9f70f4 commit 96fa5dc
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cortex4py/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, url, api_key, **kwargs):
self.__base_url = '{}/api/'.format(url)
self.__proxies = kwargs.get('proxies', {})
self.__verify_cert = kwargs.get('verify_cert', kwargs.get('cert', True))
self.__session = kwargs.get('session', requests.Session())

self.organizations = OrganizationsController(self)
self.users = UsersController(self)
Expand Down Expand Up @@ -61,7 +62,7 @@ def do_get(self, endpoint, params={}):
}

try:
response = requests.get('{}{}'.format(self.__base_url, endpoint),
response = self.__session.get('{}{}'.format(self.__base_url, endpoint),
headers=headers,
params=params,
proxies=self.__proxies,
Expand All @@ -78,7 +79,7 @@ def do_file_post(self, endpoint, data, **kwargs):
}

try:
response = requests.post('{}{}'.format(self.__base_url, endpoint),
response = self.__session.post('{}{}'.format(self.__base_url, endpoint),
headers=headers,
proxies=self.__proxies,
data=data,
Expand All @@ -96,7 +97,7 @@ def do_post(self, endpoint, data, params={}, **kwargs):
}

try:
response = requests.post('{}{}'.format(self.__base_url, endpoint),
response = self.__session.post('{}{}'.format(self.__base_url, endpoint),
headers=headers,
proxies=self.__proxies,
json=data,
Expand All @@ -115,7 +116,7 @@ def do_patch(self, endpoint, data, params={}):
}

try:
response = requests.patch('{}{}'.format(self.__base_url, endpoint),
response = self.__session.patch('{}{}'.format(self.__base_url, endpoint),
headers=headers,
proxies=self.__proxies,
json=data,
Expand All @@ -132,7 +133,7 @@ def do_delete(self, endpoint):
}

try:
response = requests.delete('{}{}'.format(self.__base_url, endpoint),
response = self.__session.delete('{}{}'.format(self.__base_url, endpoint),
headers=headers,
proxies=self.__proxies,
verify=self.__verify_cert)
Expand Down

0 comments on commit 96fa5dc

Please sign in to comment.