@@ -83,26 +83,28 @@ You can enable this by installing `aiohttp`:
8383
8484``` sh
8585# install from PyPI
86- pip install coingecko_sdk[aiohttp]
86+ pip install " coingecko_sdk[aiohttp]"
8787```
8888
8989Then you can enable it by instantiating the client with ` http_client=DefaultAioHttpClient() ` :
9090
9191``` python
92+ import os
9293import asyncio
9394from coingecko_sdk import DefaultAioHttpClient
9495from coingecko_sdk import AsyncCoingecko
9596
9697
9798async def main () -> None :
9899 async with AsyncCoingecko(
99- pro_api_key = " My Pro API Key " ,
100+ pro_api_key = os.environ.get( " COINGECKO_PRO_API_KEY " ) ,
100101 http_client = DefaultAioHttpClient(),
101102 ) as client:
102103 price = await client.simple.price.get(
103104 vs_currencies = " usd" ,
104105 ids = " bitcoin" ,
105106 )
107+ print (price[" bitcoin" ].usd)
106108
107109
108110asyncio.run(main())
@@ -127,16 +129,22 @@ response), a subclass of `coingecko_sdk.APIStatusError` is raised, containing `s
127129All errors inherit from ` coingecko_sdk.APIError ` .
128130
129131``` python
132+ import os
130133import coingecko_sdk
131134from coingecko_sdk import Coingecko
132135
133- client = Coingecko()
136+ client = Coingecko(
137+ pro_api_key = os.environ.get(" COINGECKO_PRO_API_KEY" ),
138+ # demo_api_key=os.environ.get("COINGECKO_DEMO_API_KEY"), # Optional, for Demo API access
139+ environment = " pro" , # "demo" to initialize the client with Demo API access
140+ )
134141
135142try :
136- client.simple.price.get(
143+ price = client.simple.price.get(
137144 vs_currencies = " usd" ,
138145 ids = " bitcoin" ,
139146 )
147+ print (price[" bitcoin" ].usd)
140148except coingecko_sdk.APIConnectionError as e:
141149 print (" The server could not be reached" )
142150 print (e.__cause__) # an underlying Exception, likely raised within httpx.
@@ -170,19 +178,24 @@ Connection errors (for example, due to a network connectivity problem), 408 Requ
170178You can use the ` max_retries ` option to configure or disable retry settings:
171179
172180``` python
181+ import os
173182from coingecko_sdk import Coingecko
174183
175184# Configure the default for all requests:
176185client = Coingecko(
177- # default is 2
178- max_retries = 0 ,
186+ pro_api_key = os.environ.get(" COINGECKO_PRO_API_KEY" ),
187+ # demo_api_key=os.environ.get("COINGECKO_DEMO_API_KEY"), # Optional, for Demo API access
188+ environment = " pro" , # "demo" to initialize the client with Demo API access
189+
190+ max_retries = 0 , # default is 2
179191)
180192
181193# Or, configure per-request:
182- client.with_options(max_retries = 5 ).simple.price.get(
194+ price = client.with_options(max_retries = 5 ).simple.price.get(
183195 vs_currencies = " usd" ,
184196 ids = " bitcoin" ,
185197)
198+ print (price[" bitcoin" ].usd)
186199```
187200
188201### Timeouts
@@ -191,24 +204,29 @@ By default requests time out after 1 minute. You can configure this with a `time
191204which accepts a float or an [ ` httpx.Timeout ` ] ( https://www.python-httpx.org/advanced/timeouts/#fine-tuning-the-configuration ) object:
192205
193206``` python
207+ import os
194208from coingecko_sdk import Coingecko
195209
196210# Configure the default for all requests:
197211client = Coingecko(
198- # 20 seconds (default is 1 minute)
199- timeout = 20.0 ,
212+ pro_api_key = os.environ.get(" COINGECKO_PRO_API_KEY" ),
213+ # demo_api_key=os.environ.get("COINGECKO_DEMO_API_KEY"), # Optional, for Demo API access
214+ environment = " pro" , # "demo" to initialize the client with Demo API access
215+
216+ timeout = 20.0 , # 20 seconds (default is 1 minute)
200217)
201218
202219# More granular control:
203- client = Coingecko(
204- timeout = httpx.Timeout(60.0 , read = 5.0 , write = 10.0 , connect = 2.0 ),
205- )
220+ # client = Coingecko(
221+ # timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
222+ # )
206223
207224# Override per-request:
208- client.with_options(timeout = 5.0 ).simple.price.get(
225+ price = client.with_options(timeout = 5.0 ).simple.price.get(
209226 vs_currencies = " usd" ,
210227 ids = " bitcoin" ,
211228)
229+ print (price[" bitcoin" ].usd)
212230```
213231
214232On timeout, an ` APITimeoutError ` is thrown.
@@ -246,14 +264,19 @@ if response.my_field is None:
246264The "raw" Response object can be accessed by prefixing ` .with_raw_response. ` to any HTTP method call, e.g.,
247265
248266``` py
267+ import os
249268from coingecko_sdk import Coingecko
250269
251- client = Coingecko()
270+ client = Coingecko(
271+ pro_api_key = os.environ.get(" COINGECKO_PRO_API_KEY" ),
272+ # demo_api_key=os.environ.get("COINGECKO_DEMO_API_KEY"), # Optional, for Demo API access
273+ environment = " pro" , # "demo" to initialize the client with Demo API access
274+ )
252275response = client.simple.price.with_raw_response.get(
253276 vs_currencies = " usd" ,
254277 ids = " bitcoin" ,
255278)
256- print (response.headers.get(' X-My-Header ' ))
279+ print (response.headers.get(' YOUR-HEADER-NAME ' ))
257280
258281price_data = response.parse() # get the object that `simple.price.get()` would have returned
259282print (price_data[' bitcoin' ].usd)
@@ -274,7 +297,7 @@ with client.simple.price.with_streaming_response.get(
274297 vs_currencies = " usd" ,
275298 ids = " bitcoin" ,
276299) as response:
277- print (response.headers.get(" X-My-Header " ))
300+ print (response.headers.get(" YOUR-HEADER-NAME " ))
278301
279302 for line in response.iter_lines():
280303 print (line)
0 commit comments