This project fetches cryptocurrency prices (Bitcoin, Ethereum, and DAI) from a public API every hour and commits the data to this repository. It uses GitHub Actions to automate the process.
Retry Mechanism: Added a retry strategy using requests.adapters.HTTPAdapter and urllib3.util.retry.Retry. This retries failed requests up to 3 times with exponential backoff, handling network issues or server-side errors (e.g., 429, 5xx errors).
Logging Implementation: Replaced all print statements with Python’s logging module, allowing messages to be logged at various levels (INFO, ERROR, etc.). This provides more control over log output and improves debugging.
Environment Variable Check: Added a check to verify if BASE_URL is set in the environment variables before making the API request. If it’s missing, an error is logged.
Session Object for Requests: Introduced a requests.Session object to improve performance by reusing connections and to easily attach the retry mechanism.
Dynamic Crypto List: Refactored the list of cryptocurrency IDs (dai, ethereum, bitcoin) to be passed as an argument (crypto_ids) to the fetch_crypto_prices function. This allows the function to work with any set of cryptocurrencies.
Response Validation: Added validation to ensure that all requested cryptocurrencies exist in the API response before saving the data. This prevents saving partial or incorrect data.
Timestamp in Output Filename: The output JSON filename now includes a timestamp (crypto_prices_YYYY-MM-DD_HH-MM-SS.json), making it unique and easy to track when the data was fetched.
Error Handling: Improved error handling by catching both `requests error messages for each.
Docstring: Added a docstring to fetch_crypto_prices() to provide clear documentation of its purpose and arguments.
The changes make the code more reliable, flexible, and easier to maintain or troubleshoot.
Retry Mechanism: Added a retry strategy using
requests.adapters.HTTPAdapter
andurllib3.util.retry.Retry
. This retries failed requests up to 3 times with exponential backoff, handling network issues or server-side errors (e.g., 429, 5xx errors).Logging Implementation: Replaced all
print
statements with Python’slogging
module, allowing messages to be logged at various levels (INFO
,ERROR
, etc.). This provides more control over log output and improves debugging.Environment Variable Check: Added a check to verify if
BASE_URL
is set in the environment variables before making the API request. If it’s missing, an error is logged.Session Object for Requests: Introduced a
requests.Session
object to improve performance by reusing connections and to easily attach the retry mechanism.Dynamic Crypto List: Refactored the list of cryptocurrency IDs (
dai
,ethereum
,bitcoin
) to be passed as an argument (crypto_ids
) to thefetch_crypto_prices
function. This allows the function to work with any set of cryptocurrencies.Response Validation: Added validation to ensure that all requested cryptocurrencies exist in the API response before saving the data. This prevents saving partial or incorrect data.
Timestamp in Output Filename: The output JSON filename now includes a timestamp (
crypto_prices_YYYY-MM-DD_HH-MM-SS.json
), making it unique and easy to track when the data was fetched.Error Handling: Improved error handling by catching both `requests error messages for each.
Docstring: Added a docstring to
fetch_crypto_prices()
to provide clear documentation of its purpose and arguments.The changes make the code more reliable, flexible, and easier to maintain or troubleshoot.