"""The universal contract: every source returns exactly this shape.""" from datetime import date, datetime from typing import Literal from pydantic import BaseModel, Field # "fetched" is a value published by the source, "computed" is calculated by this service. Origin = Literal["fetched", "computed"] class Rate(BaseModel): """All prices are for 1 unit of the currency and are always present. They are in CZK as published by the sources, API responses can convert them to another base. """ currency: str = Field(pattern=r"^[A-Z]{3}$", description="ISO 4217 currency code") buy: float = Field(gt=0, description="Price of 1 unit in the base currency when the bank buys") sell: float = Field(gt=0, description="Price of 1 unit in the base currency when the bank sells") middle: float = Field(gt=0, description="Middle price of 1 unit in the base currency") buy_sell_origin: Origin = Field(description="Whether buy and sell are fetched or computed") middle_origin: Origin = Field(description="Whether middle is fetched or computed") class ParsedRates(BaseModel): """What a parser extracts from a single source document.""" valid_date: date = Field(description="Date the rates are valid for") rates: list[Rate] = Field(min_length=1) class RateSheet(ParsedRates): """A stored result of a successful download.""" source: str source_name: str source_url: str fetched_at: datetime class RateSheetResponse(RateSheet): base: str = Field(description="Currency all prices in this response are expressed in") stale: bool = Field(description="True when recent refreshes failed and the data may be outdated")