Skip to content

Commit 56ca196

Browse files
committed
feature/python-sdk-refactor Updated Type Annotations for all the Http classes.
1 parent 78b3a82 commit 56ca196

File tree

4 files changed

+60
-84
lines changed

4 files changed

+60
-84
lines changed

bunq/sdk/context/api_context.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import datetime
4+
import typing
45
from typing import List, Optional
56

67
from Cryptodome.PublicKey import RSA
@@ -10,10 +11,12 @@
1011
from bunq.sdk.context.session_context import SessionContext
1112
from bunq.sdk.exception.bunq_exception import BunqException
1213
from bunq.sdk.json import converter
13-
from bunq.sdk.model.core.session_server import SessionServer
1414
from bunq.sdk.model.generated import endpoint
1515
from bunq.sdk.security import security
1616

17+
if typing.TYPE_CHECKING:
18+
from bunq.sdk.model.core.session_server import SessionServer
19+
1720

1821
class ApiContext(object):
1922
"""
@@ -124,7 +127,8 @@ def _initialize_session(self) -> None:
124127
self._session_context = SessionContext(token, expiry_time, user_id)
125128

126129
@classmethod
127-
def _get_expiry_timestamp(cls, session_server: SessionServer) -> datetime.datetime:
130+
def _get_expiry_timestamp(cls,
131+
session_server: SessionServer) -> datetime.datetime:
128132
"""
129133
130134
:param session_server:
@@ -254,7 +258,7 @@ def to_json(self) -> str:
254258
return converter.class_to_json(self)
255259

256260
@classmethod
257-
def restore(cls, path: str = None) -> ApiContext:
261+
def restore(cls, path: str = None) -> 'ApiContext':
258262
"""
259263
260264
:param path:
@@ -267,7 +271,7 @@ def restore(cls, path: str = None) -> ApiContext:
267271
return cls.from_json(file_.read())
268272

269273
@classmethod
270-
def from_json(cls, json_str: str) -> ApiContext:
274+
def from_json(cls, json_str: str) -> 'ApiContext':
271275
"""
272276
Creates an ApiContext instance from JSON string.
273277
@@ -276,7 +280,7 @@ def from_json(cls, json_str: str) -> ApiContext:
276280

277281
return converter.json_to_class(ApiContext, json_str)
278282

