Skip to content

Commit 7279a09

Browse files
committed
feature/python-sdk-refactor Updated Type Annotations for all the Context classes.
1 parent 186f64c commit 7279a09

File tree

6 files changed

+119
-205
lines changed

6 files changed

+119
-205
lines changed

bunq/sdk/context/api_context.py

Lines changed: 56 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
import datetime
4+
from typing import List, Optional
25

36
from Cryptodome.PublicKey import RSA
47

@@ -7,6 +10,7 @@
710
from bunq.sdk.context.session_context import SessionContext
811
from bunq.sdk.exception.bunq_exception import BunqException
912
from bunq.sdk.json import converter
13+
from bunq.sdk.model.core.session_server import SessionServer
1014
from bunq.sdk.model.generated import endpoint
1115
from bunq.sdk.security import security
1216

@@ -33,14 +37,19 @@ class ApiContext(object):
3337
# Default path to the file storing serialized API context
3438
_PATH_API_CONTEXT_DEFAULT = 'bunq.conf'
3539

36-
def __init__(self, environment_type, api_key, device_description,
37-
permitted_ips=None, proxy_url=None):
40+
def __init__(self,
41+
environment_type: ApiEnvironmentType,
42+
api_key: str,
43+
device_description: str,
44+
permitted_ips: List[str] = None,
45+
proxy_url: List[str] = None) -> None:
3846
"""
39-
:type environment_type: ApiEnvironmentType
40-
:type api_key: str
41-
:type device_description: str
42-
:type permitted_ips: list[str]|None
43-
:type proxy_url: str|None
47+
48+
:param environment_type:
49+
:param api_key:
50+
:param device_description:
51+
:param permitted_ips:
52+
:param proxy_url:
4453
"""
4554

4655
if permitted_ips is None:
@@ -53,22 +62,20 @@ def __init__(self, environment_type, api_key, device_description,
5362
self._proxy_url = proxy_url
5463
self._initialize(device_description, permitted_ips)
5564

56-
def _initialize(self, device_description, permitted_ips):
65+
def _initialize(self,
66+
device_description: str,
67+
permitted_ips: List[str]) -> None:
5768
"""
58-
:type device_description: str
59-
:type permitted_ips: list[str]
6069
61-
:rtype: None
70+
:param device_description:
71+
:param permitted_ips:
6272
"""
6373

6474
self._initialize_installation()
6575
self._register_device(device_description, permitted_ips)
6676
self._initialize_session()
6777

68-
def _initialize_installation(self):
69-
"""
70-
:rtype: None
71-
"""
78+
def _initialize_installation(self) -> None:
7279
from bunq.sdk.model.core.installation import Installation
7380

7481
private_key_client = security.generate_rsa_private_key()
@@ -88,13 +95,13 @@ def _initialize_installation(self):
8895
public_key_server
8996
)
9097

91-
def _register_device(self, device_description,
92-
permitted_ips):
98+
def _register_device(self,
99+
device_description: str,
100+
permitted_ips: List[str]) -> None:
93101
"""
94-
:type device_description: str
95-
:type permitted_ips: list[]
96102
97-
:rtype: None
103+
:param device_description:
104+
:param permitted_ips:
98105
"""
99106

100107
from bunq.sdk.model.core.device_server_internal import DeviceServerInternal
@@ -106,11 +113,7 @@ def _register_device(self, device_description,
106113
api_context=self
107114
)
108115

109-
def _initialize_session(self):
110-
"""
111-
:rtype: None
112-
"""
113-
116+
def _initialize_session(self) -> None:
114117
from bunq.sdk.model.core.session_server import SessionServer
115118

116119
session_server = SessionServer.create(self).value
@@ -121,11 +124,10 @@ def _initialize_session(self):
121124
self._session_context = SessionContext(token, expiry_time, user_id)
122125

