Fetch the exchange rate
Python · FastAPI · beginner · greenfield
Adds the FX rate fetch: calls the upstream provider and returns the parsed rate using requests.
get_rate runs inside a request handler that serves the checkout page; the upstream provider occasionally stalls.
Requirements
- get_rate(base, quote) fetches the FX rate from the upstream provider.
- The upstream is a third-party HTTP API that can be slow or hang.
- If the upstream stops sending data, the call must time out rather than wait indefinitely.
Files touched
- app/fx/rates.py
--- app/fx/rates.py
+import requests
+
+PROVIDER = "https://fx.example.com/rate"
+
+
+def get_rate(base: str, quote: str) -> float:
+ resp = requests.get(PROVIDER, params={"base": base, "quote": quote})
+ resp.raise_for_status()
+ return resp.json()["rate"]