def on_get(self, req, resp):
from_currency = req.get_param("from", required=True)
to_currency = req.get_param("to", required=True)
date_of_exchange = req.get_param_as_date("date")
date_of_exchange = date_of_exchange if date_of_exchange else date.today()
invalid_currencies = [currency for currency in (from_currency, to_currency) if currency not in SUPPORTED_CURRENCIES]
if invalid_currencies:
raise falcon.HTTPInvalidParam("Invalid currency", " and ".join(invalid_currencies))
exchange_rate = None
try:
exchange_rate = self.container.exchange_rate_manager.get_exchange_rate_by_date(date_of_exchange, from_currency, to_currency)
except DatabaseError:
self.container.db_session.rollback()
self.container.logger.exception("Database error occurred. Rollback session to allow reconnect to the DB on next request.")
except Exception:
self.container.logger.exception("Unexpected exception while rate request %s->%s (%s)", from_currency, to_currency, date_of_exchange)
if not exchange_rate:
self.container.logger.error("Exchange rate not found: rate %s %s->%s", date_of_exchange, from_currency, to_currency)
raise falcon.HTTPInternalServerError("Exchange rate not found", "Exchange rate not found")
self.container.logger.info("GET rate %s %s->%s %s", date_of_exchange, from_currency, to_currency, exchange_rate)
resp.status = falcon.HTTP_200
resp.body = json.dumps(
{
"date": date_of_exchange.strftime(format="%Y-%m-%d"),
"from_currency": from_currency,
"to_currency": to_currency,
"exchange_rate": str(exchange_rate)
}
)
评论列表
文章目录