Skip to content

Commit a57f0fd

Browse files
committed
Fix issue with headers not updating and refactor nodes wrapper
1 parent dbebd2e commit a57f0fd

11 files changed

Lines changed: 106 additions & 69 deletions

File tree

synapse_pay_rest/api/Nodes.py

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
11
from .helper_functions import *
22

3-
NODES_PATH = '/users/{0}/nodes/{1}'
4-
53

64
class Nodes():
75
def __init__(self, client):
86
self.client = client
97

10-
def create_node_path(self, node_id=None):
8+
def create_node_path(self, user_id, node_id=None):
9+
path = '/users/{0}/nodes'.format(user_id)
1110
if node_id:
12-
return NODES_PATH.format(self.client.user_id, node_id)
11+
return path + '/' + node_id
1312
else:
14-
return NODES_PATH.replace('/{1}', '').format(self.client.user_id)
15-
16-
def add(self, **kwargs):
17-
if not 'payload' in kwargs:
18-
return create_custom_error_message(error_message='Missing the "payload" parameter.')
19-
path = self.create_node_path()
20-
response = self.client.post(path, kwargs['payload'])
21-
return analyze_response(response)
22-
23-
def verify(self, **kwargs):
24-
micro_keys = ['payload', 'node_id']
25-
ok_micro, error_micro = checkKwargs(micro_keys, kwargs)
26-
mfa_keys = ['payload']
27-
ok_mfa, error_mfa = checkKwargs(mfa_keys, kwargs)
28-
response = None
29-
if ok_micro:
30-
path = self.create_node_path(kwargs['node_id'])
31-
response = self.client.patch(path, kwargs['payload'])
32-
elif ok_mfa:
33-
path = self.create_node_path()
34-
response = self.client.post(path, kwargs['payload'])
13+
return path
14+
15+
def add(self, user_id, payload, **kwargs):
16+
path = self.create_node_path(user_id)
17+
response = self.client.post(path, payload)
18+
return response
19+
20+
def get(self, user_id, node_id=None, **kwargs):
21+
path = self.create_node_path(user_id, node_id)
22+
response = self.client.get(path)
23+
return response
24+
25+
def update(self, user_id, node_id, **kwargs):
26+
path = self.create_node_path(user_id, node_id)
27+
response = self.client.patch(path, payload)
28+
return response
29+
30+
def verify(self, user_id, payload, node_id=None, **kwargs):
31+
if node_id:
32+
# PATCH to verify microdeposits
33+
self.update(user_id, payload, node_id, **kwargs)
3534
else:
36-
return error_micro
37-
return analyze_response(response)
35+
# POST to verify MFA
36+
path = self.create_node_path(user_id)
37+
response = self.client.post(path, payload)
38+
return response
3839

39-
def delete(self, **kwargs):
40-
if not 'node_id' in kwargs:
41-
return create_custom_error_message(error_message='Missing "node_id" argument')
42-
path = self.create_node_path(kwargs['node_id'])
40+
def delete(self, user_id, **kwargs):
41+
path = self.create_node_path(user_id, node_id)
4342
response = self.client.delete(path)
44-
return analyze_response(response)
45-
46-
def get(self, **kwargs):
47-
path = None
48-
if 'node_id' in kwargs:
49-
path = self.create_node_path(kwargs['node_id'])
50-
elif self.client.user_id:
51-
path = self.create_node_path()
52-
else:
53-
return create_custom_error_message(error_message='Set the user id before making this API call.')
54-
response = self.client.get(path)
55-
return analyze_response(response)
43+
return response

synapse_pay_rest/api/Users.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
class Users():
55
def __init__(self, client):
6-
self.url = '/users'
76
self.client = client
87

98
def create_user_path(self, user_id=None):
9+
path = '/users'
1010
if user_id:
11-
return self.url + '/' + user_id
11+
return path + '/' + user_id
1212
else:
13-
return self.url
13+
return path
1414

1515
def create(self, payload, **kwargs):
1616
""" Creates a SynapsePay user and updates the client with the new oauth
@@ -52,7 +52,7 @@ def update(self, user_id, payload, **kwargs):
5252

5353
def refresh(self, user_id, payload, **kwargs):
5454
path = '/oauth/{0}'.format(self.client.user_id)
55-
response = self.client.post(path, kwargs.get('payload'))
55+
response = self.client.post(path, payload)
5656
if 'oauth_key' in response:
5757
self.client.update_headers(oauth_key=response['oauth_key'])
5858
return response
@@ -68,7 +68,7 @@ def add_doc(self, user_id, payload, **kwargs):
6868
:return response The JSON response
6969
"""
7070
path = self.create_user_path(user_id)
71-
response = self.client.patch(path, kwargs.get('payload'))
71+
response = self.client.patch(path, payload)
7272
return response
7373

