Skip to content

Commit 250335c

Browse files
committed
feature/python-sdk-refactor Updated Type Annotations for all the model/core classes.
1 parent 2d305d0 commit 250335c

File tree

9 files changed

+83
-192
lines changed

9 files changed

+83
-192
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class AnchorObjectInterface:
2-
def is_all_field_none(self):
2+
def is_all_field_none(self) -> None:
33
pass

bunq/sdk/model/core/bunq_model.py

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
from __future__ import annotations
2+
3+
from typing import Dict, List
4+
15
from bunq import Pagination
6+
from bunq.sdk.context.api_context import ApiContext
27
from bunq.sdk.http.bunq_response import BunqResponse
8+
from bunq.sdk.http.bunq_response_raw import BunqResponseRaw
39
from bunq.sdk.json import converter
410

511

@@ -17,39 +23,24 @@ class BunqModel:
1723
__STRING_FORMAT_FIELD_FOR_REQUEST_ONE_UNDERSCORE = '_field_for_request'
1824
__STRING_FORMAT_FIELD_FOR_REQUEST_TWO_UNDERSCORE = '__field_for_request'
1925

20-
def is_all_field_none(self):
26+
def is_all_field_none(self) -> None:
2127
raise NotImplementedError
2228

23-
def to_json(self):
24-
"""
25-
:rtype: str
26-
"""
27-
29+
def to_json(self) -> str:
2830
return converter.class_to_json(self)
2931

3032
@classmethod
31-
def _from_json_array_nested(cls, response_raw):
32-
"""
33-
:type response_raw: BunqResponseRaw
34-
35-
:rtype: BunqResponse[cls]
36-
"""
37-
33+
def _from_json_array_nested(cls, response_raw: BunqResponseRaw) -> BunqResponse[BunqModel]:
3834
json = response_raw.body_bytes.decode()
3935
obj = converter.json_to_class(dict, json)
4036
value = converter.deserialize(cls, obj[cls._FIELD_RESPONSE])
4137

4238
return BunqResponse(value, response_raw.headers)
4339

