Skip to content

Commit 6fea894

Browse files
committed
Use a requests.Session object for all request calls
1 parent b758a32 commit 6fea894

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

HISTORY.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ History
55
0.7.4 (unreleased)
66
------------------
77

8-
*
8+
* Use a `requests.Session` object for all api calls
99

1010
0.7.3 (2017-05-18)
1111
------------------

iland/api.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Api(object):
2828
_token_expiration_time = None
2929

3030
_verify_ssl = True
31+
_session = None
3132

3233
def __init__(self, client_id, client_secret, username, password,
3334
base_url=None, access_token_url=None, verify_ssl=True):
@@ -57,6 +58,7 @@ def __init__(self, client_id, client_secret, username, password,
5758
# testing reason)
5859
self._refresh_token_url = access_token_url + '?refresh=1'
5960
self._verify_ssl = verify_ssl
61+
self._session = requests.Session()
6062

6163
def _get_access_token(self):
6264

@@ -70,7 +72,7 @@ def _get_access_token(self):
7072
'username': self._username,
7173
'password': self._password,
7274
'grant_type': 'password'}
73-
r = requests.post(
75+
r = self._session.post(
7476
self._access_token_url, data=params, verify=self._verify_ssl)
7577
json_payload = json.loads(r.content.decode('ascii'))
7678
if r.status_code not in [200, 201, 202]:
@@ -94,8 +96,8 @@ def _refresh_token(self):
9496
'grant_type': 'refresh_token',
9597
'refresh_token': self._token['refresh_token']
9698
}
97-
r = requests.post(self._refresh_token_url, data=params,
98-
verify=self._verify_ssl)
99+
r = self._session.post(self._refresh_token_url, data=params,
100+
verify=self._verify_ssl)
99101
json_payload = json.loads(r.content.decode('ascii'))
100102
if r.status_code not in [200, 201, 202]:
101103
raise UnauthorizedException(json_payload)
@@ -127,15 +129,17 @@ def _do_request(self, rpath, verb='GET', form_data=None):
127129
'Authorization': 'Bearer %s' % self._get_access_token_string(),
128130
'content-type': 'application/json'}
129131
if verb == 'GET':
130-
r = requests.get(url, headers=headers, verify=self._verify_ssl)
132+
r = self._session.get(url, headers=headers,
133+
verify=self._verify_ssl)
131134
elif verb == 'PUT':
132-
r = requests.put(url, data=data, headers=headers,
133-
verify=self._verify_ssl)
135+
r = self._session.put(url, data=data, headers=headers,
136+
verify=self._verify_ssl)
134137
elif verb == 'POST':
135-
r = requests.post(url, data=data, headers=headers,
136-
verify=self._verify_ssl)
138+
r = self._session.post(url, data=data, headers=headers,
139+
verify=self._verify_ssl)
137140
elif verb == 'DELETE':
138-
r = requests.delete(url, headers=headers, verify=self._verify_ssl)
141+
r = self._session.delete(url, headers=headers,
142+
verify=self._verify_ssl)
139143
else:
140144
raise ApiException({'message': 'Unsupported HTTP verb %s' % verb})
141145

0 commit comments

Comments
 (0)