Example #1
0
def convert():
    if request.method == 'GET':
        from_currency = request.headers.get('from_currency')
        to_currency = request.headers.get('to_currency')
        value = request.headers.get('value')
        retriever = RateRetriever(RATES_FILE)
        converter = CurrencyConverter(from_currency, to_currency, value,
                                      retriever)
        converted_value = converter.convert()
        return json.dumps(converter.build_response(converted_value), indent=4)
Example #2
0
def convert_multi():
    if request.method == 'POST':
        with open(SWAGGER, 'r') as swagger:
            try:
                schema = yaml.safe_load(swagger)
            except yaml.YAMLError as exc:
                print(exc)
        try:
            validate(request.get_json(), schema)
        except ValidationError as err:
            response = dict()
            response['ERROR'] = str(err)
            return json.dumps(response, indent=4)
        retriever = RateRetriever(RATES_FILE)
        converter = MultiCurrencyConverter(retriever)
        return json.dumps(converter.multi_convert(request.get_json()),
                          indent=4)
Example #3
0
 def test_load_rates(self):
     self.retriever = RateRetriever(MOCKED_RATES_FILE)
     self.retriever.conversion_rates = {
         "disclaimer":
         "Usage subject to terms: https://openexchangerates.org/terms",
         "license": "https://openexchangerates.org/license",
         "timestamp": "TEST",
         "base": "USD",
         "rates": {
             "CZK": 22.9508,
             "EUR": 0.89194,
             "PLN": 3.8174
         }
     }
     self.retriever._update_rates()
     with open(MOCKED_RATES_FILE, 'r') as rates:
         file_written = json.load(rates)
     self.assertDictEqual(file_written, self.retriever.conversion_rates)
Example #4
0
 def setUp(self):
     self.retriever = RateRetriever(MOCKED_RATES_FILE)
Example #5
0
 def test_load_rates_no_file(self):
     self.retriever = RateRetriever(None)
     self.assertDictEqual(self.retriever.conversion_rates, {})
Example #6
0
def rate():
    if request.method == 'GET':
        retriever = RateRetriever(RATES_FILE)
        return json.dumps(retriever.get_rates(request.headers.get('currency')),
                          indent=4)
 def setUp(self):
     retriever = RateRetriever(MOCKED_RATES_FILE)
     self.converter = MultiCurrencyConverter(retriever)