Skip to content

#9 Introduce BunqResponse #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# bunq Python SDK
Version 0.9.0 **BETA**

## Introduction
Hi developers!
Expand Down Expand Up @@ -183,4 +182,4 @@ Please do not forget to set the `_API_KEY` constant in
## Running Tests

Information regarding the test cases can be found in the [README.md](./tests/README.md)
located in [test](/tests)
located in [test](/tests)
88 changes: 81 additions & 7 deletions bunq/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ApiClient(object):
HEADER_AUTHENTICATION = 'X-Bunq-Client-Authentication'

# Default header values
_USER_AGENT_BUNQ = 'bunq-sdk-python/0.9'
_USER_AGENT_BUNQ = 'bunq-sdk-python/0.9.1'
_GEOLOCATION_ZERO = '0 0 0 0 NL'
_LANGUAGE_EN_US = 'en_US'
_REGION_NL_NL = 'nl_NL'
Expand Down Expand Up @@ -66,7 +66,7 @@ def post(self, uri_relative, request_bytes, custom_headers):
:type request_bytes: bytes
:type custom_headers: dict[str, str]

:return: requests.Response
:return: BunqResponseRaw
"""

return self._request(
Expand All @@ -83,7 +83,7 @@ def _request(self, method, uri_relative, request_bytes, custom_headers):
:type request_bytes: bytes
:type custom_headers: dict[str, str]

:return: requests.Response
:return: BunqResponseRaw
"""

self._api_context.ensure_session_active()
Expand Down Expand Up @@ -111,7 +111,7 @@ def _request(self, method, uri_relative, request_bytes, custom_headers):
response.headers
)

return response
return self._create_bunq_response_raw(response)

def _get_all_headers(self, method, endpoint, request_bytes, custom_headers):
"""
Expand Down Expand Up @@ -184,6 +184,16 @@ def _assert_response_success(self, response):
self._fetch_error_messages(response)
)

@classmethod
def _create_bunq_response_raw(cls, response):
"""
:type response: requests.Response

:rtype: BunqResponseRaw
"""

return BunqResponseRaw(response.content, response.headers)

def _fetch_error_messages(self, response):
"""
:type response: requests.Response
Expand Down Expand Up @@ -221,7 +231,7 @@ def put(self, uri_relative, request_bytes, custom_headers):
:type request_bytes: bytes
:type custom_headers: dict[str, str]

:rtype: requests.Response
:rtype: BunqResponseRaw
"""

return self._request(
Expand All @@ -236,7 +246,7 @@ def get(self, uri_relative, custom_headers):
:type uri_relative: str
:type custom_headers: dict[str, str]

:rtype: requests.Response
:rtype: BunqResponseRaw
"""

return self._request(
Expand All @@ -251,7 +261,7 @@ def delete(self, uri_relative, custom_headers):
:type uri_relative: str
:type custom_headers: dict[str, str]

:rtype: requests.Response
:rtype: BunqResponseRaw
"""

return self._request(
Expand All @@ -260,3 +270,67 @@ def delete(self, uri_relative, custom_headers):
self._BYTES_EMPTY,
custom_headers
)


class BunqResponseRaw(object):
"""
:type _body_bytes: bytes
:type _headers: dict[str, str]
"""

def __init__(self, body_bytes, headers):
"""
:type body_bytes: bytes
:type headers: dict[str, str]
"""

self._body_bytes = body_bytes
self._headers = headers

@property
def body_bytes(self):
"""
:rtype: bytes
"""

return self._body_bytes

@property
def headers(self):
"""
:rtype: dict[str, str]
"""

return self._headers


class BunqResponse(object):
"""
:type _value: T
:type _headers: dict[str, str]
"""

def __init__(self, value, headers):
"""
:type value: T
:type headers: dict[str, str]
"""

self._value = value
self._headers = headers

@property
def value(self):
"""
:rtype: T
"""

return self._value

@property
def headers(self):
"""
:rtype: dict[str, str]
"""

return self._headers
13 changes: 10 additions & 3 deletions bunq/sdk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _initialize_installation(self):
installation = model.Installation.create(
self,
security.public_key_to_string(private_key_client.publickey())
)
).value
token = installation.token.token
public_key_server_string = \
installation.server_public_key.server_public_key
Expand All @@ -116,14 +116,21 @@ def _register_device(self, device_description,
:rtype: None
"""

model.DeviceServer.create(self, device_description, permitted_ips)
generated.DeviceServer.create(
self,
{
generated.DeviceServer.FIELD_DESCRIPTION: device_description,
generated.DeviceServer.FIELD_SECRET: self.api_key,
generated.DeviceServer.FIELD_PERMITTED_IPS: permitted_ips,
}
)

def _initialize_session(self):
"""
:rtype: None
"""

session_server = model.SessionServer.create(self)
session_server = model.SessionServer.create(self).value
token = session_server.token.token
expiry_time = self._get_expiry_timestamp(session_server)

Expand Down
Loading