Skip to content

Commit b73076f

Browse files
committed
feature/python-sdk-refactor Updated Type Annotations for all the Http classes (missed api_client).
1 parent 56ca196 commit b73076f

File tree

1 file changed

+83
-66
lines changed

1 file changed

+83
-66
lines changed

bunq/sdk/http/api_client.py

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
from __future__ import annotations
2+
3+
import typing
14
import uuid
5+
from typing import Dict, List
26
from urllib.parse import urlencode
37

48
import requests
9+
from requests import Response
510

611
from bunq.sdk.exception.exception_factory import ExceptionFactory
712
from bunq.sdk.http.bunq_response_raw import BunqResponseRaw
813
from bunq.sdk.json import converter
914
from bunq.sdk.security import security
1015

16+
if typing.TYPE_CHECKING:
17+
from bunq.sdk.context.api_context import ApiContext
18+
1119

12-
class ApiClient(object):
20+
class ApiClient:
1321
"""
1422
:type _api_context: ApiContext
1523
"""
@@ -77,16 +85,23 @@ class ApiClient(object):
7785
# Empty bytes
7886
BYTES_EMPTY = b''
7987

80-
def __init__(self, api_context):
88+
def __init__(self, api_context: ApiContext) -> None:
89+
"""
90+
91+
:param api_context:
92+
"""
93+
8194
self._api_context = api_context
8295

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:
84100
"""
85-
:type uri_relative: str
86-
:type request_bytes: bytes
87-
:type custom_headers: dict[str, str]
88101
89-
:return: BunqResponseRaw
102+
:param uri_relative:
103+
:param request_bytes:
104+
:param custom_headers:
90105
"""
91106

92107
return self._request(
@@ -97,15 +112,19 @@ def post(self, uri_relative, request_bytes, custom_headers):
97112
custom_headers
98113
)
99114

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:
101121
"""
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]
107122
108-
:return: BunqResponseRaw
123+
:param method:
124+
:param uri_relative:
125+
:param request_bytes:
126+
:param params:
127+
:param custom_headers:
109128
"""
110129

111130
from bunq.sdk.context.bunq_context import BunqContext
@@ -143,27 +162,31 @@ def _request(self, method, uri_relative, request_bytes, params, custom_headers):
143162
return self._create_bunq_response_raw(response)
144163

145164
@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:
147168
"""
148-
:type uri: str
149-
:type params: dict[str, str]
150169
151-
:rtype: str
170+
:param uri:
171+
:param params:
152172
"""
153173

154174
if params:
155175
return uri + cls.DELIMITER_URL_QUERY + urlencode(params)
156176

157177
return uri
158178

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]:
160184
"""
161-
:type method: str
162-
:type endpoint: str
163-
:type request_bytes: bytes
164-
:type custom_headers: dict[str, str]
165185
166-
:rtype: dict[str, str]
186+
:param method:
187+
:param endpoint:
188+
:param request_bytes:
189+
:param custom_headers:
167190
"""
168191

169192
headers = self._get_default_headers()
@@ -182,11 +205,7 @@ def _get_all_headers(self, method, endpoint, request_bytes, custom_headers):
182205
return headers
183206

184207
@classmethod
185-
def _get_default_headers(cls):
186-
"""
187-
:rtype: dict[str, str]
188-
"""
189-
208+
def _get_default_headers(cls) -> Dict[str, str]:
190209
return {
191210
cls.HEADER_USER_AGENT: cls.USER_AGENT_BUNQ,
192211
cls.HEADER_REQUEST_ID: cls._generate_random_request_id(),
@@ -197,27 +216,22 @@ def _get_default_headers(cls):
197216
}
198217

199218
@staticmethod
200-
def _generate_random_request_id():
201-
"""
202-
:rtype: str
203-
"""
204-
219+
def _generate_random_request_id() -> str:
205220
return str(uuid.uuid4())
206221

