def currency_converter(): amount = request.args.get("amount") input_currency = request.args.get("input_currency") output_currency = request.args.get("output_currency") # amount and input_currency are required if amount is None or input_currency is None: abort(400) # amount has to be valid float try: amount = float(amount) except ValueError: abort(400) c = CurrencyConverter() # handle errors from conversion try: output = c.convert(amount, input_currency, output_currency) except (HTTPError, ConnectionError): abort(503) # server error, API unavailable except KeyError: abort(400) # client error, invalud currency code return jsonify(output)
def convert_in(self, to_currenty: str): try: el = CurrencyConverter() return Ccy(round( el.convert(self.currency, to_currenty.upper(), self.suma), 2), to_currenty) except KeyError as e: print(e)
def all(self, other, operation): try: if str(other).isdigit(): return operation(self.suma, other) else: el = CurrencyConverter() new_suma = el.convert(other.currency, self.currency, other.suma) return operation(self.suma, new_suma) except KeyError as e: print(e)
parser.add_argument( "--amount", help="Enter amount to convert", type=float, required=True ) parser.add_argument( "--input_currency", help="Enter 3-letter code or symbol of currency, which you want to convert from", type=str, required=True, ) parser.add_argument( "--output_currency", help="Enter 3-letter code or symbol of currency, which you want to convert to (if not specified, converts to all available currencies)", type=str, ) args = parser.parse_args() c = CurrencyConverter() try: output_json = json.dumps( c.convert(args.amount, args.input_currency, args.output_currency) ) except (HTTPError, ConnectionError): output_json = json.dumps( {"error": "Currency API is not available at this time."} ) except KeyError as err: output_json = json.dumps({"error": f"{err} is not a valid currency code."}) print(output_json)