Skip to content

Commit 42cc16f

Browse files
feat(api): OpenAPI spec update via Stainless API (#20)
1 parent 267c755 commit 42cc16f

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ pip install git+ssh://git@github.com/lumalabs/lumaai-python.git
2525
The full API of this library can be found in [api.md](api.md).
2626

2727
```python
28+
import os
2829
from lumaai import Lumaai
2930

3031
client = Lumaai(
31-
auth_token="My Auth Token",
32+
# This is the default and can be omitted
33+
auth_token=os.environ.get("LUMAAI_API_KEY"),
3234
)
3335

3436
generation = client.generations.create(
@@ -39,16 +41,23 @@ generation = client.generations.create(
3941
print(generation.id)
4042
```
4143

44+
While you can provide a `auth_token` keyword argument,
45+
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
46+
to add `LUMAAI_API_KEY="My Auth Token"` to your `.env` file
47+
so that your Auth Token is not stored in source control.
48+
4249
## Async usage
4350

4451
Simply import `AsyncLumaai` instead of `Lumaai` and use `await` with each API call:
4552

4653
```python
54+
import os
4755
import asyncio
4856
from lumaai import AsyncLumaai
4957

5058
client = AsyncLumaai(
51-
auth_token="My Auth Token",
59+
# This is the default and can be omitted
60+
auth_token=os.environ.get("LUMAAI_API_KEY"),
5261
)
5362

5463

src/lumaai/_client.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from ._version import __version__
2727
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
28-
from ._exceptions import APIStatusError
28+
from ._exceptions import LumaaiError, APIStatusError
2929
from ._base_client import (
3030
DEFAULT_MAX_RETRIES,
3131
SyncAPIClient,
@@ -57,7 +57,7 @@ class Lumaai(SyncAPIClient):
5757
def __init__(
5858
self,
5959
*,
60-
auth_token: str,
60+
auth_token: str | None = None,
6161
base_url: str | httpx.URL | None = None,
6262
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
6363
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -77,7 +77,16 @@ def __init__(
7777
# part of our public interface in the future.
7878
_strict_response_validation: bool = False,
7979
) -> None:
80-
"""Construct a new synchronous lumaai client instance."""
80+
"""Construct a new synchronous lumaai client instance.
81+
82+
This automatically infers the `auth_token` argument from the `LUMAAI_API_KEY` environment variable if it is not provided.
83+
"""
84+
if auth_token is None:
85+
auth_token = os.environ.get("LUMAAI_API_KEY")
86+
if auth_token is None:
87+
raise LumaaiError(
88+
"The auth_token client option must be set either by passing auth_token to the client or by setting the LUMAAI_API_KEY environment variable"
89+
)
8190
self.auth_token = auth_token
8291

8392
if base_url is None:
@@ -218,7 +227,7 @@ class AsyncLumaai(AsyncAPIClient):
218227
def __init__(
219228
self,
220229
*,
221-
auth_token: str,
230+
auth_token: str | None = None,
222231
base_url: str | httpx.URL | None = None,
223232
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
224233
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -238,7 +247,16 @@ def __init__(
238247
# part of our public interface in the future.
239248
_strict_response_validation: bool = False,
240249
) -> None:
241-
"""Construct a new async lumaai client instance."""
250+
"""Construct a new async lumaai client instance.
251+
252+
This automatically infers the `auth_token` argument from the `LUMAAI_API_KEY` environment variable if it is not provided.
253+
"""
254+
if auth_token is None:
255+
auth_token = os.environ.get("LUMAAI_API_KEY")
256+
if auth_token is None:
257+
raise LumaaiError(
258+
"The auth_token client option must be set either by passing auth_token to the client or by setting the LUMAAI_API_KEY environment variable"
259+
)
242260
self.auth_token = auth_token
243261

244262
if base_url is None:

tests/test_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
from pydantic import ValidationError
1818

1919
from lumaai import Lumaai, AsyncLumaai, APIResponseValidationError
20+
from lumaai._types import Omit
2021
from lumaai._models import BaseModel, FinalRequestOptions
2122
from lumaai._constants import RAW_RESPONSE_HEADER
22-
from lumaai._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
23+
from lumaai._exceptions import LumaaiError, APIStatusError, APITimeoutError, APIResponseValidationError
2324
from lumaai._base_client import DEFAULT_TIMEOUT, HTTPX_DEFAULT_TIMEOUT, BaseClient, make_request_options
2425

2526
from .utils import update_env
@@ -328,6 +329,11 @@ def test_validate_headers(self) -> None:
328329
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
329330
assert request.headers.get("Authorization") == f"Bearer {auth_token}"
330331

332+
with pytest.raises(LumaaiError):
333+
with update_env(**{"LUMAAI_API_KEY": Omit()}):
334+
client2 = Lumaai(base_url=base_url, auth_token=None, _strict_response_validation=True)
335+
_ = client2
336+
331337
def test_default_query_option(self) -> None:
332338
client = Lumaai(
333339
base_url=base_url,
@@ -1055,6 +1061,11 @@ def test_validate_headers(self) -> None:
10551061
request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
10561062
assert request.headers.get("Authorization") == f"Bearer {auth_token}"
10571063

1064+
with pytest.raises(LumaaiError):
1065+
with update_env(**{"LUMAAI_API_KEY": Omit()}):
1066+
client2 = AsyncLumaai(base_url=base_url, auth_token=None, _strict_response_validation=True)
1067+
_ = client2
1068+
10581069
def test_default_query_option(self) -> None:
10591070
client = AsyncLumaai(
10601071
base_url=base_url,

0 commit comments

Comments
 (0)