Skip to content

Commit

Permalink
#9 move to BunqResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
dnl-blkv committed Aug 8, 2017
1 parent 5d7bced commit 2b84e2e
Show file tree
Hide file tree
Showing 14 changed files with 671 additions and 576 deletions.
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)
56 changes: 49 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,35 @@ 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
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

0 comments on commit 2b84e2e

Please sign in to comment.