Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,40 @@
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
-
from
|
|
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
@app.get("/")
|
10 |
-
def
|
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
|
21 |
"""
|
22 |
try:
|
23 |
-
#
|
24 |
-
|
25 |
-
|
|
|
|
|
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 |
-
"
|
33 |
}
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
raise HTTPException(status_code=500, detail=f"Erro interno: {str(e)}")
|
|
|
1 |
from fastapi import FastAPI, Query, HTTPException
|
2 |
+
from currency_converter import CurrencyConverter
|
3 |
+
from datetime import date
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
+
# Inicializa o CurrencyConverter
|
8 |
+
c = CurrencyConverter()
|
9 |
|
10 |
@app.get("/")
|
11 |
+
def welcome():
|
12 |
return {"message": "Bem-vindo 脿 API de Convers茫o de Moedas!"}
|
13 |
|
14 |
@app.get("/convert-currency/")
|
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 |
+
# Verifica se a data foi fornecida
|
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 |
+
"date": conversion_date if conversion_date else "Taxa mais recente"
|
37 |
}
|
38 |
+
|
39 |
+
except ValueError as e:
|
40 |
+
raise HTTPException(status_code=400, detail=str(e))
|
|