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: 14
configured_endpoints: 15
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 --pre 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(
api_key=os.environ.get("BRAINBASE_API_KEY"), # This is the default and can be omitted
bearer_token="My Bearer Token",
)

client.workers.list()
```

While you can provide an `api_key` keyword argument,
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
to add `BRAINBASE_API_KEY="My API Key"` to your `.env` file
so that your API Key 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(
api_key=os.environ.get("BRAINBASE_API_KEY"), # 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
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Methods:

- <code title="post /api/workers">client.workers.<a href="./src/brainbase/resources/workers/workers.py">create</a>(\*\*<a href="src/brainbase/types/worker_create_params.py">params</a>) -> None</code>
- <code title="get /api/workers/{id}">client.workers.<a href="./src/brainbase/resources/workers/workers.py">retrieve</a>(id) -> None</code>
- <code title="post /api/workers/{id}">client.workers.<a href="./src/brainbase/resources/workers/workers.py">update</a>(id, \*\*<a href="src/brainbase/types/worker_update_params.py">params</a>) -> None</code>
- <code title="get /api/workers">client.workers.<a href="./src/brainbase/resources/workers/workers.py">list</a>() -> None</code>
- <code title="delete /api/workers/{id}">client.workers.<a href="./src/brainbase/resources/workers/workers.py">delete</a>(id) -> None</code>

Expand Down
52 changes: 20 additions & 32 deletions src/brainbase/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class Brainbase(SyncAPIClient):
with_streaming_response: BrainbaseWithStreamedResponse

# client options
api_key: str
bearer_token: str

def __init__(
self,
*,
api_key: str | None = None,
bearer_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
Expand All @@ -78,15 +78,15 @@ def __init__(
) -> None:
"""Construct a new synchronous brainbase client instance.

This automatically infers the `api_key` argument from the `BRAINBASE_API_KEY` environment variable if it is not provided.
This automatically infers the `bearer_token` argument from the `BEARER_TOKEN` environment variable if it is not provided.
"""
if api_key is None:
api_key = os.environ.get("BRAINBASE_API_KEY")
if api_key is None:
if bearer_token is None:
bearer_token = os.environ.get("BEARER_TOKEN")
if bearer_token is None:
raise BrainbaseError(
"The api_key client option must be set either by passing api_key to the client or by setting the BRAINBASE_API_KEY environment variable"
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the BEARER_TOKEN environment variable"
)
self.api_key = api_key
self.bearer_token = bearer_token

if base_url is None:
base_url = os.environ.get("BRAINBASE_BASE_URL")
Expand All @@ -113,12 +113,6 @@ def __init__(
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
api_key = self.api_key
return {"x-api-key": api_key}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
Expand All @@ -131,7 +125,7 @@ def default_headers(self) -> dict[str, str | Omit]:
def copy(
self,
*,
api_key: str | None = None,
bearer_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.Client | None = None,
Expand Down Expand Up @@ -165,7 +159,7 @@ def copy(

http_client = http_client or self._client
return self.__class__(
api_key=api_key or self.api_key,
bearer_token=bearer_token or self.bearer_token,
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
Expand Down Expand Up @@ -219,12 +213,12 @@ class AsyncBrainbase(AsyncAPIClient):
with_streaming_response: AsyncBrainbaseWithStreamedResponse

# client options
api_key: str
bearer_token: str

def __init__(
self,
*,
api_key: str | None = None,
bearer_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
Expand All @@ -246,15 +240,15 @@ def __init__(
) -> None:
"""Construct a new async brainbase client instance.

This automatically infers the `api_key` argument from the `BRAINBASE_API_KEY` environment variable if it is not provided.
This automatically infers the `bearer_token` argument from the `BEARER_TOKEN` environment variable if it is not provided.
"""
if api_key is None:
api_key = os.environ.get("BRAINBASE_API_KEY")
if api_key is None:
if bearer_token is None:
bearer_token = os.environ.get("BEARER_TOKEN")
if bearer_token is None:
raise BrainbaseError(
"The api_key client option must be set either by passing api_key to the client or by setting the BRAINBASE_API_KEY environment variable"
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the BEARER_TOKEN environment variable"
)
self.api_key = api_key
self.bearer_token = bearer_token

if base_url is None:
base_url = os.environ.get("BRAINBASE_BASE_URL")
Expand All @@ -281,12 +275,6 @@ def __init__(
def qs(self) -> Querystring:
return Querystring(array_format="comma")

@property
@override
def auth_headers(self) -> dict[str, str]:
api_key = self.api_key
return {"x-api-key": api_key}

@property
@override
def default_headers(self) -> dict[str, str | Omit]:
Expand All @@ -299,7 +287,7 @@ def default_headers(self) -> dict[str, str | Omit]:
def copy(
self,
*,
api_key: str | None = None,
bearer_token: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.AsyncClient | None = None,
Expand Down Expand Up @@ -333,7 +321,7 @@ def copy(

http_client = http_client or self._client
return self.__class__(
api_key=api_key or self.api_key,
bearer_token=bearer_token or self.bearer_token,
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
Expand Down
104 changes: 103 additions & 1 deletion src/brainbase/resources/workers/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
FlowsResourceWithStreamingResponse,
AsyncFlowsResourceWithStreamingResponse,
)
from ...types import worker_create_params
from ...types import worker_create_params, worker_update_params
from ..._types import NOT_GIVEN, Body, Query, Headers, NoneType, NotGiven
from ..._utils import (
maybe_transform,
Expand Down Expand Up @@ -143,6 +143,51 @@ def retrieve(
cast_to=NoneType,
)

def update(
self,
id: str,
*,
description: str | NotGiven = NOT_GIVEN,
name: str | NotGiven = NOT_GIVEN,
status: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> None:
"""
Update a worker

Args:
extra_headers: Send extra headers

extra_query: Add additional query parameters to the request

extra_body: Add additional JSON properties to the request

timeout: Override the client-level default timeout for this request, in seconds
"""
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
extra_headers = {"Accept": "*/*", **(extra_headers or {})}
return self._post(
f"/api/workers/{id}",
body=maybe_transform(
{
"description": description,
"name": name,
"status": status,
},
worker_update_params.WorkerUpdateParams,
),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
cast_to=NoneType,
)

def list(
self,
*,
Expand Down Expand Up @@ -302,6 +347,51 @@ async def retrieve(
cast_to=NoneType,
)

async def update(
self,
id: str,
*,
description: str | NotGiven = NOT_GIVEN,
name: str | NotGiven = NOT_GIVEN,
status: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> None:
"""
Update a worker

Args:
extra_headers: Send extra headers

extra_query: Add additional query parameters to the request

extra_body: Add additional JSON properties to the request

timeout: Override the client-level default timeout for this request, in seconds
"""
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
extra_headers = {"Accept": "*/*", **(extra_headers or {})}
return await self._post(
f"/api/workers/{id}",
body=await async_maybe_transform(
{
"description": description,
"name": name,
"status": status,
},
worker_update_params.WorkerUpdateParams,
),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
cast_to=NoneType,
)

async def list(
self,
*,
Expand Down Expand Up @@ -367,6 +457,9 @@ def __init__(self, workers: WorkersResource) -> None:
self.retrieve = to_raw_response_wrapper(
workers.retrieve,
)
self.update = to_raw_response_wrapper(
workers.update,
)
self.list = to_raw_response_wrapper(
workers.list,
)
Expand All @@ -393,6 +486,9 @@ def __init__(self, workers: AsyncWorkersResource) -> None:
self.retrieve = async_to_raw_response_wrapper(
workers.retrieve,
)
self.update = async_to_raw_response_wrapper(
workers.update,
)
self.list = async_to_raw_response_wrapper(
workers.list,
)
Expand All @@ -419,6 +515,9 @@ def __init__(self, workers: WorkersResource) -> None:
self.retrieve = to_streamed_response_wrapper(
workers.retrieve,
)
self.update = to_streamed_response_wrapper(
workers.update,
)
self.list = to_streamed_response_wrapper(
workers.list,
)
Expand All @@ -445,6 +544,9 @@ def __init__(self, workers: AsyncWorkersResource) -> None:
self.retrieve = async_to_streamed_response_wrapper(
workers.retrieve,
)
self.update = async_to_streamed_response_wrapper(
workers.update,
)
self.list = async_to_streamed_response_wrapper(
workers.list,
)
Expand Down
Loading