Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 15
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/brainbase-egrigokhan%2Fbrainbase-a197a88d1abbdbd5c886875d2cf12ffd6abf79aec7d4dbd0439ed50197692eba.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/brainbase-egrigokhan%2Fbrainbase-0c00485d66a3b7505f3247467ef293fa5fb43a64e90a8b03a4127a2d9b15e6ab.yml
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,25 @@ pip install brainbase-labs
The full API of this library can be found in [api.md](api.md).

```python
import os
from brainbase import Brainbase

client = Brainbase(
bearer_token=os.environ.get("BEARER_TOKEN"), # This is the default and can be omitted
bearer_token="My Bearer Token",
)

client.workers.list()
```

While you can provide a `bearer_token` keyword argument,
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
to add `BEARER_TOKEN="My Bearer Token"` to your `.env` file
so that your Bearer Token is not stored in source control.

## Async usage

Simply import `AsyncBrainbase` instead of `Brainbase` and use `await` with each API call:

```python
import os
import asyncio
from brainbase import AsyncBrainbase

client = AsyncBrainbase(
bearer_token=os.environ.get("BEARER_TOKEN"), # This is the default and can be omitted
bearer_token="My Bearer Token",
)


Expand Down Expand Up @@ -84,7 +77,9 @@ All errors inherit from `brainbase.APIError`.
import brainbase
from brainbase import Brainbase

client = Brainbase()
client = Brainbase(
bearer_token="My Bearer Token",
)

try:
client.workers.list()
Expand Down Expand Up @@ -127,6 +122,7 @@ from brainbase import Brainbase
client = Brainbase(
# default is 2
max_retries=0,
bearer_token="My Bearer Token",
)

# Or, configure per-request:
Expand All @@ -145,11 +141,13 @@ from brainbase import Brainbase
client = Brainbase(
# 20 seconds (default is 1 minute)
timeout=20.0,
bearer_token="My Bearer Token",
)

# More granular control:
client = Brainbase(
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
bearer_token="My Bearer Token",
)

# Override per-request:
Expand Down Expand Up @@ -193,7 +191,9 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
```py
from brainbase import Brainbase

client = Brainbase()
client = Brainbase(
bearer_token="My Bearer Token",
)
response = client.workers.with_raw_response.list()
print(response.headers.get('X-My-Header'))

Expand Down Expand Up @@ -274,6 +274,7 @@ client = Brainbase(
proxy="http://my.test.proxy.example.com",
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
),
bearer_token="My Bearer Token",
)
```

Expand All @@ -290,7 +291,9 @@ By default the library closes underlying HTTP connections whenever the client is
```py
from brainbase import Brainbase

with Brainbase() as client:
with Brainbase(
bearer_token="My Bearer Token",
) as client:
# make requests here
...

Expand Down
12 changes: 0 additions & 12 deletions src/brainbase/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ def __init__(
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
bearer_token = self.bearer_token
return {"Authorization": f"Bearer {bearer_token}"}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
Expand Down Expand Up @@ -281,12 +275,6 @@ def __init__(
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
bearer_token = self.bearer_token
return {"Authorization": f"Bearer {bearer_token}"}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
Expand Down
22 changes: 1 addition & 21 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from brainbase._types import Omit
from brainbase._models import BaseModel, FinalRequestOptions
from brainbase._constants import RAW_RESPONSE_HEADER
from brainbase._exceptions import APIStatusError, BrainbaseError, APITimeoutError, APIResponseValidationError
from brainbase._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
from brainbase._base_client import (
DEFAULT_TIMEOUT,
HTTPX_DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -340,16 +340,6 @@ def test_default_headers_option(self) -> None:
assert request.headers.get("x-foo") == "stainless"
assert request.headers.get("x-stainless-lang") == "my-overriding-header"

def test_validate_headers(self) -> None:
client = Brainbase(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=True)
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
assert request.headers.get("Authorization") == f"Bearer {bearer_token}"

with pytest.raises(BrainbaseError):
with update_env(**{"BEARER_TOKEN": Omit()}):
client2 = Brainbase(base_url=base_url, bearer_token=None, _strict_response_validation=True)
_ = client2

def test_default_query_option(self) -> None:
client = Brainbase(
base_url=base_url,
Expand Down Expand Up @@ -1124,16 +1114,6 @@ def test_default_headers_option(self) -> None:
assert request.headers.get("x-foo") == "stainless"
assert request.headers.get("x-stainless-lang") == "my-overriding-header"

def test_validate_headers(self) -> None:
client = AsyncBrainbase(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=True)
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
assert request.headers.get("Authorization") == f"Bearer {bearer_token}"

with pytest.raises(BrainbaseError):
with update_env(**{"BEARER_TOKEN": Omit()}):
client2 = AsyncBrainbase(base_url=base_url, bearer_token=None, _strict_response_validation=True)
_ = client2

def test_default_query_option(self) -> None:
client = AsyncBrainbase(
base_url=base_url,
Expand Down