@@ -11,7 +11,7 @@ It is generated with [Stainless](https://www.stainless.com/).
1111
1212## Documentation
1313
14- The REST API documentation can be found on [ brapi.dev] ( https://brapi.dev ) . The full API of this library can be found in [ api.md] ( api.md ) .
14+ The REST API documentation can be found on [ brapi.dev] ( https://brapi.dev/docs ) . The full API of this library can be found in [ api.md] ( api.md ) .
1515
1616## Installation
1717
@@ -25,40 +25,43 @@ pip install brapi
2525The full API of this library can be found in [ api.md] ( api.md ) .
2626
2727``` python
28+ import os
2829from brapi import Brapi
2930
3031client = Brapi(
31- api_key = " My API Key" ,
32- # defaults to "production".
33- environment = " environment_1" ,
32+ api_key = os.environ.get(" BRAPI_API_KEY" ), # This is the default and can be omitted
3433)
3534
36- quote = client.quote.retrieve (
37- tickers = " REPLACE_ME " ,
35+ response = client.v2.stocks.quote (
36+ symbols = " PETR4 " ,
3837)
39- print (quote.guidance )
38+ print (response.requested_at )
4039```
4140
41+ While you can provide an ` api_key ` keyword argument,
42+ we recommend using [ python-dotenv] ( https://pypi.org/project/python-dotenv/ )
43+ to add ` BRAPI_API_KEY="My API Key" ` to your ` .env ` file
44+ so that your API Key is not stored in source control.
45+
4246## Async usage
4347
4448Simply import ` AsyncBrapi ` instead of ` Brapi ` and use ` await ` with each API call:
4549
4650``` python
51+ import os
4752import asyncio
4853from brapi import AsyncBrapi
4954
5055client = AsyncBrapi(
51- api_key = " My API Key" ,
52- # defaults to "production".
53- environment = " environment_1" ,
56+ api_key = os.environ.get(" BRAPI_API_KEY" ), # This is the default and can be omitted
5457)
5558
5659
5760async def main () -> None :
58- quote = await client.quote.retrieve (
59- tickers = " REPLACE_ME " ,
61+ response = await client.v2.stocks.quote (
62+ symbols = " PETR4 " ,
6063 )
61- print (quote.guidance )
64+ print (response.requested_at )
6265
6366
6467asyncio.run(main())
@@ -80,20 +83,21 @@ pip install brapi[aiohttp]
8083Then you can enable it by instantiating the client with ` http_client=DefaultAioHttpClient() ` :
8184
8285``` python
86+ import os
8387import asyncio
8488from brapi import DefaultAioHttpClient
8589from brapi import AsyncBrapi
8690
8791
8892async def main () -> None :
8993 async with AsyncBrapi(
90- api_key = " My API Key " ,
94+ api_key = os.environ.get( " BRAPI_API_KEY " ), # This is the default and can be omitted
9195 http_client = DefaultAioHttpClient(),
9296 ) as client:
93- quote = await client.quote.retrieve (
94- tickers = " REPLACE_ME " ,
97+ response = await client.v2.stocks.quote (
98+ symbols = " PETR4 " ,
9599 )
96- print (quote.guidance )
100+ print (response.requested_at )
97101
98102
99103asyncio.run(main())
@@ -121,13 +125,11 @@ All errors inherit from `brapi.APIError`.
121125import brapi
122126from brapi import Brapi
123127
124- client = Brapi(
125- api_key = " My API Key" ,
126- )
128+ client = Brapi()
127129
128130try :
129- client.quote.retrieve (
130- tickers = " REPLACE_ME " ,
131+ client.v2.stocks.quote (
132+ symbols = " PETR4 " ,
131133 )
132134except brapi.APIConnectionError as e:
133135 print (" The server could not be reached" )
@@ -166,14 +168,13 @@ from brapi import Brapi
166168
167169# Configure the default for all requests:
168170client = Brapi(
169- api_key = " My API Key" ,
170171 # default is 2
171172 max_retries = 0 ,
172173)
173174
174175# Or, configure per-request:
175- client.with_options(max_retries = 5 ).quote.retrieve (
176- tickers = " REPLACE_ME " ,
176+ client.with_options(max_retries = 5 ).v2.stocks.quote (
177+ symbols = " PETR4 " ,
177178)
178179```
179180
@@ -187,20 +188,18 @@ from brapi import Brapi
187188
188189# Configure the default for all requests:
189190client = Brapi(
190- api_key = " My API Key" ,
191191 # 20 seconds (default is 1 minute)
192192 timeout = 20.0 ,
193193)
194194
195195# More granular control:
196196client = Brapi(
197- api_key = " My API Key" ,
198197 timeout = httpx.Timeout(60.0 , read = 5.0 , write = 10.0 , connect = 2.0 ),
199198)
200199
201200# Override per-request:
202- client.with_options(timeout = 5.0 ).quote.retrieve (
203- tickers = " REPLACE_ME " ,
201+ client.with_options(timeout = 5.0 ).v2.stocks.quote (
202+ symbols = " PETR4 " ,
204203)
205204```
206205
@@ -241,16 +240,14 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
241240``` py
242241from brapi import Brapi
243242
244- client = Brapi(
245- api_key = " My API Key" ,
246- )
247- response = client.quote.with_raw_response.retrieve(
248- tickers = " REPLACE_ME" ,
243+ client = Brapi()
244+ response = client.v2.stocks.with_raw_response.quote(
245+ symbols = " PETR4" ,
249246)
250247print (response.headers.get(' X-My-Header' ))
251248
252- quote = response.parse() # get the object that `quote.retrieve ()` would have returned
253- print (quote.guidance )
249+ stock = response.parse() # get the object that `v2.stocks.quote ()` would have returned
250+ print (stock.requested_at )
254251```
255252
256253These methods return an [ ` APIResponse ` ] ( https://github.com/brapi-dev/brapi-python/tree/main/src/brapi/_response.py ) object.
@@ -264,8 +261,8 @@ The above interface eagerly reads the full response body when you make the reque
264261To stream the response body, use ` .with_streaming_response ` instead, which requires a context manager and only reads the response body once you call ` .read() ` , ` .text() ` , ` .json() ` , ` .iter_bytes() ` , ` .iter_text() ` , ` .iter_lines() ` or ` .parse() ` . In the async client, these are async methods.
265262
266263``` python
267- with client.quote. with_streaming_response.retrieve (
268- tickers = " REPLACE_ME " ,
264+ with client.v2.stocks. with_streaming_response.quote (
265+ symbols = " PETR4 " ,
269266) as response:
270267 print (response.headers.get(" X-My-Header" ))
271268
@@ -322,7 +319,6 @@ import httpx
322319from brapi import Brapi, DefaultHttpxClient
323320
324321client = Brapi(
325- api_key = " My API Key" ,
326322 # Or use the `BRAPI_BASE_URL` env var
327323 base_url = " http://my.test.server.example.com:8083" ,
328324 http_client = DefaultHttpxClient(
@@ -345,9 +341,7 @@ By default the library closes underlying HTTP connections whenever the client is
345341``` py
346342from brapi import Brapi
347343
348- with Brapi(
349- api_key = " My API Key" ,
350- ) as client:
344+ with Brapi() as client:
351345 # make requests here
352346 ...
353347
0 commit comments