I feel I have to many GET requests, how do I use the api more efficiently (python)

Viewed 32

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!

1 Answers

One option, and probably the option that I would choose, would be to use the /latest API endpoint to get the exchange rates, and then do the conversion calculations yourself. With the API's /latest endpoint you can get the exchange rate from a single currency to all others in a single get request.

You can then use the convert endpoint to test some of your results to make sure they match correctly.

The other option is to continue to do it the way you are currently with some sort of caching or memoization so that you know you aren't making duplicate requests.

Here is an example of getting the CAD to USD exchange rate and several others as well.

url = "https://api.apilayer.com/fixer/latest"
params = {"symbols":["USD", "MXN", "RUB"], "base": "CAD"}
payload = {}
headers= {
  "apikey": "{API-KEY}"
}
response = requests.get(url,headers=headers,params=params,payload=payload)
result = response.json()
from_CAD_USD = result["rates"]["USD"]

If each and every value should be 100% identical to the output of the convert endpoint. then this strategy probably isn't the better option.

Related