|
| 1 | +from python_core import HttpEndpoint |
| 2 | +from collections import namedtuple |
| 3 | + |
| 4 | +class AbstractExchangeRates: |
| 5 | + api_key = None |
| 6 | + endpoint_subdomain = 'exchange-rates' |
| 7 | + global_req_params = { |
| 8 | + 'lang' : 'python' |
| 9 | + } |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def configure(api_key): |
| 13 | + AbstractExchangeRates.api_key = api_key |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def live(base, target=""): |
| 17 | + |
| 18 | + http_endpoint = HttpEndpoint( |
| 19 | + endpoint_subdomain=AbstractExchangeRates.endpoint_subdomain, |
| 20 | + global_req_params=AbstractExchangeRates.global_req_params, |
| 21 | + path='live/' |
| 22 | + ) |
| 23 | + |
| 24 | + if AbstractExchangeRates.api_key is None: |
| 25 | + exception_type = "APINotConfiguredException" |
| 26 | + exception_msg = "Can't use the endpoint unless it's configured, use configure(api_key)" |
| 27 | + raise Exception( |
| 28 | + "[{}] : {}".format(exception_type, exception_msg) |
| 29 | + ) |
| 30 | + |
| 31 | + try: |
| 32 | + req_params = { |
| 33 | + "api_key" : AbstractExchangeRates.api_key, |
| 34 | + "base" : base |
| 35 | + } |
| 36 | + |
| 37 | + if target != "": |
| 38 | + req_params['target'] = target |
| 39 | + |
| 40 | + response = http_endpoint.get( |
| 41 | + req_params=req_params |
| 42 | + ) |
| 43 | + # Convert the dict to an object |
| 44 | + for key in response: |
| 45 | + if (type(response[key]) == dict): |
| 46 | + EmbeddedObject = namedtuple('EmbeddedObject', response[key]) |
| 47 | + embedded_obj = EmbeddedObject(**response[key]) |
| 48 | + response[key] = embedded_obj |
| 49 | + ResponseObject = namedtuple('ResponseObject', response.keys()) |
| 50 | + response_obj = ResponseObject(**response) |
| 51 | + return response_obj |
| 52 | + |
| 53 | + except Exception as e: |
| 54 | + # HttpEndpoint has expressive exceptions |
| 55 | + raise SystemExit(e) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def historical(base, date, target=""): |
| 59 | + |
| 60 | + http_endpoint = HttpEndpoint( |
| 61 | + endpoint_subdomain=AbstractExchangeRates.endpoint_subdomain, |
| 62 | + global_req_params=AbstractExchangeRates.global_req_params, |
| 63 | + path='historical/' |
| 64 | + ) |
| 65 | + |
| 66 | + if AbstractExchangeRates.api_key is None: |
| 67 | + exception_type = "APINotConfiguredException" |
| 68 | + exception_msg = "Can't use the endpoint unless it's configured, use configure(api_key)" |
| 69 | + raise Exception( |
| 70 | + "[{}] : {}".format(exception_type, exception_msg) |
| 71 | + ) |
| 72 | + |
| 73 | + try: |
| 74 | + req_params = { |
| 75 | + "api_key" : AbstractExchangeRates.api_key, |
| 76 | + "base" : base, |
| 77 | + "date" : date |
| 78 | + } |
| 79 | + |
| 80 | + if target != "": |
| 81 | + req_params['target'] = target |
| 82 | + |
| 83 | + response = http_endpoint.get( |
| 84 | + req_params=req_params |
| 85 | + ) |
| 86 | + # Convert the dict to an object |
| 87 | + for key in response: |
| 88 | + if (type(response[key]) == dict): |
| 89 | + EmbeddedObject = namedtuple('EmbeddedObject', response[key]) |
| 90 | + embedded_obj = EmbeddedObject(**response[key]) |
| 91 | + response[key] = embedded_obj |
| 92 | + ResponseObject = namedtuple('ResponseObject', response.keys()) |
| 93 | + response_obj = ResponseObject(**response) |
| 94 | + return response_obj |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + # HttpEndpoint has expressive exceptions |
| 98 | + raise SystemExit(e) |
| 99 | + |
| 100 | + @staticmethod |
| 101 | + def convert(base, target, date="", base_amount=""): |
| 102 | + |
| 103 | + http_endpoint = HttpEndpoint( |
| 104 | + endpoint_subdomain=AbstractExchangeRates.endpoint_subdomain, |
| 105 | + global_req_params=AbstractExchangeRates.global_req_params, |
| 106 | + path='convert/' |
| 107 | + ) |
| 108 | + |
| 109 | + if AbstractExchangeRates.api_key is None: |
| 110 | + exception_type = "APINotConfiguredException" |
| 111 | + exception_msg = "Can't use the endpoint unless it's configured, use configure(api_key)" |
| 112 | + raise Exception( |
| 113 | + "[{}] : {}".format(exception_type, exception_msg) |
| 114 | + ) |
| 115 | + |
| 116 | + try: |
| 117 | + req_params = { |
| 118 | + "api_key" : AbstractExchangeRates.api_key, |
| 119 | + "base" : base, |
| 120 | + "target" : target |
| 121 | + } |
| 122 | + |
| 123 | + if date != "": |
| 124 | + req_params['date'] = date |
| 125 | + |
| 126 | + if base_amount != "": |
| 127 | + req_params['base_amount'] = base_amount |
| 128 | + |
| 129 | + response = http_endpoint.get( |
| 130 | + req_params=req_params |
| 131 | + ) |
| 132 | + # Convert the dict to an object |
| 133 | + for key in response: |
| 134 | + if (type(response[key]) == dict): |
| 135 | + EmbeddedObject = namedtuple('EmbeddedObject', response[key]) |
| 136 | + embedded_obj = EmbeddedObject(**response[key]) |
| 137 | + response[key] = embedded_obj |
| 138 | + ResponseObject = namedtuple('ResponseObject', response.keys()) |
| 139 | + response_obj = ResponseObject(**response) |
| 140 | + return response_obj |
| 141 | + |
| 142 | + except Exception as e: |
| 143 | + # HttpEndpoint has expressive exceptions |
| 144 | + raise SystemExit(e) |
0 commit comments