Skip to content

Commit d752cf4

Browse files
authored
v0.1.3 (#13)
* Added missing endpoints and refactored all tests * Updated the ApiClient so that you can you can create an ApiClient with any variables using a kwargs * Updated the Readme and changed the __init__ function in ApiClientPersisting * Changed the folder structure in order to adhere to the PyPi standards (tests needs to be a top-level folder) * Fixed the Avatar endpoint (added a verify parameter to making get-requests) Fixed the ApiClientPersistent instantiation * Fixed the get request for the case that a byte response is returned instead of JSON * Changed the constructor of ApiClient and ApiClientPersisting based on OGKevin’s suggestion.
1 parent 96c25d0 commit d752cf4

8 files changed

Lines changed: 56 additions & 43 deletions

File tree

apiwrapper/clients/api_client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@ class ApiClient:
2424
_uri_production = "https://api.bunq.com/v%d" % __version_api
2525
_uri_sandbox = "https://sandbox.public.api.bunq.com/v%d" % __version_api
2626

27-
__variables = ['installation_id', 'installation_token', 'privkey',
27+
__variables = ['installation_id', 'installation_token', 'api_key',
2828
'server_token', 'server_pubkey', 'session_token']
2929

30-
def __init__(self, api_key, use_sandbox=True, **kwargs):
31-
self.api_key = api_key
30+
def __init__(self, privkey, use_sandbox=True, **kwargs):
31+
self.privkey = privkey
3232
self._uri = self._uri_sandbox if use_sandbox else self._uri_production
3333
self._handle_kwargs(kwargs)
3434

3535
self.__endpoint_controller = EndpointController(self)
3636

3737
def _handle_kwargs(self, kwargs):
3838
for k in self.__variables:
39-
setattr(self, k, kwargs.get(k))
39+
if getattr(self, k, None) is None:
40+
setattr(self, k, kwargs.get(k))
4041

41-
def get(self, endpoint):
42+
def get(self, endpoint, verify=True):
4243
result = self.request('GET', endpoint)
4344

44-
if self.verify(result):
45-
return result
45+
if verify and not self.verify(result):
46+
return None
4647

47-
return None
48+
return result
4849

4950
def post(self, endpoint, payload):
5051
return self.request('POST', endpoint, payload)

apiwrapper/clients/api_client_persisting.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class ApiClientPersisting(ApiClient):
1111
in config/configcontroller.
1212
"""
1313

14-
def __init__(self, api_key=None, use_sandbox=True, **kwargs):
14+
def __init__(self, privkey=None, use_sandbox=True, **kwargs):
1515
self.config = Controller()
16-
self.api_key = api_key if api_key is not None else self.config.get('api_key')
16+
self.privkey = privkey if privkey is not None else self.config.get('key_private')
1717

18-
super.__init__(self.api_key, use_sandbox, kwargs)
18+
super().__init__(self.privkey, use_sandbox=use_sandbox, **kwargs)
1919

2020
@property
2121
def api_key(self):
@@ -45,6 +45,10 @@ def installation_token(self, value):
4545
def privkey(self):
4646
return self.config.get('key_private')
4747

48+
@privkey.setter
49+
def privkey(self, value):
50+
self.config.set('key_private', value)
51+
4852
@property
4953
def server_pubkey(self):
5054
return self.config.get('server_pubkey')
@@ -68,5 +72,3 @@ def session_token(self):
6872
@session_token.setter
6973
def session_token(self, value):
7074
self.config.set('session_token', value)
71-
72-

apiwrapper/config/controller.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ def __init__(self, filepath=None):
2525

2626
def get(self, name, section=__section_default):
2727
"""Returns a value with a given name from the configuration file."""
28-
try:
28+
29+
if self.parser.has_option(section, name):
2930
return self.parser[section][name]
30-
except KeyError:
31+
else:
3132
return None
3233

3334
def set(self, name, val, section=__section_default):
3435
"""Sets an entry in the default section of the config file to a
3536
specified value
37+
38+
If the entry should be set to None, this function will delete it
39+
from the config file.
40+
3641
:param section: [Optional] The section in which an entry
3742
should be changed
3843
:param name: The entry whose value should be changed
@@ -42,7 +47,12 @@ def set(self, name, val, section=__section_default):
4247
if section not in self.parser.sections():
4348
self.parser.add_section(section)
4449

45-
self.parser.set(section, name, str(val))
50+
if val is None:
51+
if self.parser.has_option(section, name):
52+
self.parser.remove_option(section, name)
53+
else:
54+
self.parser.set(section, name, str(val))
55+
4656
self.save()
4757

4858
def save(self):

apiwrapper/endpoints/attachment_public.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def _get_base_endpoint(cls, attachment_id):
1313
def get_public_attachment_by_id(self, attachment_id):
1414
endpoint = self._get_base_endpoint(attachment_id)
1515

16-
return self._make_get_request(endpoint)
16+
return self._make_get_request(endpoint, verify=False)
1717

1818
def get_content_of_public_attachment(self, attachment_id):
1919
endpoint = self._get_base_endpoint(attachment_id)
2020
endpoint += "/%s" % self.__endpoint_attachment_public_content
2121

22-
return self._make_get_request(endpoint)
22+
return self._make_get_request(endpoint, verify=False)

apiwrapper/endpoints/avatar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ def _get_base_endpoint(cls, avatar_id):
1212
def get_avatar_by_id(self, avatar_id):
1313
endpoint = self._get_base_endpoint(avatar_id)
1414

15-
return self._make_get_request(endpoint)
15+
return self._make_get_request(endpoint, verify=False)

apiwrapper/endpoints/endpoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Endpoint:
88
def __init__(self, api_client):
99
self._api_client = api_client
1010

11-
def _make_get_request(self, endpoint):
12-
return self._api_client.get(endpoint)
11+
def _make_get_request(self, endpoint, verify=True):
12+
return self._api_client.get(endpoint, verify)
1313

1414
def _make_post_request(self, endpoint, payload):
1515
return self._api_client.post(endpoint, payload)

apiwrapper/utils/setup.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ def setup_w_new_key_pair(self, save_to_config=True):
3434

3535
def setup_w_existing_private_key(self, private_key, save_to_config=True):
3636
if save_to_config:
37-
self.api_client = ApiClientPersisting(self.api_key)
37+
self.api_client = ApiClientPersisting(private_key,
38+
api_key=self.api_key)
3839
else:
39-
self.api_client = ApiClient(private_key, self.api_key)
40+
self.api_client = ApiClient(private_key, api_key=self.api_key)
4041

4142
self.endpoints = endpoints.Controller(self.api_client)
4243

@@ -59,7 +60,7 @@ def convert_private_key_to_pem(private_key):
5960

6061
def create_new_key_pair(self, save_to_config=True):
6162
"""Creates a new public/private key pair and saves them to the config file
62-
63+
6364
:return: Prints out a success message
6465
"""
6566
private_key = rsa.generate_private_key(
@@ -93,18 +94,18 @@ def create_new_key_pair(self, save_to_config=True):
9394
def register_key_pair(self):
9495
"""Registers a public/private key pair with the Bunq API
9596
Ref: https://doc.bunq.com/api/1/call/installation/method/post
96-
97+
9798
Saves the installation (user) token and server public key to config
98-
99+
99100
KEY_PRIVATE needs to be set in the config for this method to run
100-
101-
:return: Prints out either a success message or
101+
102+
:return: Prints out either a success message or
102103
the Error message of the API
103104
"""
104105

105-
r = self.endpoints.installation.create_installation()
106+
r = self.endpoints.installation.create_installation().json()
106107
try:
107-
res = r.json()['Response']
108+
res = r['Response']
108109

109110
id_entry = [x for x in res if list(x)[0] == 'Id'][0]
110111
token_entry = [x for x in res if list(x)[0] == 'Token'][0]
@@ -132,18 +133,18 @@ def register_key_pair(self):
132133
return False
133134

134135
def create_new_device_server(self):
135-
"""Creates a new device server at the Bunq API
136+
"""Creates a new device server at the Bunq API
136137
Ref: https://doc.bunq.com/api/1/call/device-server/method/post
137-
138+
138139
API_KEY needs to be set in the config for this method to run
139-
140+
140141
:return: Prints out either a Success or the Error message of the API
141142
"""
142143
r = self.endpoints.device_server.create_new_device_server(
143-
description="New Device")
144+
description="New Device").json()
144145

145146
try:
146-
res = r.json()['Response']
147+
res = r['Response']
147148

148149
print('New device server was created successfully.')
149150
return True
@@ -154,19 +155,19 @@ def create_new_device_server(self):
154155
def create_new_session(self):
155156
"""Creates a new session at the Bunq API
156157
Ref: https://doc.bunq.com/api/1/call/session-server/method/post
157-
158+
158159
Saves the session token to config
159-
160+
160161
API_KEY needs to be set in the config for this method to run
161-
162-
:return: Prints out either a success message or the Error
162+
163+
:return: Prints out either a success message or the Error
163164
message of the API
164165
"""
165166

166-
r = self.endpoints.session_server.create_new_session_server()
167+
r = self.endpoints.session_server.create_new_session_server().json()
167168

168169
try:
169-
res = r.json()['Response']
170+
res = r['Response']
170171

171172
res = [x for x in res if list(x)[0] == 'Token'][0]
172173
session_token = res['Token']['token']

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
def read(fname):
1010
return open(os.path.join(os.path.dirname(__file__), fname)).read()
1111

12-
1312
setup(
1413
name="bunq-api-sdk",
15-
version="0.1.0",
14+
version="0.1.2",
1615
author="Peter Ullrich",
1716
author_email="peter.j.ullrich@gmail.com",
1817
description=("A SDK for the Bunq API through which all HTTP "

0 commit comments

Comments
 (0)