Skip to content

Commit e049134

Browse files
committed
Refactored the http directory to match that of the other SDKs.
1 parent 01e97e7 commit e049134

File tree

16 files changed

+926
-926
lines changed

16 files changed

+926
-926
lines changed

bunq/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from bunq.sdk.http.pagination import Pagination
12
from bunq.sdk.json import converter
23

34

@@ -9,7 +10,7 @@ def initialize_converter():
910
import datetime
1011
import inspect
1112

12-
from bunq.sdk.http import client
13+
from bunq.sdk.http import api_client
1314
from bunq.sdk.context import api_context
1415
from bunq.sdk.model import core
1516
from bunq.sdk.json import adapters
@@ -38,7 +39,7 @@ def initialize_converter():
3839
)
3940
converter.register_adapter(object_.ShareDetail, adapters.ShareDetailAdapter)
4041
converter.register_adapter(datetime.datetime, adapters.DateTimeAdapter)
41-
converter.register_adapter(client.Pagination, adapters.PaginationAdapter)
42+
converter.register_adapter(Pagination, adapters.PaginationAdapter)
4243

4344
def register_anchor_adapter(class_to_regsiter):
4445
if issubclass(class_to_regsiter, core.AnchoredObjectInterface):

bunq/sdk/http/client.py renamed to bunq/sdk/http/api_client.py

Lines changed: 17 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
import requests
55

6-
from bunq.sdk.exception import exception
7-
from bunq.sdk.security import security
86
from bunq.sdk.exception.exception_factory import ExceptionFactory
7+
from bunq.sdk.http.bunq_response_raw import BunqResponseRaw
98
from bunq.sdk.json import converter
9+
from bunq.sdk.security import security
1010

1111

1212
class ApiClient(object):
@@ -50,13 +50,13 @@ class ApiClient(object):
5050

5151
# Default header values
5252
_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'
5757

5858
# Request method names
59-
_METHOD_POST = 'POST'
59+
METHOD_POST = 'POST'
6060
_METHOD_PUT = 'PUT'
6161
_METHOD_GET = 'GET'
6262
_METHOD_DELETE = 'DELETE'
@@ -65,7 +65,7 @@ class ApiClient(object):
6565
_DELIMITER_URL_QUERY = '?'
6666

6767
# Status code for successful execution
68-
_STATUS_CODE_OK = 200
68+
STATUS_CODE_OK = 200
6969

7070
# Fields for fetching errors
7171
_FIELD_ERROR = 'Error'
@@ -90,7 +90,7 @@ def post(self, uri_relative, request_bytes, custom_headers):
9090
"""
9191

9292
return self._request(
93-
self._METHOD_POST,
93+
self.METHOD_POST,
9494
uri_relative,
9595
request_bytes,
9696
{},
@@ -109,11 +109,10 @@ def _request(self, method, uri_relative, request_bytes, params,
109109
:return: BunqResponseRaw
110110
"""
111111

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)
114113
if uri_relative not in self._URIS_NOT_REQUIRING_ACTIVE_SESSION:
115114
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
117116

118117
BunqContext.update_api_context(self._api_context)
119118

@@ -192,10 +191,10 @@ def _get_default_headers(cls):
192191
return {
193192
cls.HEADER_USER_AGENT: cls._USER_AGENT_BUNQ,
194193
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,
199198
}
200199

201200
@staticmethod
@@ -223,7 +222,7 @@ def _assert_response_success(self, response):
223222
:raise ApiException: When the response is not successful.
224223
"""
225224

226-
if response.status_code != self._STATUS_CODE_OK:
225+
if response.status_code != self.STATUS_CODE_OK:
227226
raise ExceptionFactory.create_exception_for_response(
228227
response.status_code,
229228
self._fetch_all_error_message(response),
@@ -336,205 +335,4 @@ def delete(self, uri_relative, custom_headers):
336335
self._BYTES_EMPTY,
337336
{},
338337
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+
)

bunq/sdk/http/bunq_response.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class BunqResponse(object):
2+
"""
3+
:type _value: T
4+
:type _headers: dict[str, str]
5+
:type _pagination: Pagination|None
6+
"""
7+
8+
def __init__(self, value, headers, pagination=None):
9+
"""
10+
:type value: T
11+
:type headers: dict[str, str]
12+
:type pagination Pagination|None
13+
"""
14+
15+
self._value = value
16+
self._headers = headers
17+
self._pagination = pagination
18+
19+
@property
20+
def value(self):
21+
"""
22+
:rtype: T
23+
"""
24+
25+
return self._value
26+
27+
@property
28+
def headers(self):
29+
"""
30+
:rtype: dict[str, str]
31+
"""
32+
33+
return self._headers
34+
35+
@property
36+
def pagination(self):
37+
"""
38+
:rtype: Pagination
39+
"""
40+
41+
return self._pagination
42+
43+
@classmethod
44+
def cast_from_bunq_response(cls, bunq_response):
45+
"""
46+
:type bunq_response: BunqResponse
47+
"""
48+
49+
return cls(
50+
bunq_response.value,
51+
bunq_response.headers,
52+
bunq_response.pagination
53+
)

0 commit comments

Comments
 (0)