7474
def verify(self, user_id, payload, **kwargs):

synapse_pay_rest/http_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ def __init__(self, **kwargs):
1818
self.logging = kwargs.get('logging', False)
1919
self.user_id = kwargs.get('user_id')
2020

21-
self.session = requests.Session()
22-
self.session.headers.update(self.headers)
23-
2421
def update_headers(self, **kwargs):
2522
"""Updates the supplied properties on self and in the header dictionary.
2623
@@ -43,6 +40,8 @@ def update_headers(self, **kwargs):
4340
'X-SP-USER': self.oauth_key + '|' + self.fingerprint,
4441
'X-SP-USER-IP': self.ip_address
4542
}
43+
self.session = requests.Session()
44+
self.session.headers.update(self.headers)
4645

4746
def get(self, url, params=None):
4847
self.log_information(self.logging)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .client import *
2+
from .user import *
3+
from .node import *
4+
from .transaction import *
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
from synapse_pay_rest.tests.test_helpers import *
3+
4+
CLIENT_ID = os.environ['CLIENT_ID']
5+
CLIENT_SECRET = os.environ['CLIENT_SECRET']
6+
FINGERPRINT = os.environ['FINGERPRINT']
7+
IP_ADDRESS = '127.0.0.1'
8+
USER_ID = '57d2055a86c27339ffdee4cc'
9+
TO_NODE_ID = '57ec57be86c27345b3f8a159'
10+
11+
test_client = Client(
12+
client_id=CLIENT_ID,
13+
client_secret=CLIENT_SECRET,
14+
fingerprint=FINGERPRINT,
15+
ip_address=IP_ADDRESS,
16+
development_mode=True,
17+
logging=False
18+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from synapse_pay_rest.tests.test_helpers import *
2+
3+
nodes_create_payload = {
4+
"type": "SYNAPSE-US",
5+
"info": {
6+
"nickname": "Python Test SYNAPSE-US"
7+
},
8+
"extra": {
9+
"supp_id": "123sa"
10+
}
11+
}
12+
13+
# nodes_update_payload =

synapse_pay_rest/tests/fixtures/transaction.py

Whitespace-only changes.
File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .test_helpers import *
2+
3+
4+
class NodesTestCases(unittest.TestCase):
5+
def setUp(self):
6+
self.client = test_client
7+
self.user = self.client.users.create(users_create_payload)
8+
refresh_payload = {'refresh_token': self.user['refresh_token']}
9+
self.client.users.refresh(self.user['_id'], refresh_payload)
10+
11+
def test_create_a_new_node(self):
12+
node = self.client.nodes.add(self.user['_id'], nodes_create_payload)
13+
self.assertIsNotNone(node['nodes'][0]['_id'])
14+
15+
def test_get_existing_node(self):
16+
pass
17+
18+
# def test_get_multiple_nodes(self):
19+
# pass
20+
# # self.client.nodes.get()
21+
22+
# def test_update_node_info(self):
23+
# pass
24+
25+
# def test_delete_node(self):
26+
# pass
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import unittest
22
import pdb
3-
import os
43
from pprint import pprint
5-
from synapse_pay_rest.client import Client
64
from synapse_pay_rest.http_client import HttpClient
5+
from synapse_pay_rest.client import Client
76
from synapse_pay_rest.api.users import Users
87
from synapse_pay_rest.api.transactions import Transactions
98
from synapse_pay_rest.api.nodes import Nodes
10-
11-
CLIENT_ID = os.environ['CLIENT_ID']
12-
CLIENT_SECRET = os.environ['CLIENT_SECRET']
13-
FINGERPRINT = os.environ['FINGERPRINT']
14-
IP_ADDRESS = '127.0.0.1'
15-
USER_ID = '57d2055a86c27339ffdee4cc'
16-
TO_NODE_ID = '57ec57be86c27345b3f8a159'
9+
from .fixtures import *

0 commit comments

Comments
 (0)