|
2 | 2 |
|
3 | 3 |
|
4 | 4 | class UrlUtils: |
| 5 | + """ |
| 6 | + Helper class which allows automatic conversion from Python function parameters to Payment |
| 7 | + Rails supported API parameters. |
| 8 | + """ |
| 9 | + |
5 | 10 | @staticmethod |
6 | 11 | 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 | + """ |
8 | 19 | components = string.split('_') |
9 | 20 | return components[0] + ''.join(x.title() for x in components[1:]) |
10 | 21 |
|
11 | 22 | @staticmethod |
12 | 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] |
13 | 36 | params = [] |
| 37 | + # Iterate over attribute keys |
14 | 38 | for key in attributes.keys(): |
| 39 | + # Note that locals() also returns the "self" attribute, so just ignore it |
15 | 40 | if key == "self": |
16 | 41 | 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) |
20 | 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 |
21 | 53 | return "&".join(params) |
0 commit comments