Skip to content

Commit 96fa5dc

Browse files
committed
Allow for custom requests sessions
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.
1 parent e9f70f4 commit 96fa5dc

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

cortex4py/api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(self, url, api_key, **kwargs):
2929
self.__base_url = '{}/api/'.format(url)
3030
self.__proxies = kwargs.get('proxies', {})
3131
self.__verify_cert = kwargs.get('verify_cert', kwargs.get('cert', True))
32+
self.__session = kwargs.get('session', requests.Session())
3233

3334
self.organizations = OrganizationsController(self)
3435
self.users = UsersController(self)
@@ -61,7 +62,7 @@ def do_get(self, endpoint, params={}):
6162
}
6263

6364
try:
64-
response = requests.get('{}{}'.format(self.__base_url, endpoint),
65+
response = self.__session.get('{}{}'.format(self.__base_url, endpoint),
6566
headers=headers,
6667
params=params,
6768
proxies=self.__proxies,
@@ -78,7 +79,7 @@ def do_file_post(self, endpoint, data, **kwargs):
7879
}
7980

8081
try:
81-
response = requests.post('{}{}'.format(self.__base_url, endpoint),
82+
response = self.__session.post('{}{}'.format(self.__base_url, endpoint),
8283
headers=headers,
8384
proxies=self.__proxies,
8485
data=data,
@@ -96,7 +97,7 @@ def do_post(self, endpoint, data, params={}, **kwargs):
9697
}
9798

9899
try:
99-
response = requests.post('{}{}'.format(self.__base_url, endpoint),
100+
response = self.__session.post('{}{}'.format(self.__base_url, endpoint),
100101
headers=headers,
101102
proxies=self.__proxies,
102103
json=data,
@@ -115,7 +116,7 @@ def do_patch(self, endpoint, data, params={}):
115116
}
116117

117118
try:
118-
response = requests.patch('{}{}'.format(self.__base_url, endpoint),
119+
response = self.__session.patch('{}{}'.format(self.__base_url, endpoint),
119120
headers=headers,
120121
proxies=self.__proxies,
121122
json=data,
@@ -132,7 +133,7 @@ def do_delete(self, endpoint):
132133
}
133134

134135
try:
135-
response = requests.delete('{}{}'.format(self.__base_url, endpoint),
136+
response = self.__session.delete('{}{}'.format(self.__base_url, endpoint),
136137
headers=headers,
137138
proxies=self.__proxies,
138139
verify=self.__verify_cert)

0 commit comments

Comments
 (0)