Skip to content

Commit 7dd48a6

Browse files
feat(api): update via SDK Studio (#35)
1 parent fca87c0 commit 7dd48a6

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
configured_endpoints: 15
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/brainbase-egrigokhan%2Fbrainbase-a197a88d1abbdbd5c886875d2cf12ffd6abf79aec7d4dbd0439ed50197692eba.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/brainbase-egrigokhan%2Fbrainbase-0c00485d66a3b7505f3247467ef293fa5fb43a64e90a8b03a4127a2d9b15e6ab.yml

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,32 @@ pip install brainbase-labs
2424
The full API of this library can be found in [api.md](api.md).
2525

2626
```python
27+
import os
2728
from brainbase import Brainbase
2829

2930
client = Brainbase(
30-
api_key="My API Key",
31+
api_key=os.environ.get("BRAINBASE_API_KEY"), # This is the default and can be omitted
3132
)
3233

3334
client.workers.list()
3435
```
3536

37+
While you can provide an `api_key` keyword argument,
38+
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
39+
to add `BRAINBASE_API_KEY="My API Key"` to your `.env` file
40+
so that your API Key is not stored in source control.
41+
3642
## Async usage
3743

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

4046
```python
47+
import os
4148
import asyncio
4249
from brainbase import AsyncBrainbase
4350

4451
client = AsyncBrainbase(
45-
api_key="My API Key",
52+
api_key=os.environ.get("BRAINBASE_API_KEY"), # This is the default and can be omitted
4653
)
4754

4855

@@ -77,9 +84,7 @@ All errors inherit from `brainbase.APIError`.
7784
import brainbase
7885
from brainbase import Brainbase
7986

80-
client = Brainbase(
81-
api_key="My API Key",
82-
)
87+
client = Brainbase()
8388

8489
try:
8590
client.workers.list()
@@ -122,7 +127,6 @@ from brainbase import Brainbase
122127
client = Brainbase(
123128
# default is 2
124129
max_retries=0,
125-
api_key="My API Key",
126130
)
127131

128132
# Or, configure per-request:
@@ -141,13 +145,11 @@ from brainbase import Brainbase
141145
client = Brainbase(
142146
# 20 seconds (default is 1 minute)
143147
timeout=20.0,
144-
api_key="My API Key",
145148
)
146149

147150
# More granular control:
148151
client = Brainbase(
149152
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
150-
api_key="My API Key",
151153
)
152154

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

194-
client = Brainbase(
195-
api_key="My API Key",
196-
)
196+
client = Brainbase()
197197
response = client.workers.with_raw_response.list()
198198
print(response.headers.get('X-My-Header'))
199199

@@ -274,7 +274,6 @@ client = Brainbase(
274274
proxy="http://my.test.proxy.example.com",
275275
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
276276
),
277-
api_key="My API Key",
278277
)
279278
```
280279

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

294-
with Brainbase(
295-
api_key="My API Key",
296-
) as client:
293+
with Brainbase() as client:
297294
# make requests here
298295
...
299296

src/brainbase/_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def __init__(
113113
def qs(self) -> Querystring:
114114
return Querystring(array_format="comma")
115115

116+
@property
117+
@override
118+
def auth_headers(self) -> dict[str, str]:
119+
api_key = self.api_key
120+
return {"x-api-key": api_key}
121+
116122
@property
117123
@override
118124
def default_headers(self) -> dict[str, str | Omit]:
@@ -275,6 +281,12 @@ def __init__(
275281
def qs(self) -> Querystring:
276282
return Querystring(array_format="comma")
277283

284+
@property
285+
@override
286+
def auth_headers(self) -> dict[str, str]:
287+
api_key = self.api_key
288+
return {"x-api-key": api_key}
289+
278290
@property
279291
@override
280292
def default_headers(self) -> dict[str, str | Omit]:

tests/test_client.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from brainbase._types import Omit
2626
from brainbase._models import BaseModel, FinalRequestOptions
2727
from brainbase._constants import RAW_RESPONSE_HEADER
28-
from brainbase._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
28+
from brainbase._exceptions import APIStatusError, BrainbaseError, APITimeoutError, APIResponseValidationError
2929
from brainbase._base_client import (
3030
DEFAULT_TIMEOUT,
3131
HTTPX_DEFAULT_TIMEOUT,
@@ -334,6 +334,16 @@ def test_default_headers_option(self) -> None:
334334
assert request.headers.get("x-foo") == "stainless"
335335
assert request.headers.get("x-stainless-lang") == "my-overriding-header"
336336

337+
def test_validate_headers(self) -> None:
338+
client = Brainbase(base_url=base_url, api_key=api_key, _strict_response_validation=True)
339+
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
340+
assert request.headers.get("x-api-key") == api_key
341+
342+
with pytest.raises(BrainbaseError):
343+
with update_env(**{"BRAINBASE_API_KEY": Omit()}):
344+
client2 = Brainbase(base_url=base_url, api_key=None, _strict_response_validation=True)
345+
_ = client2
346+
337347
def test_default_query_option(self) -> None:
338348
client = Brainbase(
339349
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_query={"query_param": "bar"}
@@ -1080,6 +1090,16 @@ def test_default_headers_option(self) -> None:
10801090
assert request.headers.get("x-foo") == "stainless"
10811091
assert request.headers.get("x-stainless-lang") == "my-overriding-header"
10821092

1093+
def test_validate_headers(self) -> None:
1094+
client = AsyncBrainbase(base_url=base_url, api_key=api_key, _strict_response_validation=True)
1095+
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
1096+
assert request.headers.get("x-api-key") == api_key
1097+
1098+
with pytest.raises(BrainbaseError):
1099+
with update_env(**{"BRAINBASE_API_KEY": Omit()}):
1100+
client2 = AsyncBrainbase(base_url=base_url, api_key=None, _strict_response_validation=True)
1101+
_ = client2
1102+
10831103
def test_default_query_option(self) -> None:
10841104
client = AsyncBrainbase(
10851105
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_query={"query_param": "bar"}

0 commit comments

Comments
 (0)