4440
@classmethod
45-
def _from_json(cls, response_raw, wrapper=None):
46-
"""
47-
:type response_raw: BunqResponseRaw
48-
:type wrapper: str|None
49-
50-
:rtype: BunqResponse[cls]
51-
"""
52-
41+
def _from_json(cls,
42+
response_raw: BunqResponseRaw,
43+
wrapper: str = None) -> BunqResponse[BunqModel]:
5344
json = response_raw.body_bytes.decode()
5445
obj = converter.json_to_class(dict, json)
5546
value = converter.deserialize(
@@ -60,27 +51,16 @@ def _from_json(cls, response_raw, wrapper=None):
6051
return BunqResponse(value, response_raw.headers)
6152

6253
@classmethod
63-
def _unwrap_response_single(cls, obj, wrapper=None):
64-
"""
65-
:type obj: dict
66-
:type wrapper: str|None
67-
68-
:rtype: dict
69-
"""
70-
54+
def _unwrap_response_single(cls,
55+
obj: Dict,
56+
wrapper: str = None) -> Dict:
7157
if wrapper is not None:
7258
return obj[cls._FIELD_RESPONSE][cls._INDEX_FIRST][wrapper]
7359

7460
return obj[cls._FIELD_RESPONSE][cls._INDEX_FIRST]
7561

7662
@classmethod
77-
def _process_for_id(cls, response_raw):
78-
"""
79-
:type response_raw: BunqResponseRaw
80-
81-
:rtype: BunqResponse[int]
82-
"""
83-
63+
def _process_for_id(cls, response_raw: BunqResponseRaw) -> BunqResponse[int]:
8464
from bunq.sdk.model.core.id import Id
8565

8666
json = response_raw.body_bytes.decode()
@@ -93,13 +73,7 @@ def _process_for_id(cls, response_raw):
9373
return BunqResponse(id_.id_, response_raw.headers)
9474

9575
@classmethod
96-
def _process_for_uuid(cls, response_raw):
97-
"""
98-
:type response_raw: BunqResponseRaw
99-
100-
:rtype: BunqResponse[str]
101-
"""
102-
76+
def _process_for_uuid(cls, response_raw: BunqResponseRaw) -> BunqResponse[str]:
10377
from bunq.sdk.model.core.uuid import Uuid
10478

10579
json = response_raw.body_bytes.decode()
@@ -112,14 +86,9 @@ def _process_for_uuid(cls, response_raw):
11286
return BunqResponse(uuid.uuid, response_raw.headers)
11387

11488
@classmethod
115-
def _from_json_list(cls, response_raw, wrapper=None):
116-
"""
117-
:type response_raw: BunqResponseRaw
118-
:type wrapper: str|None
119-
120-
:rtype: BunqResponse[list[cls]]
121-
"""
122-
89+
def _from_json_list(cls,
90+
response_raw: BunqResponseRaw,
91+
wrapper: str = None) -> BunqResponse[List[BunqModel]]:
12392
json = response_raw.body_bytes.decode()
12493
obj = converter.json_to_class(dict, json)
12594
array = obj[cls._FIELD_RESPONSE]
@@ -135,33 +104,19 @@ def _from_json_list(cls, response_raw, wrapper=None):
135104
return BunqResponse(array_deserialized, response_raw.headers, pagination)
136105

137106
@classmethod
138-
def _get_api_context(cls):
139-
"""
140-
:rtype: ApiContext
141-
"""
142-
107+
def _get_api_context(cls) -> ApiContext:
143108
from bunq.sdk.context.bunq_context import BunqContext
144109

145110
return BunqContext.api_context()
146111

147112
@classmethod
148-
def _determine_user_id(cls):
149-
"""
150-
:rtype: int
151-
"""
152-
113+
def _determine_user_id(cls) -> int:
153114
from bunq.sdk.context.bunq_context import BunqContext
154115

155116
return BunqContext.user_context().user_id
156117

157118
@classmethod
158-
def _determine_monetary_account_id(cls, monetary_account_id=None):
159-
"""
160-
:type monetary_account_id: int
161-
162-
:rtype: int
163-
"""
164-
119+
def _determine_monetary_account_id(cls, monetary_account_id: int = None) -> int:
165120
from bunq.sdk.context.bunq_context import BunqContext
166121

167122
if monetary_account_id is None:
@@ -170,7 +125,7 @@ def _determine_monetary_account_id(cls, monetary_account_id=None):
170125
return monetary_account_id
171126

172127
@classmethod
173-
def _remove_field_for_request(cls, json_str: str):
128+
def _remove_field_for_request(cls, json_str: str) -> str:
174129
return json_str.replace(
175130
cls.__STRING_FORMAT_FIELD_FOR_REQUEST_TWO_UNDERSCORE,
176131
cls.__STRING_FORMAT_EMPTY

bunq/sdk/model/core/device_server_internal.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import List, Dict
2+
3+
from bunq.sdk.context.api_context import ApiContext
14
from bunq.sdk.exception.bunq_exception import BunqException
25
from bunq.sdk.http.api_client import ApiClient
36
from bunq.sdk.json import converter
@@ -6,12 +9,15 @@
69

710

811
class DeviceServerInternal(DeviceServer):
9-
_ERROR_API_CONTEXT_IS_NULL = 'ApiContext should not be None,' \
10-
' use the generated class instead.'
12+
_ERROR_API_CONTEXT_IS_NULL = 'ApiContext should not be None, use the generated class instead.'
1113

1214
@classmethod
13-
def create(cls, description, secret, permitted_ips=None,
14-
custom_headers=None, api_context=None):
15+
def create(cls,
16+
api_context: ApiContext,
17+
description: str,
18+
secret: str,
19+
permitted_ips: List[str] = None,
20+
custom_headers: Dict[str, str] = None) -> BunqResponseInt: # TODO: The generated bass class needs to be updated?
1521
"""
1622
Create a new DeviceServer providing the installation token in the header
1723
and signing the request with the private part of the key you used to
@@ -32,7 +38,7 @@ def create(cls, description, secret, permitted_ips=None,
3238
be able to do calls from. These will be linked to the API key.
3339
:type permitted_ips: list[str]
3440
:type custom_headers: dict[str, str]|None
35-
:type api_context: context.ApiContext
41+
:type api_context: ApiContext
3642
3743
:rtype: BunqResponseInt
3844
"""

bunq/sdk/model/core/id.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ class Id(BunqModel):
66
:type _id_: int
77
"""
88

9-
def __init__(self):
9+
def __init__(self) -> None:
1010
self._id_ = None
1111

1212
@property
13-
def id_(self):
14-
"""
15-
:rtype: int
16-
"""
17-
13+
def id_(self) -> int:
1814
return self._id_
1915

20-
def is_all_field_none(self):
16+
def is_all_field_none(self) -> bool:
2117
if self.id_ is not None:
2218
return False
2319

bunq/sdk/model/core/installation.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
from bunq.sdk.context.api_context import ApiContext
12
from bunq.sdk.http.api_client import ApiClient
3+
from bunq.sdk.http.bunq_response import BunqResponse
24
from bunq.sdk.json import converter
35
from bunq.sdk.model.core.bunq_model import BunqModel
6+
from bunq.sdk.model.core.id import Id
7+
from bunq.sdk.model.core.public_key_server import PublicKeyServer
8+
from bunq.sdk.model.core.session_token import SessionToken
49

510

611
class Installation(BunqModel):
@@ -16,37 +21,27 @@ class Installation(BunqModel):
1621
# Field constants.
1722
FIELD_CLIENT_PUBLIC_KEY = "client_public_key"
1823

19-
def __init__(self):
24+
def __init__(self) -> None:
2025
self._id_ = None
2126
self._token = None
2227
self._server_public_key = None
2328

2429
@property
25-
def id_(self):
26-
"""
27-
:rtype: Id
28-
"""
29-
30+
def id_(self) -> Id:
3031
return self._id_
3132

3233
@property
33-
def token(self):
34-
"""
35-
:rtype: SessionToken
36-
"""
37-
34+
def token(self) -> SessionToken:
3835
return self._token
3936

4037
@property
41-
def server_public_key(self):
42-
"""
43-
:rtype: PublicKeyServer
44-
"""
45-
38+
def server_public_key(self) -> PublicKeyServer:
4639
return self._server_public_key
4740

4841
@classmethod
49-
def create(cls, api_context, public_key_string):
42+
def create(cls,
43+
api_context: ApiContext,
44+
public_key_string: str) -> BunqResponse: # TODO: Check
5045
"""
5146
:type api_context: ApiContext
5247
:type public_key_string: str
@@ -55,28 +50,20 @@ def create(cls, api_context, public_key_string):
5550
"""
5651

5752
api_client = ApiClient(api_context)
58-
body_bytes = cls.generate_request_body_bytes(
59-
public_key_string
60-
)
53+
body_bytes = cls.generate_request_body_bytes(public_key_string)
6154
response_raw = api_client.post(cls._ENDPOINT_URL_POST, body_bytes, {})
6255

6356
return cls._from_json_array_nested(response_raw)
6457

6558
@classmethod
66-
def generate_request_body_bytes(cls, public_key_string):
67-
"""
68-
:type public_key_string: str
69-
70-
:rtype: bytes
71-
"""
72-
59+
def generate_request_body_bytes(cls, public_key_string: str) -> bytes:
7360
return converter.class_to_json(
7461
{
7562
cls.FIELD_CLIENT_PUBLIC_KEY: public_key_string,
7663
}
7764
).encode()
7865

79-
def is_all_field_none(self):
66+
def is_all_field_none(self) -> bool:
8067
if self.id_ is not None:
8168
return False
8269

bunq/sdk/model/core/public_key_server.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ class PublicKeyServer(BunqModel):
66
:type _server_public_key: str
77
"""
88

9-
def __init__(self):
9+
def __init__(self) -> None:
1010
self._server_public_key = None
1111

1212
@property
13-
def server_public_key(self):
14-
"""
15-
:rtype: str
16-
"""
17-
13+
def server_public_key(self) -> str:
1814
return self._server_public_key
1915

20-
def is_all_field_none(self):
16+
def is_all_field_none(self) -> bool:
2117
if self.server_public_key is not None:
2218
return False
2319

0 commit comments

Comments
 (0)