File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change 11import requests
2- import json
32
4- def get_exchange_data (api_url : str = "https://theratesapi. com/api /latest/ " ) -> dict :
3+ def get_exchange_data (api_url : str = "https://open.er-api. com/v6 /latest" ) -> dict :
54 """Fetch latest exchange data from the API."""
65 response = requests .get (api_url )
76 if response .status_code != 200 :
87 raise Exception (f"API request failed with status { response .status_code } " )
98
109 data = response .json ()
11- return data # This includes 'base', 'date', and 'rates'
10+ # Ensure response was successful
11+ if data .get ("result" ) != "success" :
12+ raise Exception (f"API returned error: { data .get ('error-type' , 'Unknown error' )} " )
13+
14+ return data # Includes 'base_code', 'time_last_update_utc', and 'rates'
1215
1316# NOTE - for logging & debugging
1417if __name__ == "__main__" :
1518 exchange_data = get_exchange_data ()
16- print ("Base currency:" , exchange_data ["base " ])
17- print ("Date:" , exchange_data ["date " ])
19+ print ("Base currency:" , exchange_data ["base_code " ])
20+ print ("Date:" , exchange_data ["time_last_update_utc " ])
1821 print ("Rates:" , list (exchange_data ["rates" ].items ())[:5 ])
You can’t perform that action at this time.
0 commit comments