Pridani stahovani kurzu a jednotneho API
- GET /{source_id} pro cnb, kb, csob, moneta, jt, mbank, ppf, trinity, fio, creditas
- jednotny tvar odpovedi s bezhotovostnimi kurzy za 1 jednotku meny
- parametr ?base pro ceny v jine mene nez CZK
- pole buy_sell_origin a middle_origin rikaji, co je stazene a co dopocitane
- pravidelne stahovani na pozadi, ukladani do JSON, priznak stale
- testy a dokumentace ve slozce documentation
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"""Stores one JSON file per source."""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from app.models import RateSheet
|
||||
|
||||
|
||||
def _sheet_path(data_dir: Path, source_id: str) -> Path:
|
||||
return data_dir / f"{source_id}.json"
|
||||
|
||||
|
||||
def write_sheet(data_dir: Path, sheet: RateSheet) -> None:
|
||||
"""Replaces the file atomically, so a reader never sees a partial write."""
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
descriptor, temp_name = tempfile.mkstemp(dir=data_dir, suffix=".tmp")
|
||||
try:
|
||||
with os.fdopen(descriptor, "wb") as temp_file:
|
||||
temp_file.write(sheet.model_dump_json(indent=2).encode("utf-8"))
|
||||
os.replace(temp_name, _sheet_path(data_dir, sheet.source))
|
||||
except BaseException:
|
||||
Path(temp_name).unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
|
||||
def read_sheet(data_dir: Path, source_id: str) -> RateSheet | None:
|
||||
path = _sheet_path(data_dir, source_id)
|
||||
if not path.exists():
|
||||
return None
|
||||
return RateSheet.model_validate_json(path.read_bytes())
|
||||
Reference in New Issue
Block a user