Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,68 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Access the API key from the secret
|
| 11 |
+
API_KEY = os.getenv("apikey")
|
| 12 |
+
|
| 13 |
+
# Check if the API key is loaded correctly (optional)
|
| 14 |
+
if api_key:
|
| 15 |
+
print("API key loaded successfully!")
|
| 16 |
+
else:
|
| 17 |
+
print("API key not found!")
|
| 18 |
+
|
| 19 |
+
API_URL = f"https://v6.exchangerate-api.com/v6/{API_KEY}/latest/USD"
|
| 20 |
+
CACHE = None
|
| 21 |
+
LAST_FETCH_TIME = None
|
| 22 |
+
CACHE_TTL = 3600 # Time to live for cache in seconds (1 hour)
|
| 23 |
+
|
| 24 |
+
def fetch_exchange_rates():
|
| 25 |
+
"""Fetch the exchange rates from the API."""
|
| 26 |
+
response = requests.get(API_URL)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
return response.json()
|
| 29 |
+
else:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
def get_cached_rates():
|
| 33 |
+
"""Return cached rates if they exist and are not expired."""
|
| 34 |
+
global CACHE, LAST_FETCH_TIME
|
| 35 |
+
|
| 36 |
+
# Check if the cache is expired
|
| 37 |
+
if CACHE is None or (time.time() - LAST_FETCH_TIME) > CACHE_TTL:
|
| 38 |
+
# Cache is either not set or expired, fetch new rates
|
| 39 |
+
rates_data = fetch_exchange_rates()
|
| 40 |
+
if rates_data:
|
| 41 |
+
CACHE = rates_data
|
| 42 |
+
LAST_FETCH_TIME = time.time() # Update the fetch time
|
| 43 |
+
else:
|
| 44 |
+
return {"error": "Failed to fetch exchange rates"}
|
| 45 |
+
|
| 46 |
+
return CACHE
|
| 47 |
+
|
| 48 |
+
@app.get("/exchange-rates")
|
| 49 |
+
async def get_exchange_rates():
|
| 50 |
+
"""Endpoint to fetch the exchange rates."""
|
| 51 |
+
rates = get_cached_rates()
|
| 52 |
+
return rates
|
| 53 |
+
|
| 54 |
+
@app.get("/convert")
|
| 55 |
+
async def convert_currency(amount: float, from_currency: str, to_currency: str):
|
| 56 |
+
"""Endpoint to convert currency from one to another."""
|
| 57 |
+
rates = get_cached_rates()
|
| 58 |
+
|
| 59 |
+
if "error" in rates:
|
| 60 |
+
return {"error": "Failed to fetch exchange rates"}
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
from_rate = rates["conversion_rates"][from_currency]
|
| 64 |
+
to_rate = rates["conversion_rates"][to_currency]
|
| 65 |
+
converted_amount = amount * (to_rate / from_rate)
|
| 66 |
+
return {"converted_amount": converted_amount}
|
| 67 |
+
except KeyError:
|
| 68 |
+
return {"error": "Invalid currency code"}
|