File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22Fetch 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
815def 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
2633if __name__ == "__main__" :
27- print (crypto_price (" bitcoin" ) )
34+ print (f" { crypto_price (' bitcoin' ) = } " )
You can’t perform that action at this time.
0 commit comments