habulaj commited on
Commit
a116da6
1 Parent(s): 24bf72f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -1,37 +1,40 @@
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)}")
 
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))