-
Notifications
You must be signed in to change notification settings - Fork 7
/
bunq_lib.py
235 lines (183 loc) · 7.25 KB
/
bunq_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import json
from os.path import isfile
import socket
import requests
from bunq.sdk.client import Pagination
from bunq.sdk.context import ApiContext
from bunq.sdk.context import ApiEnvironmentType
from bunq.sdk.context import BunqContext
from bunq.sdk.exception import BunqException
from bunq.sdk.model.generated import endpoint
from bunq.sdk.model.generated.object_ import Pointer, Amount, NotificationFilter
NOTIFICATION_DELIVERY_METHOD_URL = 'URL'
NOTIFICATION_CATEGORY_MUTATION = 'MUTATION'
class BunqLib(object):
_ERROR_COULD_NOT_DETIRMINE_CONF = 'Could not find the bunq configuration' \
' file.'
_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER = "Could not create new sandbox" \
" user."
_BUNQ_CONF_PRODUCTION = 'bunq-production.conf'
_BUNQ_CONF_SANDBOX = 'bunq-sandbox.conf'
_MONETARY_ACCOUNT_STATUS_ACTIVE = 'ACTIVE'
_DEFAULT_COUNT = 10
_POINTER_TYPE_EMAIL = 'EMAIL'
_CURRENCY_EURL = 'EUR'
_DEVICE_DESCRIPTION = "python tinker"
def __init__(self, env):
"""
:type env: ApiEnvironmentType
"""
self.user = None
self.env = env
self.setup_context()
self.setup_current_user()
def setup_context(self):
if isfile(self.determine_bunq_conf_filename()):
pass # Config is already present
elif self.env == ApiEnvironmentType.SANDBOX:
sandbox_user = self.generate_new_sandbox_user()
ApiContext(ApiEnvironmentType.SANDBOX, sandbox_user.api_key,
socket.gethostname()).save(
self.determine_bunq_conf_filename())
else:
raise BunqException(self._ERROR_COULD_NOT_DETIRMINE_CONF)
api_context = ApiContext.restore(self.determine_bunq_conf_filename())
api_context.ensure_session_active()
api_context.save(self.determine_bunq_conf_filename())
BunqContext.load_api_context(api_context)
def determine_bunq_conf_filename(self):
if self.env == ApiEnvironmentType.PRODUCTION:
return self._BUNQ_CONF_PRODUCTION
else:
return self._BUNQ_CONF_SANDBOX
def setup_current_user(self):
user = endpoint.User.get().value.get_referenced_object()
if (isinstance(user, endpoint.UserPerson)
or isinstance(user, endpoint.UserCompany)
or isinstance(user, endpoint.UserLight)
):
self.user = user
def update_context(self):
BunqContext.api_context().save(self.determine_bunq_conf_filename())
def get_current_user(self):
"""
:rtype: UserCompany|UserPerson
"""
return self.user
def get_all_monetary_account_active(self, count=_DEFAULT_COUNT):
"""
:type count: int
:rtype: list[endpoint.MonetaryAccountBank]
"""
pagination = Pagination()
pagination.count = count
all_monetary_account_bank = endpoint.MonetaryAccountBank.list(
pagination.url_params_count_only).value
all_monetary_account_bank_active = []
for monetary_account_bank in all_monetary_account_bank:
if monetary_account_bank.status == \
self._MONETARY_ACCOUNT_STATUS_ACTIVE:
all_monetary_account_bank_active.append(monetary_account_bank)
return all_monetary_account_bank_active
def get_all_payment(self, count=_DEFAULT_COUNT):
"""
:type count: int
:rtype: list[Payment]
"""
pagination = Pagination()
pagination.count = count
return endpoint.Payment.list(
params=pagination.url_params_count_only).value
def get_all_request(self, count=_DEFAULT_COUNT):
"""
:type count: int
:rtype: list[endpoint.RequestInquiry]
"""
pagination = Pagination()
pagination.count = count
return endpoint.RequestInquiry.list(
params=pagination.url_params_count_only).value
def get_all_card(self, count=_DEFAULT_COUNT):
"""
:type count: int
:rtype: list(endpoint.Card)
"""
pagination = Pagination()
pagination.count = count
return endpoint.Card.list(pagination.url_params_count_only).value
def make_payment(self, amount_string, description, recipient):
"""
:type amount_string: str
:type description: str
:type recipient: str
"""
endpoint.Payment.create(
amount=Amount(amount_string, self._CURRENCY_EURL),
counterparty_alias=Pointer(self._POINTER_TYPE_EMAIL, recipient),
description=description
)
def make_request(self, amount_string, description, recipient):
"""
:type amount_string: str
:type description: str
:type recipient: str
"""
endpoint.RequestInquiry.create(
amount_inquired=Amount(amount_string, self._CURRENCY_EURL),
counterparty_alias=Pointer(self._POINTER_TYPE_EMAIL, recipient),
description=description,
allow_bunqme=True
)
def link_card(self, card_id, account_id):
"""
:type card_id: int
:type account_id: int
"""
endpoint.Card.update(card_id=int(card_id),
monetary_account_current_id=int(account_id))
def add_callback_url(self, callback_url):
"""
:type callback_url: str
"""
all_notification_filter_current = \
self.get_current_user().notification_filters
all_notification_filter_updated = []
for notification_filter in all_notification_filter_current:
if notification_filter.notification_target == callback_url:
all_notification_filter_updated.append(notification_filter)
all_notification_filter_updated.append(
NotificationFilter(NOTIFICATION_DELIVERY_METHOD_URL, callback_url,
NOTIFICATION_CATEGORY_MUTATION)
)
self.get_current_user().update(
notification_filters=all_notification_filter_updated)
def update_account(self, name, account_id):
"""
:type name: str
:type account_id: int
"""
endpoint.MonetaryAccountBank.update(monetary_account_bank_id=account_id,
description=name)
def get_all_user_alias(self):
"""
:rtype: list[Pointer]
"""
return self.get_current_user().alias
def generate_new_sandbox_user(self):
"""
:rtype: SandboxUser
"""
url = "https://sandbox.public.api.bunq.com/v1/sandbox-user"
headers = {
'x-bunq-client-request-id': "uniqueness-is-required",
'cache-control': "no-cache",
'x-bunq-geolocation': "0 0 0 0 NL",
'x-bunq-language': "en_US",
'x-bunq-region': "en_US",
}
response = requests.request("POST", url, headers=headers)
if response.status_code is 200:
response_json = json.loads(response.text)
return endpoint.SandboxUser.from_json(
json.dumps(response_json["Response"][0]["ApiKey"]))
raise BunqException(self._ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)