From a7888eab8f8a8062389a1a6759e66214b31dcf4f Mon Sep 17 00:00:00 2001 From: hasdajustin Date: Fri, 20 Mar 2026 18:17:39 +0600 Subject: [PATCH 1/2] feat: add crypto price fetcher using httpx --- web_programming/fetch_crypto_prices.py | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 web_programming/fetch_crypto_prices.py diff --git a/web_programming/fetch_crypto_prices.py b/web_programming/fetch_crypto_prices.py new file mode 100644 index 000000000000..9ea7f7da252a --- /dev/null +++ b/web_programming/fetch_crypto_prices.py @@ -0,0 +1,41 @@ +""" +Fetch current cryptocurrency prices using the CoinGecko API. +This script uses the 'httpx' library as per the repository's preference. +""" + +from __future__ import annotations +import httpx + +def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]: + """ + Fetch the current price of a cryptocurrency in USD. + :param coin_id: The ID of the coin (e.g., 'bitcoin', 'ethereum', 'dogecoin') + :return: A dictionary containing the price data. + + >>> # Note: Actual API call results may vary over time. + >>> isinstance(fetch_crypto_price("bitcoin"), dict) + True + >>> "bitcoin" in fetch_crypto_price("bitcoin") + True + """ + url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd" + + with httpx.Client() as client: + response = client.get(url) + if response.status_code != 200: + raise ValueError(f"Could not fetch price for {coin_id}. Status code: {response.status_code}") + + data = response.json() + if not data: + raise ValueError(f"Invalid coin ID: {coin_id}") + + return data + +if __name__ == "__main__": + try: + coin = "bitcoin" + price_data = fetch_crypto_price(coin) + price = price_data[coin]["usd"] + print(f"The current price of {coin.capitalize()} is ${price:,.2f} USD") + except Exception as e: + print(f"Error: {e}") \ No newline at end of file From c4a839f7062abe90c8c5cd4550c110ec4778812f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Mar 2026 12:19:23 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/fetch_crypto_prices.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/web_programming/fetch_crypto_prices.py b/web_programming/fetch_crypto_prices.py index 9ea7f7da252a..7c0172f9ae2f 100644 --- a/web_programming/fetch_crypto_prices.py +++ b/web_programming/fetch_crypto_prices.py @@ -6,6 +6,7 @@ from __future__ import annotations import httpx + def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]: """ Fetch the current price of a cryptocurrency in USD. @@ -18,19 +19,24 @@ def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]: >>> "bitcoin" in fetch_crypto_price("bitcoin") True """ - url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd" - + url = ( + f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd" + ) + with httpx.Client() as client: response = client.get(url) if response.status_code != 200: - raise ValueError(f"Could not fetch price for {coin_id}. Status code: {response.status_code}") - + raise ValueError( + f"Could not fetch price for {coin_id}. Status code: {response.status_code}" + ) + data = response.json() if not data: raise ValueError(f"Invalid coin ID: {coin_id}") - + return data + if __name__ == "__main__": try: coin = "bitcoin" @@ -38,4 +44,4 @@ def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]: price = price_data[coin]["usd"] print(f"The current price of {coin.capitalize()} is ${price:,.2f} USD") except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f"Error: {e}")