Exemple #1
0
    def currency_converter():
        args = Namespace(amount=request.args.get('amount'),
                         input_currency=request.args.get('input_currency'),
                         output_currency=request.args.get('output_currency'))
        if not args.amount or not args.input_currency:
            return error(GET_PARAM_ERR)
        try:
            amount = float(args.amount)
        except ValueError:
            return error(GET_PARAM_ERR)
        if check_currency_args_validity(args) is False or amount < 0:
            return error(GET_PARAM_ERR)

        currency_from, currency_to = adjust_from_to_currency(
            args.input_currency, args.output_currency)

        exchange_rates = download_exchange_rates()

        converted_currency = convert(amount, currency_from, currency_to,
                                     exchange_rates)

        return jsonify({
            'input': {
                'amount': amount,
                'currency_from': currency_from
            },
            'output': converted_currency
        })
Exemple #2
0
 def test_symbols_to_names(self):
     input_currency, output_currency = adjust_from_to_currency('₩', '฿')
     assert (input_currency, output_currency) == ('KRW', 'THB')
Exemple #3
0
 def test_name_and_symbol_to_names(self):
     input_currency, output_currency = adjust_from_to_currency('EUR', '$')
     assert (input_currency, output_currency) == ('EUR', 'USD')
Exemple #4
0
 def test_symbol_and_name_to_names(self):
     input_currency, output_currency = adjust_from_to_currency('₪', 'CZK')
     assert (input_currency, output_currency) == ('ILS', 'CZK')
Exemple #5
0
# Author: Jakub Kubik
# Date: 10.03.2019

from functions.arguments import parse_arguments, check_currency_args_validity, adjust_from_to_currency
from functions.download import download_exchange_rates
from functions.conversion import convert
from sys import stderr
from json import dumps


if __name__ == '__main__':
    args = parse_arguments()
    if check_currency_args_validity(args) is False or args.amount < 0:
        stderr.write('Wrong arguments!\n')
        raise SystemExit(2)

    amount = args.amount
    currency_from, currency_to = adjust_from_to_currency(args.input_currency, args.output_currency)

    exchange_rates = download_exchange_rates()
    if exchange_rates is None:
        stderr.write('Can\'t download today\'s currency rates!\n')
        raise SystemExit(3)

    converted_currency = convert(amount, currency_from, currency_to, exchange_rates)

    print(
        dumps({'input': {'amount': amount, 'currency': currency_from},
               'output': converted_currency},
              indent=4))