Skip to content

Instantly share code, notes, and snippets.

@vinodjayachandran
Created April 12, 2024 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinodjayachandran/10dbc55418a5e4291752b43f66103056 to your computer and use it in GitHub Desktop.
Save vinodjayachandran/10dbc55418a5e4291752b43f66103056 to your computer and use it in GitHub Desktop.
Python Script to fetch Open exchange rates
import requests
import os
def fetchExchangeRate(currencyDate, baseCurrency, currencySymbol):
"""
Method to fetch open Currency exchange rate of base currency (1 Unit) to a currencySymbol
Reference : https://docs.openexchangerates.org/reference/api-introduction
Args:
currencyDate: Date for which you need open exchange rate.
baseCurrency: ISO currency code
currencySymbol: ISO currency codes
Returns:
JSON response of Open Exchange Rate
"""
url = "https://openexchangerates.org/api/historical/{currencyDate}.json?base={base}&symbols={symbol}".format(currencyDate=currencyDate, base=baseCurrency, symbol=currencySymbol)
open_exchange_token = os.environ.get("OPEN_EXCHANGE_TOKEN")
headers = {
"Authorization": f"Token {open_exchange_token}", # Replace with your API token or other headers
"Content-Type": "application/json", # Adjust as needed
}
print(url)
response = requests.get(url, headers=headers)
if response.status_code == 200:
# Process successful response
data = response.json() # If the response is JSON
print(data)
else:
# Handle error
print("Request failed with status code:", response.status_code)
print("Response text:", response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment