I wrote a python program in python(3.10) using the CSV module that gets some specific columns called "currency", "flight", "taxi", and "gym", with a few more. All this program does is checks if the value in the cell "currency" is equal to a set currency (like USD or RUB) and then does a conversion to USD of the following cells from that currency. It does this conversion through an API GET request (using the fixer API) that looks like this:
from_CAD_USD = f"https://api.apilayer.com/fixer/convert?to=USD&from=CAD&amount={flight}"
response = requests.request("GET", from_CAD_USD, headers=headers, data=payload)
status_code = response.status_code
result = response.json()
value = result["result"]
This specific request is taking in the variable "flight" and converting it from "CAD to "USD". My question is do I need to do this kind of get request for literally every currency I want to convert between and then write the same requests for the other columns? Cause as It's written now I would need for example:
CAD to USD for flights column
CAD to USD for taxi column #as taxis is a different value than flight being passed into amount
CAD to USD for gym column
so that is just one currency conversion, but now I would need to write the same for RUB currency, and MXN currency...etc, as it currently stands with only the flight conversion functionality working on about 4 rows, and its 12 get requests every time I run the program. That seems like too much. But I am a novice so I haven't dealt with something like this before. Is there a better way? I thought about instead of doing the conversion from the get request over and over, I could get one get request for lets say the current rate from CAD to USD of 1 dollar (1USD = .77CAD), getting the value of 20 currency types (so should only be 20 requests?) then using those values to multiply against the amount in the cell, effectively doing the conversion.
The reason I wanted the API to handle the conversion as opposed to doing some sort of floating point multiplication is in the past I have gotten inaccurate rounding and amounts of up to 9 dollars missing from the conversion. Any information on how to better manage API get requests is greatly appreciated. Thank you Community!