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 done.

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 directly
hands all requests to this per-request Session object.

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

To keep default behaviour unchanged, a fresh internal session object is
created for each request. This is done so the session does indeed not
accumulate state over time. For example this prevents learning of
cookies from responses which has been observed to confuse Cortex on
subsequent requests.

In the case of a user-supplied session it is the responsibility of the
user to configure it in such a way that it serves their purpose and does
not show behaviour that confuses Cortex.

Signed-off-by: Michael Weiser <[email protected]>
  • Loading branch information
michaelweiser committed Nov 24, 2021
1 parent 98365d0 commit f893497
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 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', None)

self.organizations = OrganizationsController(self)
self.users = UsersController(self)
Expand All @@ -55,13 +56,22 @@ def __recover(exception):
else:
raise CortexError("Unexpected exception") from exception

@property
def session(self):
# if no custom session object has been provided by the user
if self.__session is None:
# start a fresh session without cookies for each request
return requests.sessions.Session()

return self.__session

def do_get(self, endpoint, params={}):
headers = {
'Authorization': 'Bearer {}'.format(self.__api_key)
}

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 +88,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 +106,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 +125,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 +142,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 f893497

Please sign in to comment.