279-
def __eq__(self, other: ApiContext) -> bool:
283+
def __eq__(self, other: 'ApiContext') -> bool:
280284
"""
281285
282286
:param other:

bunq/sdk/http/bunq_response.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1-
class BunqResponse(object):
1+
from __future__ import annotations
2+
3+
# from types import ModuleType
4+
from typing import Dict, TypeVar
5+
6+
from bunq import Pagination
7+
8+
9+
class BunqResponse:
210
"""
311
:type _value: T
412
:type _headers: dict[str, str]
513
:type _pagination: Pagination|None
614
"""
715

8-
def __init__(self, value, headers, pagination=None):
16+
T = TypeVar('T')
17+
18+
def __init__(self,
19+
value: T,
20+
headers: Dict[str, str],
21+
pagination: Pagination = None) -> None:
922
"""
10-
:type value: T
11-
:type headers: dict[str, str]
12-
:type pagination Pagination|None
23+
24+
:param value:
25+
:param headers:
26+
:param pagination:
1327
"""
1428

1529
self._value = value
1630
self._headers = headers
1731
self._pagination = pagination
1832

1933
@property
20-
def value(self):
21-
"""
22-
:rtype: T
23-
"""
24-
34+
def value(self) -> T:
2535
return self._value
2636

2737
@property
28-
def headers(self):
29-
"""
30-
:rtype: dict[str, str]
31-
"""
32-
38+
def headers(self) -> Dict[str, str]:
3339
return self._headers
3440

3541
@property
36-
def pagination(self):
37-
"""
38-
:rtype: Pagination
39-
"""
40-
42+
def pagination(self) -> Pagination:
4143
return self._pagination
4244

4345
@classmethod
44-
def cast_from_bunq_response(cls, bunq_response):
46+
def cast_from_bunq_response(cls, bunq_response: BunqResponse) -> BunqResponse:
4547
"""
4648
:type bunq_response: BunqResponse
4749
"""

bunq/sdk/http/bunq_response_raw.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
class BunqResponseRaw(object):
1+
from typing import Dict
2+
3+
4+
class BunqResponseRaw:
25
"""
36
:type _body_bytes: bytes
47
:type _headers: dict[str, str]
58
"""
69

7-
def __init__(self, body_bytes, headers):
10+
def __init__(self,
11+
body_bytes: bytes,
12+
headers: Dict[str, str]) -> None:
813
"""
9-
:type body_bytes: bytes
10-
:type headers: dict[str, str]
14+
15+
:param body_bytes:
16+
:param headers:
1117
"""
1218

1319
self._body_bytes = body_bytes
1420
self._headers = headers
1521

1622
@property
17-
def body_bytes(self):
18-
"""
19-
:rtype: bytes
20-
"""
21-
23+
def body_bytes(self) -> bytes:
2224
return self._body_bytes
2325

2426
@property
25-
def headers(self):
26-
"""
27-
:rtype: dict[str, str]
28-
"""
29-
27+
def headers(self) -> Dict[str, str]:
3028
return self._headers

bunq/sdk/http/pagination.py

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Dict
2+
13
from bunq.sdk.exception.bunq_exception import BunqException
24

35

4-
class Pagination(object):
6+
class Pagination:
57
"""
68
:type older_id: int|None
79
:type newer_id: int|None
@@ -10,84 +12,57 @@ class Pagination(object):
1012
"""
1113

1214
# Error constants
13-
_ERROR_NO_PREVIOUS_PAGE = 'Could not generate previous page URL params: ' \
14-
'there is no previous page.'
15-
_ERROR_NO_NEXT_PAGE = 'Could not generate next page URL params: ' \
16-
'there is no next page.'
15+
_ERROR_NO_PREVIOUS_PAGE = 'Could not generate previous page URL params: there is no previous page.'
16+
_ERROR_NO_NEXT_PAGE = 'Could not generate next page URL params: there is no next page.'
1717

1818
# URL Param constants
1919
PARAM_OLDER_ID = 'older_id'
2020
PARAM_NEWER_ID = 'newer_id'
2121
PARAM_COUNT = 'count'
2222

23-
def __init__(self):
23+
def __init__(self) -> None:
2424
self.older_id = None
2525
self.newer_id = None
2626
self.future_id = None
2727
self.count = None
2828

2929
@property
30-
def url_params_previous_page(self):
31-
"""
32-
:rtype: dict[str, str]
33-
"""
34-
30+
def url_params_previous_page(self) -> Dict[str, str]:
3531
self.assert_has_previous_page()
3632

3733
params = {self.PARAM_OLDER_ID: str(self.older_id)}
3834
self._add_count_to_params_if_needed(params)
3935

4036
return params
4137

42-
def assert_has_previous_page(self):
38+
def assert_has_previous_page(self) -> None:
4339
"""
40+
4441
:raise: exception.BunqException
4542
"""
4643

4744
if not self.has_previous_page():
4845
raise BunqException(self._ERROR_NO_PREVIOUS_PAGE)
4946

50-
def has_previous_page(self):
51-
"""
52-
:rtype: bool
53-
"""
54-
47+
def has_previous_page(self) -> bool:
5548
return self.older_id is not None
5649

5750
@property
58-
def url_params_count_only(self):
59-
"""
60-
:rtype: dict[str, str]
61-
"""
62-
51+
def url_params_count_only(self) -> Dict[str, str]:
6352
params = {}
6453
self._add_count_to_params_if_needed(params)
6554

6655
return params
6756

68-
def _add_count_to_params_if_needed(self, params):
69-
"""
70-
:type params: dict[str, str]
71-
72-
:rtype: None
73-
"""
74-
57+
def _add_count_to_params_if_needed(self, params: Dict[str, str]) -> None:
7558
if self.count is not None:
7659
params[self.PARAM_COUNT] = str(self.count)
7760

78-
def has_next_page_assured(self):
79-
"""
80-
:rtype: bool
81-
"""
82-
61+
def has_next_page_assured(self) -> bool:
8362
return self.newer_id is not None
8463

8564
@property
86-
def url_params_next_page(self):
87-
"""
88-
:rtype: dict[str, str]
89-
"""
90-
65+
def url_params_next_page(self) -> Dict[str, str]:
9166
self.assert_has_next_page()
9267

9368
params = {self.PARAM_NEWER_ID: str(self._next_id)}
@@ -97,18 +72,15 @@ def url_params_next_page(self):
9772

9873
def assert_has_next_page(self):
9974
"""
75+
10076
:raise: exception.BunqException
10177
"""
10278

10379
if self._next_id is None:
10480
raise BunqException(self._ERROR_NO_NEXT_PAGE)
10581

10682
@property
107-
def _next_id(self):
108-
"""
109-
:rtype: int
110-
"""
111-
83+
def _next_id(self) -> int:
11284
if self.has_next_page_assured():
11385
return self.newer_id
11486

0 commit comments

Comments
 (0)