Skip to content

Commit 2bef3e6

Browse files
author
Kevin Hellemun
committed
Added util with automatic install. (#78)
1 parent 414a72a commit 2bef3e6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

bunq/sdk/util.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import json
2+
import socket
3+
4+
import requests
5+
6+
from bunq.sdk.context import ApiContext, ApiEnvironmentType
7+
from bunq.sdk.exception import BunqException
8+
from bunq.sdk.model.generated import endpoint
9+
10+
_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER = "Could not create new sandbox" \
11+
" user."
12+
13+
14+
def automatic_sandbox_install(file_path=None):
15+
sandbox_user = __generate_new_sandbox_user()
16+
ApiContext(
17+
ApiEnvironmentType.SANDBOX,
18+
sandbox_user.api_key,
19+
socket.gethostname()
20+
).save(file_path)
21+
22+
23+
def __generate_new_sandbox_user():
24+
"""
25+
:rtype: endpoint.SandboxUser
26+
"""
27+
28+
url = "https://sandbox.public.api.bunq.com/v1/sandbox-user"
29+
30+
headers = {
31+
'x-bunq-client-request-id': "uniqueness-is-required",
32+
'cache-control': "no-cache",
33+
'x-bunq-geolocation': "0 0 0 0 NL",
34+
'x-bunq-language': "en_US",
35+
'x-bunq-region': "en_US",
36+
}
37+
38+
response = requests.request("POST", url, headers=headers)
39+
40+
if response.status_code is 200:
41+
response_json = json.loads(response.text)
42+
return endpoint.SandboxUser.from_json(
43+
json.dumps(response_json["Response"][0]["ApiKey"]))
44+
45+
raise BunqException(_ERROR_COULD_NOT_CREATE_NEW_SANDBOX_USER)

0 commit comments

Comments
 (0)