123126
@classmethod
124-
def _get_expiry_timestamp(cls, session_server):
127+
def _get_expiry_timestamp(cls, session_server: SessionServer) -> datetime.datetime:
125128
"""
126-
:type session_server: SessionServer
127129
128-
:rtype: datetime.datetime
130+
:param session_server:
129131
"""
130132

131133
timeout_seconds = cls._get_session_timeout_seconds(session_server)
@@ -134,11 +136,10 @@ def _get_expiry_timestamp(cls, session_server):
134136
return time_now + datetime.timedelta(seconds=timeout_seconds)
135137

136138
@classmethod
137-
def _get_session_timeout_seconds(cls, session_server):
139+
def _get_session_timeout_seconds(cls, session_server: SessionServer) -> int:
138140
"""
139-
:type session_server: SessionServer
140141
141-
:rtype: int
142+
:param session_server:
142143
"""
143144

144145
if session_server.user_company is not None:
@@ -158,7 +159,6 @@ def ensure_session_active(self) -> bool:
158159
"""
159160
Resets the session if it has expired.
160161
161-
:rtype: bool
162162
"""
163163

164164
if not self.is_session_active():
@@ -168,11 +168,7 @@ def ensure_session_active(self) -> bool:
168168

169169
return False
170170

171-
def is_session_active(self):
172-
"""
173-
:rtype: bool
174-
"""
175-
171+
def is_session_active(self) -> bool:
176172
if self.session_context is None:
177173
return False
178174

@@ -184,62 +180,40 @@ def is_session_active(self):
184180

185181
return time_to_expiry > time_to_expiry_minimum
186182

187-
def reset_session(self):
183+
def reset_session(self) -> None:
188184
"""
189185
Closes the current session and opens a new one.
190186
191-
:rtype: None
192187
"""
193188

194189
self._drop_session_context()
195190
self._initialize_session()
196191

197-
def _drop_session_context(self):
198-
"""
199-
:rtype: None
200-
"""
201-
192+
def _drop_session_context(self) -> None:
202193
self._session_context = None
203194

204-
def close_session(self):
195+
def close_session(self) -> None:
205196
"""
206197
Closes the current session.
207198
208-
:rtype: None
209199
"""
210200

211201
self._delete_session()
212202
self._drop_session_context()
213203

214-
def _delete_session(self):
215-
"""
216-
:rtype: None
217-
"""
218-
204+
def _delete_session(self) -> None:
219205
endpoint.Session.delete(self._SESSION_ID_DUMMY)
220206

221207
@property
222-
def environment_type(self):
223-
"""
224-
:rtype: ApiEnvironmentType
225-
"""
226-
208+
def environment_type(self) -> ApiEnvironmentType:
227209
return self._environment_type
228210

229211
@property
230-
def api_key(self):
231-
"""
232-
:rtype: str
233-
"""
234-
212+
def api_key(self) -> str:
235213
return self._api_key
236214

237215
@property
238-
def token(self):
239-
"""
240-
:rtype: str
241-
"""
242-
216+
def token(self) -> Optional[str]:
243217
if self._session_context is not None:
244218
return self.session_context.token
245219
elif self._installation_context is not None:
@@ -248,34 +222,21 @@ def token(self):
248222
return None
249223

250224
@property
251-
def installation_context(self):
252-
"""
253-
:rtype: InstallationContext
254-
"""
255-
225+
def installation_context(self) -> InstallationContext:
256226
return self._installation_context
257227

258228
@property
259-
def session_context(self):
260-
"""
261-
:rtype: SessionContext
262-
"""
263-
229+
def session_context(self) -> SessionContext:
264230
return self._session_context
265231

266232
@property
267-
def proxy_url(self):
268-
"""
269-
:rtype: str
270-
"""
271-
233+
def proxy_url(self) -> str:
272234
return self._proxy_url
273235

274-
def save(self, path=None):
236+
def save(self, path: str = None) -> None:
275237
"""
276-
:type path: str
277238
278-
:rtype: None
239+
:param path:
279240
"""
280241

