3
3
4
4
import requests
5
5
6
- from bunq .sdk .exception import exception
7
- from bunq .sdk .security import security
8
6
from bunq .sdk .exception .exception_factory import ExceptionFactory
7
+ from bunq .sdk .http .bunq_response_raw import BunqResponseRaw
9
8
from bunq .sdk .json import converter
9
+ from bunq .sdk .security import security
10
10
11
11
12
12
class ApiClient (object ):
@@ -50,13 +50,13 @@ class ApiClient(object):
50
50
51
51
# Default header values
52
52
_USER_AGENT_BUNQ = 'bunq-sdk-python/1.10.16'
53
- _GEOLOCATION_ZERO = '0 0 0 0 NL'
54
- _LANGUAGE_EN_US = 'en_US'
55
- _REGION_NL_NL = 'nl_NL'
56
- _CACHE_CONTROL_NONE = 'no-cache'
53
+ GEOLOCATION_ZERO = '0 0 0 0 NL'
54
+ LANGUAGE_EN_US = 'en_US'
55
+ REGION_NL_NL = 'nl_NL'
56
+ CACHE_CONTROL_NONE = 'no-cache'
57
57
58
58
# Request method names
59
- _METHOD_POST = 'POST'
59
+ METHOD_POST = 'POST'
60
60
_METHOD_PUT = 'PUT'
61
61
_METHOD_GET = 'GET'
62
62
_METHOD_DELETE = 'DELETE'
@@ -65,7 +65,7 @@ class ApiClient(object):
65
65
_DELIMITER_URL_QUERY = '?'
66
66
67
67
# Status code for successful execution
68
- _STATUS_CODE_OK = 200
68
+ STATUS_CODE_OK = 200
69
69
70
70
# Fields for fetching errors
71
71
_FIELD_ERROR = 'Error'
@@ -90,7 +90,7 @@ def post(self, uri_relative, request_bytes, custom_headers):
90
90
"""
91
91
92
92
return self ._request (
93
- self ._METHOD_POST ,
93
+ self .METHOD_POST ,
94
94
uri_relative ,
95
95
request_bytes ,
96
96
{},
@@ -109,11 +109,10 @@ def _request(self, method, uri_relative, request_bytes, params,
109
109
:return: BunqResponseRaw
110
110
"""
111
111
112
- uri_relative_with_params = self ._append_params_to_uri (uri_relative ,
113
- params )
112
+ uri_relative_with_params = self ._append_params_to_uri (uri_relative , params )
114
113
if uri_relative not in self ._URIS_NOT_REQUIRING_ACTIVE_SESSION :
115
114
if self ._api_context .ensure_session_active ():
116
- from bunq .sdk .context .api_context import BunqContext
115
+ from bunq .sdk .context .bunq_context import BunqContext
117
116
118
117
BunqContext .update_api_context (self ._api_context )
119
118
@@ -192,10 +191,10 @@ def _get_default_headers(cls):
192
191
return {
193
192
cls .HEADER_USER_AGENT : cls ._USER_AGENT_BUNQ ,
194
193
cls .HEADER_REQUEST_ID : cls ._generate_random_request_id (),
195
- cls .HEADER_GEOLOCATION : cls ._GEOLOCATION_ZERO ,
196
- cls .HEADER_LANGUAGE : cls ._LANGUAGE_EN_US ,
197
- cls .HEADER_REGION : cls ._REGION_NL_NL ,
198
- cls .HEADER_CACHE_CONTROL : cls ._CACHE_CONTROL_NONE ,
194
+ cls .HEADER_GEOLOCATION : cls .GEOLOCATION_ZERO ,
195
+ cls .HEADER_LANGUAGE : cls .LANGUAGE_EN_US ,
196
+ cls .HEADER_REGION : cls .REGION_NL_NL ,
197
+ cls .HEADER_CACHE_CONTROL : cls .CACHE_CONTROL_NONE ,
199
198
}
200
199
201
200
@staticmethod
@@ -223,7 +222,7 @@ def _assert_response_success(self, response):
223
222
:raise ApiException: When the response is not successful.
224
223
"""
225
224
226
- if response .status_code != self ._STATUS_CODE_OK :
225
+ if response .status_code != self .STATUS_CODE_OK :
227
226
raise ExceptionFactory .create_exception_for_response (
228
227
response .status_code ,
229
228
self ._fetch_all_error_message (response ),
@@ -336,205 +335,4 @@ def delete(self, uri_relative, custom_headers):
336
335
self ._BYTES_EMPTY ,
337
336
{},
338
337
custom_headers
339
- )
340
-
341
-
342
- class BunqResponseRaw (object ):
343
- """
344
- :type _body_bytes: bytes
345
- :type _headers: dict[str, str]
346
- """
347
-
348
- def __init__ (self , body_bytes , headers ):
349
- """
350
- :type body_bytes: bytes
351
- :type headers: dict[str, str]
352
- """
353
-
354
- self ._body_bytes = body_bytes
355
- self ._headers = headers
356
-
357
- @property
358
- def body_bytes (self ):
359
- """
360
- :rtype: bytes
361
- """
362
-
363
- return self ._body_bytes
364
-
365
- @property
366
- def headers (self ):
367
- """
368
- :rtype: dict[str, str]
369
- """
370
-
371
- return self ._headers
372
-
373
-
374
- class BunqResponse (object ):
375
- """
376
- :type _value: T
377
- :type _headers: dict[str, str]
378
- :type _pagination: Pagination|None
379
- """
380
-
381
- def __init__ (self , value , headers , pagination = None ):
382
- """
383
- :type value: T
384
- :type headers: dict[str, str]
385
- :type pagination Pagination|None
386
- """
387
-
388
- self ._value = value
389
- self ._headers = headers
390
- self ._pagination = pagination
391
-
392
- @property
393
- def value (self ):
394
- """
395
- :rtype: T
396
- """
397
-
398
- return self ._value
399
-
400
- @property
401
- def headers (self ):
402
- """
403
- :rtype: dict[str, str]
404
- """
405
-
406
- return self ._headers
407
-
408
- @property
409
- def pagination (self ):
410
- """
411
- :rtype: Pagination
412
- """
413
-
414
- return self ._pagination
415
-
416
- @classmethod
417
- def cast_from_bunq_response (cls , bunq_response ):
418
- """
419
- :type bunq_response: BunqResponse
420
- """
421
-
422
- return cls (
423
- bunq_response .value ,
424
- bunq_response .headers ,
425
- bunq_response .pagination
426
- )
427
-
428
-
429
- class Pagination (object ):
430
- """
431
- :type older_id: int|None
432
- :type newer_id: int|None
433
- :type future_id: int|None
434
- :type count: int|None
435
- """
436
-
437
- # Error constants
438
- _ERROR_NO_PREVIOUS_PAGE = 'Could not generate previous page URL params: ' \
439
- 'there is no previous page.'
440
- _ERROR_NO_NEXT_PAGE = 'Could not generate next page URL params: ' \
441
- 'there is no next page.'
442
-
443
- # URL Param constants
444
- PARAM_OLDER_ID = 'older_id'
445
- PARAM_NEWER_ID = 'newer_id'
446
- PARAM_COUNT = 'count'
447
-
448
- def __init__ (self ):
449
- self .older_id = None
450
- self .newer_id = None
451
- self .future_id = None
452
- self .count = None
453
-
454
- @property
455
- def url_params_previous_page (self ):
456
- """
457
- :rtype: dict[str, str]
458
- """
459
-
460
- self .assert_has_previous_page ()
461
-
462
- params = {self .PARAM_OLDER_ID : str (self .older_id )}
463
- self ._add_count_to_params_if_needed (params )
464
-
465
- return params
466
-
467
- def assert_has_previous_page (self ):
468
- """
469
- :raise: exception.BunqException
470
- """
471
-
472
- if not self .has_previous_page ():
473
- raise exception .BunqException (self ._ERROR_NO_PREVIOUS_PAGE )
474
-
475
- def has_previous_page (self ):
476
- """
477
- :rtype: bool
478
- """
479
-
480
- return self .older_id is not None
481
-
482
- @property
483
- def url_params_count_only (self ):
484
- """
485
- :rtype: dict[str, str]
486
- """
487
-
488
- params = {}
489
- self ._add_count_to_params_if_needed (params )
490
-
491
- return params
492
-
493
- def _add_count_to_params_if_needed (self , params ):
494
- """
495
- :type params: dict[str, str]
496
-
497
- :rtype: None
498
- """
499
-
500
- if self .count is not None :
501
- params [self .PARAM_COUNT ] = str (self .count )
502
-
503
- def has_next_page_assured (self ):
504
- """
505
- :rtype: bool
506
- """
507
-
508
- return self .newer_id is not None
509
-
510
- @property
511
- def url_params_next_page (self ):
512
- """
513
- :rtype: dict[str, str]
514
- """
515
-
516
- self .assert_has_next_page ()
517
-
518
- params = {self .PARAM_NEWER_ID : str (self ._next_id )}
519
- self ._add_count_to_params_if_needed (params )
520
-
521
- return params
522
-
523
- def assert_has_next_page (self ):
524
- """
525
- :raise: exception.BunqException
526
- """
527
-
528
- if self ._next_id is None :
529
- raise exception .BunqException (self ._ERROR_NO_NEXT_PAGE )
530
-
531
- @property
532
- def _next_id (self ):
533
- """
534
- :rtype: int
535
- """
536
-
537
- if self .has_next_page_assured ():
538
- return self .newer_id
539
-
540
- return self .future_id
338
+ )
0 commit comments