1
+ from __future__ import annotations
2
+
3
+ import typing
1
4
import uuid
5
+ from typing import Dict , List
2
6
from urllib .parse import urlencode
3
7
4
8
import requests
9
+ from requests import Response
5
10
6
11
from bunq .sdk .exception .exception_factory import ExceptionFactory
7
12
from bunq .sdk .http .bunq_response_raw import BunqResponseRaw
8
13
from bunq .sdk .json import converter
9
14
from bunq .sdk .security import security
10
15
16
+ if typing .TYPE_CHECKING :
17
+ from bunq .sdk .context .api_context import ApiContext
18
+
11
19
12
- class ApiClient ( object ) :
20
+ class ApiClient :
13
21
"""
14
22
:type _api_context: ApiContext
15
23
"""
@@ -77,16 +85,23 @@ class ApiClient(object):
77
85
# Empty bytes
78
86
BYTES_EMPTY = b''
79
87
80
- def __init__ (self , api_context ):
88
+ def __init__ (self , api_context : ApiContext ) -> None :
89
+ """
90
+
91
+ :param api_context:
92
+ """
93
+
81
94
self ._api_context = api_context
82
95
83
- def post (self , uri_relative , request_bytes , custom_headers ):
96
+ def post (self ,
97
+ uri_relative : str ,
98
+ request_bytes : bytes ,
99
+ custom_headers : Dict [str , str ]) -> BunqResponseRaw :
84
100
"""
85
- :type uri_relative: str
86
- :type request_bytes: bytes
87
- :type custom_headers: dict[str, str]
88
101
89
- :return: BunqResponseRaw
102
+ :param uri_relative:
103
+ :param request_bytes:
104
+ :param custom_headers:
90
105
"""
91
106
92
107
return self ._request (
@@ -97,15 +112,19 @@ def post(self, uri_relative, request_bytes, custom_headers):
97
112
custom_headers
98
113
)
99
114
100
- def _request (self , method , uri_relative , request_bytes , params , custom_headers ):
115
+ def _request (self ,
116
+ method : str ,
117
+ uri_relative : str ,
118
+ request_bytes : bytes ,
119
+ params : Dict [str , str ],
120
+ custom_headers : Dict [str , str ]) -> BunqResponseRaw :
101
121
"""
102
- :type method: str
103
- :type uri_relative: str
104
- :type request_bytes: bytes
105
- :type params: dict[str, str]
106
- :type custom_headers: dict[str, str]
107
122
108
- :return: BunqResponseRaw
123
+ :param method:
124
+ :param uri_relative:
125
+ :param request_bytes:
126
+ :param params:
127
+ :param custom_headers:
109
128
"""
110
129
111
130
from bunq .sdk .context .bunq_context import BunqContext
@@ -143,27 +162,31 @@ def _request(self, method, uri_relative, request_bytes, params, custom_headers):
143
162
return self ._create_bunq_response_raw (response )
144
163
145
164
@classmethod
146
- def _append_params_to_uri (cls , uri , params ):
165
+ def _append_params_to_uri (cls ,
166
+ uri : str ,
167
+ params : Dict [str , str ]) -> str :
147
168
"""
148
- :type uri: str
149
- :type params: dict[str, str]
150
169
151
- :rtype: str
170
+ :param uri:
171
+ :param params:
152
172
"""
153
173
154
174
if params :
155
175
return uri + cls .DELIMITER_URL_QUERY + urlencode (params )
156
176
157
177
return uri
158
178
159
- def _get_all_headers (self , method , endpoint , request_bytes , custom_headers ):
179
+ def _get_all_headers (self ,
180
+ method : str ,
181
+ endpoint : str ,
182
+ request_bytes : bytes ,
183
+ custom_headers : Dict [str , str ]) -> Dict [str , str ]:
160
184
"""
161
- :type method: str
162
- :type endpoint: str
163
- :type request_bytes: bytes
164
- :type custom_headers: dict[str, str]
165
185
166
- :rtype: dict[str, str]
186
+ :param method:
187
+ :param endpoint:
188
+ :param request_bytes:
189
+ :param custom_headers:
167
190
"""
168
191
169
192
headers = self ._get_default_headers ()
@@ -182,11 +205,7 @@ def _get_all_headers(self, method, endpoint, request_bytes, custom_headers):
182
205
return headers
183
206
184
207
@classmethod
185
- def _get_default_headers (cls ):
186
- """
187
- :rtype: dict[str, str]
188
- """
189
-
208
+ def _get_default_headers (cls ) -> Dict [str , str ]:
190
209
return {
191
210
cls .HEADER_USER_AGENT : cls .USER_AGENT_BUNQ ,
192
211
cls .HEADER_REQUEST_ID : cls ._generate_random_request_id (),
@@ -197,27 +216,22 @@ def _get_default_headers(cls):
197
216
}
198
217
199
218
@staticmethod
200
- def _generate_random_request_id ():
201
- """
202
- :rtype: str
203
- """
204
-
219
+ def _generate_random_request_id () -> str :
205
220
return str (uuid .uuid4 ())
206
221
207
- def _get_uri_full (self , uri_relative ) :
222
+ def _get_uri_full (self , uri_relative : str ) -> str :
208
223
"""
209
- :type uri_relative: str
210
224
211
- :rtype: str
225
+ :param uri_relative:
212
226
"""
213
227
214
228
return self ._api_context .environment_type .uri_base + uri_relative
215
229
216
- def _assert_response_success (self , response ) :
230
+ def _assert_response_success (self , response : Response ) -> None :
217
231
"""
218
- :type response: requests.Response
219
232
220
- :rtype: None
233
+ :type response:
234
+
221
235
:raise ApiException: When the response is not successful.
222
236
"""
223
237
@@ -229,20 +243,19 @@ def _assert_response_success(self, response):
229
243
)
230
244
231
245
@classmethod
232
- def _create_bunq_response_raw (cls , response ) :
246
+ def _create_bunq_response_raw (cls , response : Response ) -> BunqResponseRaw :
233
247
"""
234
- :type response: requests.Response
235
248
236
- :rtype: BunqResponseRaw
249
+ :param response:
250
+ :return:
237
251
"""
238
252
239
253
return BunqResponseRaw (response .content , response .headers )
240
254
241
- def _fetch_all_error_message (self , response ) :
255
+ def _fetch_all_error_message (self , response : Response ) -> List [ str ] :
242
256
"""
243
- :type response: requests.Response
244
257
245
- :rtype: list[str]
258
+ :param response:
246
259
"""
247
260
248
261
response_content_string = response .content .decode ()
@@ -254,11 +267,11 @@ def _fetch_all_error_message(self, response):
254
267
except ValueError :
255
268
return [response_content_string ]
256
269
257
- def _fetch_error_descriptions (self , error_dict ) :
270
+ def _fetch_error_descriptions (self , error_dict : Dict [ str , List [ Dict [ str , str ]]]) -> List [ str ] :
258
271
"""
259
- :type error_dict: dict[str, list[dict[str, str]]
260
272
261
- :rtype: list[str]
273
+ :param error_dict:
274
+ :return:
262
275
"""
263
276
264
277
error_descriptions = []
@@ -269,11 +282,10 @@ def _fetch_error_descriptions(self, error_dict):
269
282
270
283
return error_descriptions
271
284
272
- def _fetch_response_id (self , response ) :
285
+ def _fetch_response_id (self , response : Response ) -> str :
273
286
"""
274
- :type response: requests.Response
275
287
276
- :rtype: str
288
+ :param response:
277
289
"""
278
290
279
291
headers = response .headers
@@ -286,13 +298,15 @@ def _fetch_response_id(self, response):
286
298
287
299
return self ._ERROR_COULD_NOT_DETERMINE_RESPONSE_ID_HEADER
288
300
289
- def put (self , uri_relative , request_bytes , custom_headers ):
301
+ def put (self ,
302
+ uri_relative : str ,
303
+ request_bytes : bytes ,
304
+ custom_headers : Dict ) -> BunqResponseRaw :
290
305
"""
291
- :type uri_relative: str
292
- :type request_bytes: bytes
293
- :type custom_headers: dict[str, str]
294
306
295
- :rtype: BunqResponseRaw
307
+ :param uri_relative:
308
+ :param request_bytes:
309
+ :param custom_headers:
296
310
"""
297
311
298
312
return self ._request (
@@ -303,13 +317,15 @@ def put(self, uri_relative, request_bytes, custom_headers):
303
317
custom_headers
304
318
)
305
319
306
- def get (self , uri_relative , params , custom_headers ):
320
+ def get (self ,
321
+ uri_relative : str ,
322
+ params : Dict [str , str ],
323
+ custom_headers : Dict [str , str ]) -> BunqResponseRaw :
307
324
"""
308
- :type uri_relative: str
309
- :type params: dict[str, str]
310
- :type custom_headers: dict[str, str]
311
325
312
- :rtype: BunqResponseRaw
326
+ :param uri_relative:
327
+ :param params:
328
+ :param custom_headers:
313
329
"""
314
330
315
331
return self ._request (
@@ -320,12 +336,13 @@ def get(self, uri_relative, params, custom_headers):
320
336
custom_headers
321
337
)
322
338
323
- def delete (self , uri_relative , custom_headers ):
339
+ def delete (self ,
340
+ uri_relative : str ,
341
+ custom_headers : Dict [str , str ]) -> BunqResponseRaw :
324
342
"""
325
- :type uri_relative: str
326
- :type custom_headers: dict[str, str]
327
343
328
- :rtype: BunqResponseRaw
344
+ :param uri_relative:
345
+ :param custom_headers:
329
346
"""
330
347
331
348
return self ._request (
0 commit comments