Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,37 @@
|
|
1 |
-
from fastapi import FastAPI, Query
|
2 |
-
from forex_python.converter import CurrencyRates
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
|
|
6 |
currency_rates = CurrencyRates()
|
7 |
|
8 |
-
@app.get("/
|
9 |
-
def
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
"""
|
13 |
-
Converte
|
14 |
"""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Query, HTTPException
|
2 |
+
from forex_python.converter import CurrencyRates, RatesNotAvailableError
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
+
# Inicializar o conversor de moedas
|
7 |
currency_rates = CurrencyRates()
|
8 |
|
9 |
+
@app.get("/")
|
10 |
+
def greet_json():
|
11 |
+
return {"message": "Bem-vindo 脿 API de Convers茫o de Moedas!"}
|
12 |
+
|
13 |
+
@app.get("/convert-currency/")
|
14 |
+
def convert_currency(
|
15 |
+
from_currency: str = Query(..., description="C贸digo da moeda de origem (ex: USD)"),
|
16 |
+
to_currency: str = Query(..., description="C贸digo da moeda de destino (ex: BRL)"),
|
17 |
+
amount: float = Query(..., description="Quantia a ser convertida")
|
18 |
+
):
|
19 |
"""
|
20 |
+
Converte um valor de uma moeda para outra usando taxas de c芒mbio em tempo real.
|
21 |
"""
|
22 |
+
try:
|
23 |
+
# Obter a taxa de convers茫o
|
24 |
+
converted_amount = currency_rates.convert(from_currency.upper(), to_currency.upper(), amount)
|
25 |
+
rate = currency_rates.get_rate(from_currency.upper(), to_currency.upper())
|
26 |
+
|
27 |
+
return {
|
28 |
+
"from_currency": from_currency.upper(),
|
29 |
+
"to_currency": to_currency.upper(),
|
30 |
+
"amount": amount,
|
31 |
+
"converted_amount": round(converted_amount, 2),
|
32 |
+
"exchange_rate": rate
|
33 |
+
}
|
34 |
+
except RatesNotAvailableError:
|
35 |
+
raise HTTPException(status_code=400, detail="Convers茫o n茫o dispon铆vel para as moedas fornecidas.")
|
36 |
+
except Exception as e:
|
37 |
+
raise HTTPException(status_code=500, detail=f"Erro interno: {str(e)}")
|