From 0fdf765fefd000d212737169f9af07d7a83cbf96 Mon Sep 17 00:00:00 2001 From: Pablo Martin Date: Wed, 12 Jun 2024 00:08:25 +0200 Subject: [PATCH] implement response processing from xe --- xexe/rate_fetching.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/xexe/rate_fetching.py b/xexe/rate_fetching.py index be9f108..8fd5d73 100644 --- a/xexe/rate_fetching.py +++ b/xexe/rate_fetching.py @@ -1,8 +1,9 @@ import datetime import os from abc import ABC, abstractmethod +from decimal import Decimal -from money.currency import Currency +from money.currency import Currency, CurrencyHelper from money.money import Money from xecd_rates_client import XecdClient @@ -63,4 +64,18 @@ class XERateFetcher(RateFetcher): # 1 unit of from_curr get us. ) - # handle response into proper object + from_currency = Currency(response["from"]) + to_currency = Currency(response["to"][0]["quotecurrency"]) + rate_date = datetime.datetime.fromisoformat( + response["timestamp"].replace( + "Z", "+00:00" + ) # Funny replace is necessary because of API response format + ).date() + rate = f"""{response["to"][0]["mid"]:.{CurrencyHelper.decimal_precision_for_currency(to_currency)}f}""" + + return ExchangeRate( + from_currency=from_currency, + to_currency=to_currency, + rate_date=rate_date, + rate=rate, + )