Skip to content

Commit 3898543

Browse files
authored
Merge pull request #11 from bunq/feature/bunq/tinker_python#10_automatic_money_request
Request money on Sandbox if balance zero. (#10)
2 parents c061ce3 + 495b57c commit 3898543

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

tinker/libs/bunq_lib.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from os.path import isfile
44
from os import remove
55
import socket
6+
from time import sleep
67

78
import requests
89
from bunq.sdk.client import Pagination
@@ -14,6 +15,7 @@
1415
from bunq.sdk.model.generated import endpoint
1516
from bunq.sdk.model.generated.object_ import Pointer, Amount, NotificationFilter
1617

18+
1719
NOTIFICATION_DELIVERY_METHOD_URL = 'URL'
1820

1921
NOTIFICATION_CATEGORY_MUTATION = 'MUTATION'
@@ -31,9 +33,16 @@ class BunqLib(object):
3133

3234
_DEFAULT_COUNT = 10
3335
_POINTER_TYPE_EMAIL = 'EMAIL'
34-
_CURRENCY_EURL = 'EUR'
36+
_CURRENCY_EUR = 'EUR'
3537
_DEVICE_DESCRIPTION = "python tinker"
3638

39+
_REQUEST_SPENDING_MONEY_AMOUNT = '500.0'
40+
_REQUEST_SPENDING_MONEY_RECIPIENT = 'sugardaddy@bunq.com'
41+
_REQUEST_SPENDING_MONEY_DESCRIPTION = 'Requesting some spending money.'
42+
_REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS = 1
43+
44+
_ZERO_BALANCE = 0.0
45+
3746
def __init__(self, env):
3847
"""
3948
:type env: ApiEnvironmentType
@@ -43,6 +52,7 @@ def __init__(self, env):
4352
self.env = env
4453
self.setup_context()
4554
self.setup_current_user()
55+
self.__request_spending_money_if_needed()
4656

4757
def setup_context(self, reset_config_if_needed=True):
4858
if isfile(self.determine_bunq_conf_filename()):
@@ -161,7 +171,7 @@ def make_payment(self, amount_string, description, recipient):
161171
"""
162172

163173
endpoint.Payment.create(
164-
amount=Amount(amount_string, self._CURRENCY_EURL),
174+
amount=Amount(amount_string, self._CURRENCY_EUR),
165175
counterparty_alias=Pointer(self._POINTER_TYPE_EMAIL, recipient),
166176
description=description
167177
)
@@ -174,7 +184,7 @@ def make_request(self, amount_string, description, recipient):
174184
"""
175185

176186
endpoint.RequestInquiry.create(
177-
amount_inquired=Amount(amount_string, self._CURRENCY_EURL),
187+
amount_inquired=Amount(amount_string, self._CURRENCY_EUR),
178188
counterparty_alias=Pointer(self._POINTER_TYPE_EMAIL, recipient),
179189
description=description,
180190
allow_bunqme=True
@@ -249,3 +259,17 @@ def generate_new_sandbox_user(self):
249259
json.dumps(response_json["Response"][0]["ApiKey"]))
250260

251261
raise BunqException(self._ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)
262+
263+
def __request_spending_money_if_needed(self):
264+
if self.__should_request_spending_money():
265+
endpoint.RequestInquiry.create(
266+
amount_inquired=Amount(self._REQUEST_SPENDING_MONEY_AMOUNT, self._CURRENCY_EUR),
267+
counterparty_alias=Pointer(self._POINTER_TYPE_EMAIL, self._REQUEST_SPENDING_MONEY_RECIPIENT),
268+
description=self._REQUEST_SPENDING_MONEY_DESCRIPTION,
269+
allow_bunqme=False
270+
)
271+
sleep(self._REQUEST_SPENDING_MONEY_WAIT_TIME_SECONDS)
272+
273+
def __should_request_spending_money(self):
274+
return self.env == ApiEnvironmentType.SANDBOX \
275+
and float(BunqContext.user_context().primary_monetary_account.balance.value) <= self._ZERO_BALANCE

0 commit comments

Comments
 (0)