207-
def _get_uri_full(self, uri_relative):
222+
def _get_uri_full(self, uri_relative: str) -> str:
208223
"""
209-
:type uri_relative: str
210224
211-
:rtype: str
225+
:param uri_relative:
212226
"""
213227

214228
return self._api_context.environment_type.uri_base + uri_relative
215229

216-
def _assert_response_success(self, response):
230+
def _assert_response_success(self, response: Response) -> None:
217231
"""
218-
:type response: requests.Response
219232
220-
:rtype: None
233+
:type response:
234+
221235
:raise ApiException: When the response is not successful.
222236
"""
223237

@@ -229,20 +243,19 @@ def _assert_response_success(self, response):
229243
)
230244

231245
@classmethod
232-
def _create_bunq_response_raw(cls, response):
246+
def _create_bunq_response_raw(cls, response: Response) -> BunqResponseRaw:
233247
"""
234-
:type response: requests.Response
235248
236-
:rtype: BunqResponseRaw
249+
:param response:
250+
:return:
237251
"""
238252

239253
return BunqResponseRaw(response.content, response.headers)
240254

241-
def _fetch_all_error_message(self, response):
255+
def _fetch_all_error_message(self, response: Response) -> List[str]:
242256
"""
243-
:type response: requests.Response
244257
245-
:rtype: list[str]
258+
:param response:
246259
"""
247260

248261
response_content_string = response.content.decode()
@@ -254,11 +267,11 @@ def _fetch_all_error_message(self, response):
254267
except ValueError:
255268
return [response_content_string]
256269

257-
def _fetch_error_descriptions(self, error_dict):
270+
def _fetch_error_descriptions(self, error_dict: Dict[str, List[Dict[str, str]]]) -> List[str]:
258271
"""
259-
:type error_dict: dict[str, list[dict[str, str]]
260272
261-
:rtype: list[str]
273+
:param error_dict:
274+
:return:
262275
"""
263276

264277
error_descriptions = []
@@ -269,11 +282,10 @@ def _fetch_error_descriptions(self, error_dict):
269282

270283
return error_descriptions
271284

272-
def _fetch_response_id(self, response):
285+
def _fetch_response_id(self, response: Response) -> str:
273286
"""
274-
:type response: requests.Response
275287
276-
:rtype: str
288+
:param response:
277289
"""
278290

279291
headers = response.headers
@@ -286,13 +298,15 @@ def _fetch_response_id(self, response):
286298

287299
return self._ERROR_COULD_NOT_DETERMINE_RESPONSE_ID_HEADER
288300

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:
290305
"""
291-
:type uri_relative: str
292-
:type request_bytes: bytes
293-
:type custom_headers: dict[str, str]
294306
295-
:rtype: BunqResponseRaw
307+
:param uri_relative:
308+
:param request_bytes:
309+
:param custom_headers:
296310
"""
297311

298312
return self._request(
@@ -303,13 +317,15 @@ def put(self, uri_relative, request_bytes, custom_headers):
303317
custom_headers
304318
)
305319

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:
307324
"""
308-
:type uri_relative: str
309-
:type params: dict[str, str]
310-
:type custom_headers: dict[str, str]
311325
312-
:rtype: BunqResponseRaw
326+
:param uri_relative:
327+
:param params:
328+
:param custom_headers:
313329
"""
314330

315331
return self._request(
@@ -320,12 +336,13 @@ def get(self, uri_relative, params, custom_headers):
320336
custom_headers
321337
)
322338

323-
def delete(self, uri_relative, custom_headers):
339+
def delete(self,
340+
uri_relative: str,
341+
custom_headers: Dict[str, str]) -> BunqResponseRaw:
324342
"""
325-
:type uri_relative: str
326-
:type custom_headers: dict[str, str]
327343
328-
:rtype: BunqResponseRaw
344+
:param uri_relative:
345+
:param custom_headers:
329346
"""
330347

331348
return self._request(

0 commit comments

Comments
 (0)