Commit 1559547e authored by Jo Sama's avatar Jo Sama 😱
Browse files

Add HTTP session/keep-alive support

parent ad7c2abf
......@@ -78,7 +78,8 @@ class TopDeskClient:
username=None,
password=None,
headers={},
rewrite_url=None):
rewrite_url=None,
use_sessions=True):
self.urls = Endpoints(url)
self.rewrite_url = rewrite_url
self.headers = headers
......@@ -87,6 +88,11 @@ class TopDeskClient:
else:
self.auth = None
if use_sessions:
self.session = requests.Session()
else:
self.session = requests
def _build_headers(self, headers):
request_headers = {}
for h in (self.headers):
......@@ -106,14 +112,14 @@ class TopDeskClient:
method_name,
urlparse(url).path,
params)
r = requests.request(method_name,
(url if self.rewrite_url is None
else url.replace(*self.rewrite_url)),
auth=self.auth,
headers=(self._build_headers({}) if headers is None else
self._build_headers(headers)),
params=params if params is not None else {},
**kwargs)
r = self.session.request(method_name,
(url if self.rewrite_url is None
else url.replace(*self.rewrite_url)),
auth=self.auth,
headers=(self._build_headers({}) if headers is None else
self._build_headers(headers)),
params=params if params is not None else {},
**kwargs)
logger.debug('GOT HTTP %d: %r', r.status_code, r.content)
if return_response:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment