feat: add empty_weigh and full_weight fields for auto and mechanic rvalution models
This commit is contained in:
33
core/services/currency.py
Normal file
33
core/services/currency.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from datetime import date
|
||||
|
||||
import requests
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CurrencyService:
|
||||
BASE_URL = "https://cbu.uz/oz/arkhiv-kursov-valyut/json"
|
||||
CODES = ("USD", "EUR", "RUB")
|
||||
TIMEOUT_SECONDS = 5
|
||||
|
||||
@classmethod
|
||||
def get_rates(cls, target_date=None) -> dict:
|
||||
if target_date is None:
|
||||
target_date = date.today()
|
||||
date_str = target_date.strftime("%Y-%m-%d")
|
||||
|
||||
rates = {}
|
||||
for code in cls.CODES:
|
||||
url = f"{cls.BASE_URL}/{code}/{date_str}/"
|
||||
try:
|
||||
response = requests.get(url, timeout=cls.TIMEOUT_SECONDS)
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
if isinstance(payload, list) and payload:
|
||||
rate_value = payload[0].get("Rate")
|
||||
if rate_value:
|
||||
rates[code] = rate_value
|
||||
except (requests.RequestException, ValueError) as e:
|
||||
logger.exception(f"CBU API error for {code}: {e}")
|
||||
return rates
|
||||
Reference in New Issue
Block a user