File size: 2,336 Bytes
e3181cb
fb09834
 
 
e3181cb
 
 
fb09834
 
 
 
 
 
 
 
c9d781e
fb09834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6078ea
fb09834
 
 
873f9e5
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from fastapi import FastAPI
import requests
import json
import time

app = FastAPI()

import os

# Access the API key from the secret
API_KEY = os.getenv("apikey")

API_URL = f"https://v6.exchangerate-api.com/v6/{API_KEY}/latest/USD"
CACHE = None
LAST_FETCH_TIME = None
CACHE_TTL = 86400  # Time to live for cache in seconds (1 day)

def fetch_exchange_rates():
    """Fetch the exchange rates from the API."""
    response = requests.get(API_URL)
    if response.status_code == 200:
        return response.json()
    else:
        return None

def get_cached_rates():
    """Return cached rates if they exist and are not expired."""
    global CACHE, LAST_FETCH_TIME

    # Check if the cache is expired
    if CACHE is None or (time.time() - LAST_FETCH_TIME) > CACHE_TTL:
        # Cache is either not set or expired, fetch new rates
        rates_data = fetch_exchange_rates()
        if rates_data:
            CACHE = rates_data
            LAST_FETCH_TIME = time.time()  # Update the fetch time
        else:
            return {"error": "Failed to fetch exchange rates"}
    
    return CACHE

@app.get("/exchange-rates")
async def get_exchange_rates():
    """Endpoint to fetch the exchange rates."""
    rates = get_cached_rates()
    return rates

@app.get("/convert")
async def convert_currency(amount: float, from_currency: str, to_currency: str):
    """Endpoint to convert currency from one to another."""
    rates = get_cached_rates()

    if "error" in rates:
        return {"error": "Failed to fetch exchange rates"}

    try:
        from_rate = rates["conversion_rates"][from_currency]
        to_rate = rates["conversion_rates"][to_currency]
        converted_amount = amount * (to_rate / from_rate)
        converted_amount = str(converted_amount)
        return {"converted_amount": converted_amount}
    except KeyError:
        return {"error": "Invalid currency code"}

@app.get("/supported-currencies")
async def get_supported_currencies():
    """Endpoint to return the list of supported currencies."""
    rates = get_cached_rates()

    if "error" in rates:
        return {"error": "Failed to fetch exchange rates"}

    # Extract the keys (currency codes) from the conversion_rates field
    supported_currencies = list(rates["conversion_rates"].keys())
    return {"currencies": supported_currencies}