Skip to content

Commit 0436bd9

Browse files
author
Kevin Hellemun
committed
Updated core models. (#75)
1 parent d2b3daa commit 0436bd9

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

bunq/sdk/model/core.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from bunq.sdk import client
22
from bunq.sdk.json import converter
3+
from bunq.sdk.exception import BunqException
4+
from bunq.sdk import context
35

46

57
class AnchoredObjectInterface:
@@ -135,6 +137,35 @@ def _from_json_list(cls, response_raw, wrapper=None):
135137
return client.BunqResponse(array_deserialized, response_raw.headers,
136138
pagination)
137139

140+
@classmethod
141+
def _get_api_context(cls):
142+
"""
143+
:rtype: context.ApiContext
144+
"""
145+
146+
return context.BunqContext.api_context()
147+
148+
@classmethod
149+
def _determine_user_id(cls):
150+
"""
151+
:rtype: int
152+
"""
153+
154+
return context.BunqContext.user_context().user_id
155+
156+
@classmethod
157+
def _determine_monetary_account_id(cls, monetary_account_id=None):
158+
"""
159+
:type monetary_account_id: int
160+
161+
:rtype: int
162+
"""
163+
164+
if monetary_account_id is None:
165+
return context.BunqContext.user_context().primary_monetary_account.id_
166+
167+
return monetary_account_id
168+
138169

139170
class Id(BunqModel):
140171
"""
@@ -366,6 +397,9 @@ class SessionServer(BunqModel):
366397
# Field constants
367398
FIELD_SECRET = "secret"
368399

400+
# Error constants
401+
_ERROR_ALL_FIELD_IS_NULL = 'All fields are null'
402+
369403
def __init__(self):
370404
self._id_ = None
371405
self._token = None
@@ -442,3 +476,16 @@ def is_all_field_none(self):
442476
return False
443477

444478
return True
479+
480+
def get_referenced_object(self):
481+
"""
482+
:rtype: BunqModel
483+
"""
484+
485+
if self._user_person is not None:
486+
return self._user_person
487+
488+
if self._user_company is not None:
489+
return self._user_company
490+
491+
raise BunqException(self._ERROR_ALL_FIELD_IS_NULL)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from bunq.sdk.model.generated.endpoint import DeviceServer
2+
from bunq.sdk.model.generated.endpoint import BunqResponseInt
3+
from bunq.sdk import client
4+
from bunq.sdk.json import converter
5+
from bunq.sdk.exception import BunqException
6+
7+
8+
class DeviceServerInternal(DeviceServer):
9+
_ERROR_API_CONTEXT_IS_NULL = 'ApiContext should not be None,' \
10+
' use the generated class instead.'
11+
12+
@classmethod
13+
def create(cls, description, secret, permitted_ips=None,
14+
custom_headers=None, api_context=None):
15+
"""
16+
Create a new DeviceServer providing the installation token in the header
17+
and signing the request with the private part of the key you used to
18+
create the installation. The API Key that you are using will be bound to
19+
the IP address of the DeviceServer which you have
20+
created.<br/><br/>Using a Wildcard API Key gives you the freedom to make
21+
API calls even if the IP address has changed after the POST
22+
device-server.<br/><br/>Find out more at this link <a
23+
href="https://bunq.com/en/apikey-dynamic-ip"
24+
target="_blank">https://bunq.com/en/apikey-dynamic-ip</a>.
25+
26+
:param description: The description of the DeviceServer. This is only
27+
for your own reference when reading the DeviceServer again.
28+
:type description: str
29+
:param secret: The API key. You can request an API key in the bunq app.
30+
:type secret: str
31+
:param permitted_ips: An array of IPs (v4 or v6) this DeviceServer will
32+
be able to do calls from. These will be linked to the API key.
33+
:type permitted_ips: list[str]
34+
:type custom_headers: dict[str, str]|None
35+
:type api_context: context.ApiContext
36+
37+
:rtype: BunqResponseInt
38+
"""
39+
40+
if api_context is None:
41+
raise BunqException(cls._ERROR_API_CONTEXT_IS_NULL)
42+
43+
if custom_headers is None:
44+
custom_headers = {}
45+
46+
request_map = {
47+
cls.FIELD_DESCRIPTION: description,
48+
cls.FIELD_SECRET: secret,
49+
cls.FIELD_PERMITTED_IPS: permitted_ips
50+
}
51+
52+
api_client = client.ApiClient(api_context)
53+
request_bytes = converter.class_to_json(request_map).encode()
54+
endpoint_url = cls._ENDPOINT_URL_CREATE
55+
response_raw = api_client.post(endpoint_url, request_bytes,
56+
custom_headers)
57+
58+
return BunqResponseInt.cast_from_bunq_response(
59+
cls._process_for_id(response_raw)
60+
)

0 commit comments

Comments
 (0)