Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
-
from currency_converter import CurrencyConverter
|
3 |
-
from datetime import date
|
4 |
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
c = CurrencyConverter()
|
9 |
|
10 |
@app.get("/")
|
11 |
def welcome():
|
@@ -15,26 +14,19 @@ def welcome():
|
|
15 |
def convert_currency(
|
16 |
from_currency: str = Query(..., description="C贸digo da moeda de origem (ex: USD)"),
|
17 |
to_currency: str = Query(..., description="C贸digo da moeda de destino (ex: BRL)"),
|
18 |
-
amount: float = Query(..., description="Quantia a ser convertida")
|
19 |
-
conversion_date: date = Query(None, description="Data opcional para usar a taxa de c芒mbio hist贸rica (ex: 2023-12-01)")
|
20 |
):
|
21 |
"""
|
22 |
Converte um valor de uma moeda para outra.
|
23 |
"""
|
24 |
try:
|
25 |
-
|
26 |
-
if conversion_date:
|
27 |
-
converted_amount = c.convert(amount, from_currency.upper(), to_currency.upper(), date=conversion_date)
|
28 |
-
else:
|
29 |
-
converted_amount = c.convert(amount, from_currency.upper(), to_currency.upper())
|
30 |
-
|
31 |
return {
|
32 |
"from_currency": from_currency.upper(),
|
33 |
"to_currency": to_currency.upper(),
|
34 |
"amount": amount,
|
35 |
"converted_amount": round(converted_amount, 2),
|
36 |
-
"
|
37 |
}
|
38 |
-
|
39 |
except ValueError as e:
|
40 |
-
raise HTTPException(status_code=400, detail=str(e))
|
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
+
from currency_converter import CurrencyConverter, ECB_URL
|
|
|
3 |
|
4 |
+
# Baixar dados mais recentes do Banco Central Europeu
|
5 |
+
c = CurrencyConverter(ECB_URL)
|
6 |
|
7 |
+
app = FastAPI()
|
|
|
8 |
|
9 |
@app.get("/")
|
10 |
def welcome():
|
|
|
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.
|
21 |
"""
|
22 |
try:
|
23 |
+
converted_amount = c.convert(amount, from_currency.upper(), to_currency.upper())
|
|
|
|
|
|
|
|
|
|
|
24 |
return {
|
25 |
"from_currency": from_currency.upper(),
|
26 |
"to_currency": to_currency.upper(),
|
27 |
"amount": amount,
|
28 |
"converted_amount": round(converted_amount, 2),
|
29 |
+
"source": "ECB (atualizado)"
|
30 |
}
|
|
|
31 |
except ValueError as e:
|
32 |
+
raise HTTPException(status_code=400, detail=str(e))
|