Skip to content

Commit f598ed9

Browse files
committed
Add comments
1 parent 53bbc62 commit f598ed9

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

paymentrails/utils.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,52 @@
22

33

44
class UrlUtils:
5+
"""
6+
Helper class which allows automatic conversion from Python function parameters to Payment
7+
Rails supported API parameters.
8+
"""
9+
510
@staticmethod
611
def __to_came_case(string):
7-
# https://stackoverflow.com/a/19053800/6626193
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+
"""
819
components = string.split('_')
920
return components[0] + ''.join(x.title() for x in components[1:])
1021

1122
@staticmethod
1223
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]
1336
params = []
37+
# Iterate over attribute keys
1438
for key in attributes.keys():
39+
# Note that locals() also returns the "self" attribute, so just ignore it
1540
if key == "self":
1641
continue
17-
if attributes[key] is not None:
18-
param = UrlUtils.__to_came_case(key) + "=" + urllib.parse.quote(str(attributes[key]))
19-
params.append(param)
2042

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
2153
return "&".join(params)

0 commit comments

Comments
 (0)