281242
if path is None:
@@ -284,21 +245,19 @@ def save(self, path=None):
284245
with open(path, self._FILE_MODE_WRITE) as file_:
285246
file_.write(self.to_json())
286247

287-
def to_json(self):
248+
def to_json(self) -> str:
288249
"""
289250
Serializes an ApiContext to JSON string.
290251
291-
:rtype: str
292252
"""
293253

294254
return converter.class_to_json(self)
295255

296256
@classmethod
297-
def restore(cls, path=None):
257+
def restore(cls, path: str = None) -> ApiContext:
298258
"""
299-
:type path: str
300259
301-
:rtype: ApiContext
260+
:param path:
302261
"""
303262

304263
if path is None:
@@ -308,18 +267,21 @@ def restore(cls, path=None):
308267
return cls.from_json(file_.read())
309268

310269
@classmethod
311-
def from_json(cls, json_str):
270+
def from_json(cls, json_str: str) -> ApiContext:
312271
"""
313272
Creates an ApiContext instance from JSON string.
314273
315-
:type json_str: str
316-
317-
:rtype: ApiContext
274+
:type json_str:
318275
"""
319276

320277
return converter.json_to_class(ApiContext, json_str)
321278

322-
def __eq__(self, other):
279+
def __eq__(self, other: ApiContext) -> bool:
280+
"""
281+
282+
:param other:
283+
"""
284+
323285
return (self.token == other.token and
324286
self.api_key == other.api_key and
325287
self.environment_type == other.environment_type)

bunq/sdk/context/api_environment_type.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ class ApiEnvironmentType(aenum.AutoNumberEnum):
1111
PRODUCTION = 'https://api.bunq.com/v1/'
1212
SANDBOX = 'https://public-api.sandbox.bunq.com/v1/'
1313

14-
def __init__(self, uri_base):
14+
def __init__(self, uri_base: str) -> None:
1515
"""
16-
:type uri_base: str
16+
17+
:type uri_base:
1718
"""
1819

1920
self._uri_base = uri_base
2021

2122
@property
22-
def uri_base(self):
23-
"""
24-
:rtype: str
25-
"""
26-
23+
def uri_base(self) -> str:
2724
return self._uri_base

bunq/sdk/context/bunq_context.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44

55

66
class BunqContext(object):
7-
_ERROR_CLASS_SHOULD_NOT_BE_INITIALIZED = \
8-
'This class should not be instantiated'
9-
_ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED = \
10-
'ApiContext has not been loaded. Please load ApiContext in BunqContext'
11-
_ERROR_USER_CONTEXT_HAS_NOT_BEEN_LOADED = \
12-
'UserContext has not been loaded, please load this' \
13-
' by loading ApiContext.'
7+
_ERROR_CLASS_SHOULD_NOT_BE_INITIALIZED = 'This class should not be instantiated'
8+
_ERROR_API_CONTEXT_HAS_NOT_BEEN_LOADED = 'ApiContext has not been loaded. Please load ApiContext in BunqContext'
9+
_ERROR_USER_CONTEXT_HAS_NOT_BEEN_LOADED = 'UserContext has not been loaded, please load this by loading ApiContext.'
1410

1511
_api_context = None
1612
_user_context = None
1713

18-
def __init__(self):
14+
def __init__(self) -> None:
1915
raise TypeError(self._ERROR_CLASS_SHOULD_NOT_BE_INITIALIZED)
2016

2117
@classmethod
@@ -24,6 +20,7 @@ def load_api_context(cls, api_context: ApiContext) -> None:
2420
2521
:param api_context:
2622
"""
23+
2724
cls._api_context = api_context
2825
cls._user_context = UserContext(api_context.session_context.user_id)
2926
cls._user_context.init_main_monetary_account()

0 commit comments

Comments
 (0)