1
+ from __future__ import annotations
2
+
1
3
import datetime
4
+ from typing import List , Optional
2
5
3
6
from Cryptodome .PublicKey import RSA
4
7
7
10
from bunq .sdk .context .session_context import SessionContext
8
11
from bunq .sdk .exception .bunq_exception import BunqException
9
12
from bunq .sdk .json import converter
13
+ from bunq .sdk .model .core .session_server import SessionServer
10
14
from bunq .sdk .model .generated import endpoint
11
15
from bunq .sdk .security import security
12
16
@@ -33,14 +37,19 @@ class ApiContext(object):
33
37
# Default path to the file storing serialized API context
34
38
_PATH_API_CONTEXT_DEFAULT = 'bunq.conf'
35
39
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 :
38
46
"""
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:
44
53
"""
45
54
46
55
if permitted_ips is None :
@@ -53,22 +62,20 @@ def __init__(self, environment_type, api_key, device_description,
53
62
self ._proxy_url = proxy_url
54
63
self ._initialize (device_description , permitted_ips )
55
64
56
- def _initialize (self , device_description , permitted_ips ):
65
+ def _initialize (self ,
66
+ device_description : str ,
67
+ permitted_ips : List [str ]) -> None :
57
68
"""
58
- :type device_description: str
59
- :type permitted_ips: list[str]
60
69
61
- :rtype: None
70
+ :param device_description:
71
+ :param permitted_ips:
62
72
"""
63
73
64
74
self ._initialize_installation ()
65
75
self ._register_device (device_description , permitted_ips )
66
76
self ._initialize_session ()
67
77
68
- def _initialize_installation (self ):
69
- """
70
- :rtype: None
71
- """
78
+ def _initialize_installation (self ) -> None :
72
79
from bunq .sdk .model .core .installation import Installation
73
80
74
81
private_key_client = security .generate_rsa_private_key ()
@@ -88,13 +95,13 @@ def _initialize_installation(self):
88
95
public_key_server
89
96
)
90
97
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 :
93
101
"""
94
- :type device_description: str
95
- :type permitted_ips: list[]
96
102
97
- :rtype: None
103
+ :param device_description:
104
+ :param permitted_ips:
98
105
"""
99
106
100
107
from bunq .sdk .model .core .device_server_internal import DeviceServerInternal
@@ -106,11 +113,7 @@ def _register_device(self, device_description,
106
113
api_context = self
107
114
)
108
115
109
- def _initialize_session (self ):
110
- """
111
- :rtype: None
112
- """
113
-
116
+ def _initialize_session (self ) -> None :
114
117
from bunq .sdk .model .core .session_server import SessionServer
115
118
116
119
session_server = SessionServer .create (self ).value
@@ -121,11 +124,10 @@ def _initialize_session(self):
121
124
self ._session_context = SessionContext (token , expiry_time , user_id )
122
125
123
126
@classmethod
124
- def _get_expiry_timestamp (cls , session_server ) :
127
+ def _get_expiry_timestamp (cls , session_server : SessionServer ) -> datetime . datetime :
125
128
"""
126
- :type session_server: SessionServer
127
129
128
- :rtype: datetime.datetime
130
+ :param session_server:
129
131
"""
130
132
131
133
timeout_seconds = cls ._get_session_timeout_seconds (session_server )
@@ -134,11 +136,10 @@ def _get_expiry_timestamp(cls, session_server):
134
136
return time_now + datetime .timedelta (seconds = timeout_seconds )
135
137
136
138
@classmethod
137
- def _get_session_timeout_seconds (cls , session_server ) :
139
+ def _get_session_timeout_seconds (cls , session_server : SessionServer ) -> int :
138
140
"""
139
- :type session_server: SessionServer
140
141
141
- :rtype: int
142
+ :param session_server:
142
143
"""
143
144
144
145
if session_server .user_company is not None :
@@ -158,7 +159,6 @@ def ensure_session_active(self) -> bool:
158
159
"""
159
160
Resets the session if it has expired.
160
161
161
- :rtype: bool
162
162
"""
163
163
164
164
if not self .is_session_active ():
@@ -168,11 +168,7 @@ def ensure_session_active(self) -> bool:
168
168
169
169
return False
170
170
171
- def is_session_active (self ):
172
- """
173
- :rtype: bool
174
- """
175
-
171
+ def is_session_active (self ) -> bool :
176
172
if self .session_context is None :
177
173
return False
178
174
@@ -184,62 +180,40 @@ def is_session_active(self):
184
180
185
181
return time_to_expiry > time_to_expiry_minimum
186
182
187
- def reset_session (self ):
183
+ def reset_session (self ) -> None :
188
184
"""
189
185
Closes the current session and opens a new one.
190
186
191
- :rtype: None
192
187
"""
193
188
194
189
self ._drop_session_context ()
195
190
self ._initialize_session ()
196
191
197
- def _drop_session_context (self ):
198
- """
199
- :rtype: None
200
- """
201
-
192
+ def _drop_session_context (self ) -> None :
202
193
self ._session_context = None
203
194
204
- def close_session (self ):
195
+ def close_session (self ) -> None :
205
196
"""
206
197
Closes the current session.
207
198
208
- :rtype: None
209
199
"""
210
200
211
201
self ._delete_session ()
212
202
self ._drop_session_context ()
213
203
214
- def _delete_session (self ):
215
- """
216
- :rtype: None
217
- """
218
-
204
+ def _delete_session (self ) -> None :
219
205
endpoint .Session .delete (self ._SESSION_ID_DUMMY )
220
206
221
207
@property
222
- def environment_type (self ):
223
- """
224
- :rtype: ApiEnvironmentType
225
- """
226
-
208
+ def environment_type (self ) -> ApiEnvironmentType :
227
209
return self ._environment_type
228
210
229
211
@property
230
- def api_key (self ):
231
- """
232
- :rtype: str
233
- """
234
-
212
+ def api_key (self ) -> str :
235
213
return self ._api_key
236
214
237
215
@property
238
- def token (self ):
239
- """
240
- :rtype: str
241
- """
242
-
216
+ def token (self ) -> Optional [str ]:
243
217
if self ._session_context is not None :
244
218
return self .session_context .token
245
219
elif self ._installation_context is not None :
@@ -248,34 +222,21 @@ def token(self):
248
222
return None
249
223
250
224
@property
251
- def installation_context (self ):
252
- """
253
- :rtype: InstallationContext
254
- """
255
-
225
+ def installation_context (self ) -> InstallationContext :
256
226
return self ._installation_context
257
227
258
228
@property
259
- def session_context (self ):
260
- """
261
- :rtype: SessionContext
262
- """
263
-
229
+ def session_context (self ) -> SessionContext :
264
230
return self ._session_context
265
231
266
232
@property
267
- def proxy_url (self ):
268
- """
269
- :rtype: str
270
- """
271
-
233
+ def proxy_url (self ) -> str :
272
234
return self ._proxy_url
273
235
274
- def save (self , path = None ):
236
+ def save (self , path : str = None ) -> None :
275
237
"""
276
- :type path: str
277
238
278
- :rtype: None
239
+ :param path:
279
240
"""
280
241
281
242
if path is None :
@@ -284,21 +245,19 @@ def save(self, path=None):
284
245
with open (path , self ._FILE_MODE_WRITE ) as file_ :
285
246
file_ .write (self .to_json ())
286
247
287
- def to_json (self ):
248
+ def to_json (self ) -> str :
288
249
"""
289
250
Serializes an ApiContext to JSON string.
290
251
291
- :rtype: str
292
252
"""
293
253
294
254
return converter .class_to_json (self )
295
255
296
256
@classmethod
297
- def restore (cls , path = None ):
257
+ def restore (cls , path : str = None ) -> ApiContext :
298
258
"""
299
- :type path: str
300
259
301
- :rtype: ApiContext
260
+ :param path:
302
261
"""
303
262
304
263
if path is None :
@@ -308,18 +267,21 @@ def restore(cls, path=None):
308
267
return cls .from_json (file_ .read ())
309
268
310
269
@classmethod
311
- def from_json (cls , json_str ) :
270
+ def from_json (cls , json_str : str ) -> ApiContext :
312
271
"""
313
272
Creates an ApiContext instance from JSON string.
314
273
315
- :type json_str: str
316
-
317
- :rtype: ApiContext
274
+ :type json_str:
318
275
"""
319
276
320
277
return converter .json_to_class (ApiContext , json_str )
321
278
322
- def __eq__ (self , other ):
279
+ def __eq__ (self , other : ApiContext ) -> bool :
280
+ """
281
+
282
+ :param other:
283
+ """
284
+
323
285
return (self .token == other .token and
324
286
self .api_key == other .api_key and
325
287
self .environment_type == other .environment_type )
0 commit comments