Skip to content

Commit 356d5ca

Browse files
authored
Merge pull request #10 from bunq/9-bunq-response
#9 Introduce BunqResponse
2 parents 5d7bced + 2c945ae commit 356d5ca

23 files changed

+753
-573
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# bunq Python SDK
2-
Version 0.9.0 **BETA**
32

43
## Introduction
54
Hi developers!
@@ -183,4 +182,4 @@ Please do not forget to set the `_API_KEY` constant in
183182
## Running Tests
184183

185184
Information regarding the test cases can be found in the [README.md](./tests/README.md)
186-
located in [test](/tests)
185+
located in [test](/tests)

bunq/sdk/client.py

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ApiClient(object):
3232
HEADER_AUTHENTICATION = 'X-Bunq-Client-Authentication'
3333

3434
# Default header values
35-
_USER_AGENT_BUNQ = 'bunq-sdk-python/0.9'
35+
_USER_AGENT_BUNQ = 'bunq-sdk-python/0.9.1'
3636
_GEOLOCATION_ZERO = '0 0 0 0 NL'
3737
_LANGUAGE_EN_US = 'en_US'
3838
_REGION_NL_NL = 'nl_NL'
@@ -66,7 +66,7 @@ def post(self, uri_relative, request_bytes, custom_headers):
6666
:type request_bytes: bytes
6767
:type custom_headers: dict[str, str]
6868
69-
:return: requests.Response
69+
:return: BunqResponseRaw
7070
"""
7171

7272
return self._request(
@@ -83,7 +83,7 @@ def _request(self, method, uri_relative, request_bytes, custom_headers):
8383
:type request_bytes: bytes
8484
:type custom_headers: dict[str, str]
8585
86-
:return: requests.Response
86+
:return: BunqResponseRaw
8787
"""
8888

8989
self._api_context.ensure_session_active()
@@ -111,7 +111,7 @@ def _request(self, method, uri_relative, request_bytes, custom_headers):
111111
response.headers
112112
)
113113

114-
return response
114+
return self._create_bunq_response_raw(response)
115115

116116
def _get_all_headers(self, method, endpoint, request_bytes, custom_headers):
117117
"""
@@ -184,6 +184,16 @@ def _assert_response_success(self, response):
184184
self._fetch_error_messages(response)
185185
)
186186

187+
@classmethod
188+
def _create_bunq_response_raw(cls, response):
189+
"""
190+
:type response: requests.Response
191+
192+
:rtype: BunqResponseRaw
193+
"""
194+
195+
return BunqResponseRaw(response.content, response.headers)
196+
187197
def _fetch_error_messages(self, response):
188198
"""
189199
:type response: requests.Response
@@ -221,7 +231,7 @@ def put(self, uri_relative, request_bytes, custom_headers):
221231
:type request_bytes: bytes
222232
:type custom_headers: dict[str, str]
223233
224-
:rtype: requests.Response
234+
:rtype: BunqResponseRaw
225235
"""
226236

227237
return self._request(
@@ -236,7 +246,7 @@ def get(self, uri_relative, custom_headers):
236246
:type uri_relative: str
237247
:type custom_headers: dict[str, str]
238248
239-
:rtype: requests.Response
249+
:rtype: BunqResponseRaw
240250
"""
241251

242252
return self._request(
@@ -251,7 +261,7 @@ def delete(self, uri_relative, custom_headers):
251261
:type uri_relative: str
252262
:type custom_headers: dict[str, str]
253263
254-
:rtype: requests.Response
264+
:rtype: BunqResponseRaw
255265
"""
256266

257267
return self._request(
@@ -260,3 +270,67 @@ def delete(self, uri_relative, custom_headers):
260270
self._BYTES_EMPTY,
261271
custom_headers
262272
)
273+
274+
275+
class BunqResponseRaw(object):
276+
"""
277+
:type _body_bytes: bytes
278+
:type _headers: dict[str, str]
279+
"""
280+
281+
def __init__(self, body_bytes, headers):
282+
"""
283+
:type body_bytes: bytes
284+
:type headers: dict[str, str]
285+
"""
286+
287+
self._body_bytes = body_bytes
288+
self._headers = headers
289+
290+
@property
291+
def body_bytes(self):
292+
"""
293+
:rtype: bytes
294+
"""
295+
296+
return self._body_bytes
297+
298+
@property
299+
def headers(self):
300+
"""
301+
:rtype: dict[str, str]
302+
"""
303+
304+
return self._headers
305+
306+
307+
class BunqResponse(object):
308+
"""
309+
:type _value: T
310+
:type _headers: dict[str, str]
311+
"""
312+
313+
def __init__(self, value, headers):
314+
"""
315+
:type value: T
316+
:type headers: dict[str, str]
317+
"""
318+
319+
self._value = value
320+
self._headers = headers
321+
322+
@property
323+
def value(self):
324+
"""
325+
:rtype: T
326+
"""
327+
328+
return self._value
329+
330+
@property
331+
def headers(self):
332+
"""
333+
:rtype: dict[str, str]
334+
"""
335+
336+
return self._headers

bunq/sdk/context.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _initialize_installation(self):
9595
installation = model.Installation.create(
9696
self,
9797
security.public_key_to_string(private_key_client.publickey())
98-
)
98+
).value
9999
token = installation.token.token
100100
public_key_server_string = \
101101
installation.server_public_key.server_public_key
@@ -116,14 +116,21 @@ def _register_device(self, device_description,
116116
:rtype: None
117117
"""
118118

119-
model.DeviceServer.create(self, device_description, permitted_ips)
119+
generated.DeviceServer.create(
120+
self,
121+
{
122+
generated.DeviceServer.FIELD_DESCRIPTION: device_description,
123+
generated.DeviceServer.FIELD_SECRET: self.api_key,
124+
generated.DeviceServer.FIELD_PERMITTED_IPS: permitted_ips,
125+
}
126+
)
120127

121128
def _initialize_session(self):
122129
"""
123130
:rtype: None
124131
"""
125132

126-
session_server = model.SessionServer.create(self)
133+
session_server = model.SessionServer.create(self).value
127134
token = session_server.token.token
128135
expiry_time = self._get_expiry_timestamp(session_server)
129136

0 commit comments

Comments
 (0)