Skip to content

Commit 292e9c9

Browse files
authored
Merge pull request #8 from barreeeiroo/master
feat!: Improve search/find methods
2 parents 27c62dc + f598ed9 commit 292e9c9

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ paymentrails/exception/__pycache__
55
*.pyc
66
.env
77
*.egg-info
8+
.idea/
9+
venv-*/

paymentrails/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def sendRequest(self,endpoint,method,body=""):
4242
'Authorization': authorization,
4343
'X-PR-Timestamp': str(timestamp)}
4444

45-
if method is "GET":
45+
if method == "GET":
4646
request = requests.get(self.config.enviroment + endpoint, headers=headers)
47-
elif method is "POST":
47+
elif method == "POST":
4848
request = requests.post(self.config.enviroment + endpoint, headers=headers, json=body)
49-
elif method is "PATCH":
49+
elif method == "PATCH":
5050
request = requests.patch(self.config.enviroment + endpoint, headers=headers, json=body)
51-
elif method is "DELETE":
51+
elif method == "DELETE":
5252
request = requests.delete(self.config.enviroment + endpoint, headers=headers)
5353
else:
5454
self.throw_status_code_exception(None, "Invalid Method")

paymentrails/payment_gateway.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def find(self, payment_id, batch_id):
2828
def create(self, body, batch_id):
2929
if body is None:
3030
raise InvalidFieldException("Body cannot be None.")
31+
elif batch_id is None:
32+
raise InvalidFieldException("Batch ID cannot be None.")
3133
endpoint = '/v1/batches/' + batch_id + '/payments/'
3234
response = paymentrails.configuration.Configuration.client(
3335
self.config).post(endpoint, body)

paymentrails/recipient_gateway.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import namedtuple
22
from paymentrails.exceptions.invalidFieldException import InvalidFieldException
33
import paymentrails.configuration
4+
from paymentrails.utils import UrlUtils
5+
46

57
class RecipientGateway(object):
68
"""
@@ -46,7 +48,6 @@ def update(self, recipient_id, body):
4648
paymentrails.configuration.Configuration.client(
4749
self.config).patch(endpoint, body)
4850
return True
49-
5051

5152
def delete(self, recipient_id):
5253
if recipient_id is None:
@@ -56,9 +57,10 @@ def delete(self, recipient_id):
5657
self.config).delete(endpoint)
5758
return True
5859

59-
def search(self, page, page_number, term):
60-
endpoint = '/v1/recipients?search=' + term + '&page=' + \
61-
str(page) + '&pageSize=' + str(page_number)
60+
def search(self, page=None, page_size=None, search=None, name=None, email=None, reference_id=None, start_date=None,
61+
end_date=None, status=None, compliance_status=None, country=None, payout_method=None, currency=None,
62+
order_by=None, sort_by=None):
63+
endpoint = '/v1/recipients?' + UrlUtils.parse(locals())
6264
response = paymentrails.configuration.Configuration.client(
6365
self.config).get(endpoint)
6466
recipients = []

paymentrails/utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import urllib.parse
2+
3+
4+
class UrlUtils:
5+
"""
6+
Helper class which allows automatic conversion from Python function parameters to Payment
7+
Rails supported API parameters.
8+
"""
9+
10+
@staticmethod
11+
def __to_came_case(string):
12+
"""
13+
Converts the given string from snake_case (Python PEP) to lowerCamelCase (Payment Rails API)
14+
See: https://stackoverflow.com/a/19053800/6626193
15+
16+
:param string: the given snake_case string
17+
:return: the equivalent lowerCamelCase string
18+
"""
19+
components = string.split('_')
20+
return components[0] + ''.join(x.title() for x in components[1:])
21+
22+
@staticmethod
23+
def parse(attributes):
24+
"""
25+
Given a series of attributes in a dictionary, parses such dictionary removing all None values and
26+
returns a URL compatible parameter string.
27+
To use this method, invoke it like UrlUtils.parse(locals()), as locals() will return all parameters that
28+
the function is receiving.
29+
For an example, see RecipientGateway.search.
30+
31+
:param: attributes: dictionary of all attributes, including None ones
32+
:return: url compatible parameter string
33+
"""
34+
35+
# List containing already parsed params [key1=value1, key2=value2]
36+
params = []
37+
# Iterate over attribute keys
38+
for key in attributes.keys():
39+
# Note that locals() also returns the "self" attribute, so just ignore it
40+
if key == "self":
41+
continue
42+
43+
# Ignore None attributes as well
44+
if attributes[key] is None:
45+
continue
46+
47+
# Convert the valid parameter to a Payment Rails API compatible one
48+
param = UrlUtils.__to_came_case(key) + "=" + urllib.parse.quote(str(attributes[key]))
49+
# And add it to the list
50+
params.append(param)
51+
52+
# Return the list as a string with "&" as the join character
53+
return "&".join(params)

0 commit comments

Comments
 (0)