Skip to content

Commit 4dff7d2

Browse files
authored
Update crypto_price_tracker to use httpx2
1 parent 0afa947 commit 4dff7d2

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

web_programming/crypto_price_tracker.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
Fetch the current price of a cryptocurrency in USD using CoinGecko API.
33
"""
44

5-
import httpx
5+
# /// script
6+
# requires-python = ">=3.11"
7+
# dependencies = [
8+
# "httpx2",
9+
# ]
10+
# ///
11+
12+
import httpx2
613

714

815
def crypto_price(coin: str = "bitcoin") -> float:
@@ -16,12 +23,12 @@ def crypto_price(coin: str = "bitcoin") -> float:
1623
"""
1724
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin}&vs_currencies=usd"
1825
try:
19-
response = httpx.get(url, timeout=10)
20-
response.raise_for_status()
26+
json_response = httpx2.get(url, timeout=10).raise_for_status().json()
2127
return float(response.json().get(coin, {}).get("usd", 0.0))
22-
except (httpx.RequestError, ValueError, KeyError):
28+
except (httpx2.RequestError, ValueError, KeyError):
2329
return 0.0
30+
return float(json_response.get(coin, {}).get("usd", 0.0))
2431

2532

2633
if __name__ == "__main__":
27-
print(crypto_price("bitcoin"))
34+
print(f"{crypto_price('bitcoin') = }")

0 commit comments

Comments
 (0)