1- # TODO REVIEW BELOW CODE TO ENSURE THAT WHAT IS IN THIS MODULE FOR FUNCTION RECALLING CURRENCY FROM API
2- # Importing necessary modules for currency formatting and HTTP requests
3- from locale import currency
4- import requests
5- import json
1+ from api_handler import get_exchange_data
62
7- # Get the base currency input from the user and convert it to lowercase
8- currency = input ("Enter the base currency (e.g., USD, EUR): " ).lower ()
3+ # Returns a list of all supported currency codes.
4+ def get_supported_currencies (rates ):
5+ return list (rates .keys ())
96
10- # Initialize an empty cache to store exchange rates
11- cache = {}
7+ # Checks if a currency code is supported.
8+ def is_valid_currency (currency_code , rates ):
9+ return currency_code .upper () in rates
1210
13- # Infinite loop to process exchange requests until the user exits
14- while True :
11+ # --- DEBUG / MANUAL TEST SECTION ---
12+ # This section runs only when you run this file directly (not when imported elsewhere)
13+ if __name__ == "__main__" :
14+ # Fetch live exchange data from the API
15+ exchange_data = get_exchange_data ()
1516
16- # Get the target currency input from the user and convert it to lowercase
17- currency_exch = input ("Enter the currency to exchange to (leave blank to exit): " ).lower ()
17+ # Print the first 5 currencies for quick inspection
18+ print ("Sample of live rates from API:" )
19+ print (list (exchange_data ["rates" ].items ())[:5 ])
1820
19- # If the input is blank, break out of the loop (exit condition)
20- if currency_exch == '' :
21- break
21+ # Sample rates dictionary for local testing
22+ rates_example = {"USD" : 1.12 , "EUR" : 1.0 , "GBP" : 0.87 }
2223
23- # Get the amount to exchange from the user
24- amount_to_exch = int ( input ( "Enter the amount to exchange: " ))
24+ # Print supported currencies
25+ print ( " \n Supported currencies:" , get_supported_currencies ( rates_example ))
2526
26- # URL for getting exchange rates from floatrates.com
27- URL = f'http://www.floatrates.com/daily/{ currency } .json'
28-
29- # Fetch the exchange rates in JSON format
30- exch = json .loads (requests .get (URL ).text )
31-
32- # Update cache for USD and EUR based on the base currency
33- if currency == 'usd' :
34- # If base currency is USD, cache EUR rate
35- cache .update (eur = exch ['eur' ]['rate' ])
36- elif currency == 'eur' :
37- # If base currency is EUR, cache USD rate
38- cache .update (usd = exch ['usd' ]['rate' ])
39- else :
40- # For other base currencies, cache both USD and EUR rates
41- cache .update (usd = exch ['usd' ]['rate' ], eur = exch ['eur' ]['rate' ])
42-
43- print ("Checking the cache..." )
44-
45- # Check if the target currency's rate is in the cache
46- if currency_exch in cache :
47- # If the rate is in the cache, calculate the exchanged amount
48- rate = round (amount_to_exch * cache [currency_exch ], 2 )
49- print ("Oh! It is in the cache!" )
50- print (f"You received { rate } { currency_exch .upper ()} ." )
51- else :
52- # If the rate is not in the cache, fetch it from the exchange rates and store it in cache
53- print ("Sorry, but it is not in the cache!" )
54- cache [currency_exch ] = exch [currency_exch ]['rate' ]
55- rate = round (amount_to_exch * cache [currency_exch ], 2 )
56- print (f"You received { rate } { currency_exch .upper ()} ." )
27+ # Check a few currency codes
28+ print ("Is 'usd' valid?" , is_valid_currency ("usd" , rates_example )) # True
29+ print ("Is 'AUD' valid?" , is_valid_currency ("AUD" , rates_example )) # False
0 commit comments