-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path_http_client.py
More file actions
246 lines (179 loc) · 8.15 KB
/
_http_client.py
File metadata and controls
246 lines (179 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
import requests
from verda._version import __version__
from verda.exceptions import APIException
def handle_error(response: requests.Response) -> None:
"""Checks for the response status code and raises an exception if it's 400 or higher.
:param response: the API call response
:raises APIException: an api exception with message and error type code
"""
if not response.ok:
data = json.loads(response.text)
code = data['code'] if 'code' in data else None
message = data['message'] if 'message' in data else None
raise APIException(code, message)
class HTTPClient:
"""An http client, a wrapper for the requests library.
For each request, it adds the authentication header with an access token.
If the access token is expired it refreshes it before calling the specified API endpoint.
Also checks the response status code and raises an exception if needed.
"""
def __init__(self, auth_service, base_url: str) -> None:
self._version = __version__
self._base_url = base_url
self._auth_service = auth_service
self._auth_service.authenticate()
def post(
self, url: str, json: dict | None = None, params: dict | None = None, **kwargs
) -> requests.Response:
"""Sends a POST request.
A wrapper for the requests.post method.
Builds the url, uses custom headers, refresh tokens if needed.
:param url: relative url of the API endpoint
:type url: str
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
:type json: dict, optional
:param params: Dictionary of querystring data to attach to the Request, defaults to None
:type params: dict, optional
:raises APIException: an api exception with message and error type code
:return: Response object
:rtype: requests.Response
"""
self._refresh_token_if_expired()
url = self._add_base_url(url)
headers = self._generate_headers()
response = requests.post(url, json=json, headers=headers, params=params, **kwargs)
handle_error(response)
return response
def put(
self, url: str, json: dict | None = None, params: dict | None = None, **kwargs
) -> requests.Response:
"""Sends a PUT request.
A wrapper for the requests.put method.
Builds the url, uses custom headers, refresh tokens if needed.
:param url: relative url of the API endpoint
:type url: str
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
:type json: dict, optional
:param params: Dictionary of querystring data to attach to the Request, defaults to None
:type params: dict, optional
:raises APIException: an api exception with message and error type code
:return: Response object
:rtype: requests.Response
"""
self._refresh_token_if_expired()
url = self._add_base_url(url)
headers = self._generate_headers()
response = requests.put(url, json=json, headers=headers, params=params, **kwargs)
handle_error(response)
return response
def get(self, url: str, params: dict | None = None, **kwargs) -> requests.Response:
"""Sends a GET request.
A wrapper for the requests.get method.
Builds the url, uses custom headers, refresh tokens if needed.
:param url: relative url of the API endpoint
:type url: str
:param params: Dictionary of querystring data to attach to the Request, defaults to None
:type params: dict, optional
:raises APIException: an api exception with message and error type code
:return: Response object
:rtype: requests.Response
"""
self._refresh_token_if_expired()
url = self._add_base_url(url)
headers = self._generate_headers()
response = requests.get(url, params=params, headers=headers, **kwargs)
handle_error(response)
return response
def patch(
self, url: str, json: dict | None = None, params: dict | None = None, **kwargs
) -> requests.Response:
"""Sends a PATCH request.
A wrapper for the requests.patch method.
Builds the url, uses custom headers, refresh tokens if needed.
:param url: relative url of the API endpoint
:type url: str
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
:type json: dict, optional
:param params: Dictionary of querystring data to attach to the Request, defaults to None
:type params: dict, optional
:raises APIException: an api exception with message and error type code
:return: Response object
:rtype: requests.Response
"""
self._refresh_token_if_expired()
url = self._add_base_url(url)
headers = self._generate_headers()
response = requests.patch(url, json=json, headers=headers, params=params, **kwargs)
handle_error(response)
return response
def delete(
self, url: str, json: dict | None = None, params: dict | None = None, **kwargs
) -> requests.Response:
"""Sends a DELETE request.
A wrapper for the requests.delete method.
Builds the url, uses custom headers, refresh tokens if needed.
:param url: relative url of the API endpoint
:type url: str
:param json: A JSON serializable Python object to send in the body of the Request, defaults to None
:type json: dict, optional
:param params: Dictionary of querystring data to attach to the Request, defaults to None
:type params: dict, optional
:raises APIException: an api exception with message and error type code
:return: Response object
:rtype: requests.Response
"""
self._refresh_token_if_expired()
url = self._add_base_url(url)
headers = self._generate_headers()
response = requests.delete(url, headers=headers, json=json, params=params, **kwargs)
handle_error(response)
return response
def _refresh_token_if_expired(self) -> None:
"""Refreshes the access token if it expired.
Uses the refresh token to refresh, and if the refresh token is also expired, uses the client credentials.
:raises APIException: an api exception with message and error type code
"""
if self._auth_service.is_expired():
# try to refresh. if refresh token has expired, reauthenticate
try:
self._auth_service.refresh()
except Exception:
self._auth_service.authenticate()
def _generate_headers(self) -> dict:
"""Generate the default headers for every request.
:return: dict with request headers
:rtype: dict
"""
headers = {
'Authorization': self._generate_bearer_header(),
'User-Agent': self._generate_user_agent(),
'Content-Type': 'application/json',
}
return headers
def _generate_bearer_header(self) -> str:
"""Generate the authorization header Bearer string.
:return: Authorization header Bearer string
:rtype: str
"""
return f'Bearer {self._auth_service._access_token}'
def _generate_user_agent(self) -> str:
"""Generate the user agent string.
:return: user agent string
:rtype: str
"""
# get the first 10 chars of the client id
client_id_truncated = self._auth_service._client_id[:10]
return f'datacrunch-python-v{self._version}-{client_id_truncated}'
def _add_base_url(self, url: str) -> str:
"""Adds the base url to the relative url.
Example:
if the relative url is '/balance'
and the base url is 'https://api.verda.com/v1'
then this method will return 'https://api.verda.com/v1/balance'
:param url: a relative url path
:type url: str
:return: the full url path
:rtype: str
"""
return self._base_url + url