Beispiel #1
0
def convert_currency():
    args = request.args
    amount = request.args.get("amount")
    input_currency = request.args.get("input_currency")
    output_currency = request.args.get("output_currency")
    error_response = {
        'error': "",
        'required arguemnts': {
            'amount': "Amount to exchange (float)",
            'input_currency': "Base currency (string)"
        },
        'optional arguments': {
            'output_currency': "Wanted currencies (string)"
        }
    }
    print(error_response)
    if amount == None or input_currency == None:
        error_response['error'] = "Request missing required argument"
        return jsonify(error_response), status.HTTP_400_BAD_REQUEST
    try:
        amount = float(amount)
    except:
        error_response['error'] = "Amount have to be a number"
        return jsonify(error_response), status.HTTP_400_BAD_REQUEST
    # convertion
    converter = Converter()
    result = converter.change_currency(amount, input_currency, output_currency)
    return jsonify(result), status.HTTP_200_OK
from converter import Converter

if __name__ == "__main__":
    #parse arguments
    converter = Converter()
    args = converter.parse_arguments()
    result = converter.change_currency(args.amount, args.input_currency,
                                       args.output_currency)
    print(converter.to_json(result))