Skip to content

Commit c4a839f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent a7888ea commit c4a839f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

web_programming/fetch_crypto_prices.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77
import httpx
88

9+
910
def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]:
1011
"""
1112
Fetch the current price of a cryptocurrency in USD.
@@ -18,24 +19,29 @@ def fetch_crypto_price(coin_id: str = "bitcoin") -> dict[str, dict[str, float]]:
1819
>>> "bitcoin" in fetch_crypto_price("bitcoin")
1920
True
2021
"""
21-
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
22-
22+
url = (
23+
f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
24+
)
25+
2326
with httpx.Client() as client:
2427
response = client.get(url)
2528
if response.status_code != 200:
26-
raise ValueError(f"Could not fetch price for {coin_id}. Status code: {response.status_code}")
27-
29+
raise ValueError(
30+
f"Could not fetch price for {coin_id}. Status code: {response.status_code}"
31+
)
32+
2833
data = response.json()
2934
if not data:
3035
raise ValueError(f"Invalid coin ID: {coin_id}")
31-
36+
3237
return data
3338

39+
3440
if __name__ == "__main__":
3541
try:
3642
coin = "bitcoin"
3743
price_data = fetch_crypto_price(coin)
3844
price = price_data[coin]["usd"]
3945
print(f"The current price of {coin.capitalize()} is ${price:,.2f} USD")
4046
except Exception as e:
41-
print(f"Error: {e}")
47+
print(f"Error: {e}")

0 commit comments

Comments
 (0)