def on_get(self, req, resp):
from_currency = req.get_param("from", required=True)
to_currency = req.get_param("to", required=True)
start_date = req.get_param_as_date("start_date", required=True)
end_date = req.get_param_as_date("end_date", required=True)
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:
if start_date == end_date:
exchange_rate = self.container.exchange_rate_manager.get_exchange_rate_by_date(start_date, from_currency, to_currency)
else:
exchange_rate = self.container.exchange_rate_manager.get_average_exchange_rate_by_dates(start_date, end_date, 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 range request %s->%s (%s - %s)", from_currency, to_currency, start_date, end_date)
if not exchange_rate:
self.container.logger.error("Exchange rate not found: range %s/%s %s->%s", start_date, end_date, from_currency, to_currency)
raise falcon.HTTPInternalServerError("Exchange rate not found", "Exchange rate not found")
self.container.logger.info("GET range %s/%s %s->%s %s", start_date, end_date, from_currency, to_currency, exchange_rate)
resp.status = falcon.HTTP_200
resp.body = json.dumps(
{
"start_date": start_date.strftime(format="%Y-%m-%d"),
"end_date": end_date.strftime(format="%Y-%m-%d"),
"from_currency": from_currency,
"to_currency": to_currency,
"exchange_rate": str(exchange_rate)
}
)
评论列表
文章目录