habulaj commited on
Commit
fd746a8
1 Parent(s): d02a5f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
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("/convert/")
9
- def convert_currency(amount: float = Query(..., description="Quantia a ser convertida"),
10
- from_currency: str = Query(..., description="Moeda de origem"),
11
- to_currency: str = Query(..., description="Moeda de destino")):
 
 
 
 
 
 
12
  """
13
- Converte uma quantia de uma moeda para outra.
14
  """
15
- converted_amount = currency_rates.convert(from_currency, to_currency, amount)
16
- return {
17
- "amount": amount,
18
- "from_currency": from_currency,
19
- "to_currency": to_currency,
20
- "converted_amount": converted_amount
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)}")