File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,4 +60,4 @@ def delete_usuario(id):
6060@api .route ('/exchange/usd-to-brl' , methods = ['GET' ])
6161def usd_to_brl ():
6262 result = get_usd_to_brl ()
63- return jsonify (result )
63+ return jsonify (result ), ( 200 if result . get ( "usd" ) else 503 )
Original file line number Diff line number Diff line change 22import requests
33
44def 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 )}
You can’t perform that action at this time.
0 commit comments