Skip to content

Commit ea3581d

Browse files
committed
fix(api): adicionar tratamento de erro e resposta 503 ao endpoint de câmbio
1 parent 7b45e4c commit ea3581d

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

src/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ def delete_usuario(id):
6060
@api.route('/exchange/usd-to-brl', methods=['GET'])
6161
def usd_to_brl():
6262
result = get_usd_to_brl()
63-
return jsonify(result)
63+
return jsonify(result), (200 if result.get("usd") else 503)

src/services/exchange_service.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
import requests
33

44
def get_usd_to_brl():
5-
"""
6-
Chama a API externa e retorna a cotação (bid) do USD para BRL.
7-
Retorna dicionário com a chave "usd" (pode ser None em caso de erro).
8-
"""
95
url = "https://economia.awesomeapi.com.br/json/last/USD-BRL"
10-
response = requests.get(url)
11-
data = response.json()
12-
13-
# pega com segurança: evita KeyError se estrutura mudar
14-
bid = data.get("USDBRL", {}).get("bid")
15-
return {"usd": bid}
6+
try:
7+
response = requests.get(url, timeout=5)
8+
response.raise_for_status()
9+
data = response.json()
10+
bid = data.get("USDBRL", {}).get("bid")
11+
return {"usd": bid}
12+
except Exception as e:
13+
return {"usd": None, "error": str(e)}

0 commit comments

Comments
 (0)