Skip to content

Commit 7b2f2cf

Browse files
feat(api): update via SDK Studio
1 parent c8930ae commit 7b2f2cf

File tree

4 files changed

+211
-47
lines changed

4 files changed

+211
-47
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ runner = client.runners.create(
3737
print(runner.access_token)
3838
```
3939

40+
While you can provide a `bearer_token` keyword argument,
41+
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
42+
to add `GITPOD_API_KEY="My Bearer Token"` to your `.env` file
43+
so that your Bearer Token is not stored in source control.
44+
4045
## Async usage
4146

4247
Simply import `AsyncGitpod` instead of `Gitpod` and use `await` with each API call:

Diff for: src/gitpod/_client.py

+45-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from ._version import __version__
2727
from .resources import projects, environment_classes, personal_access_tokens
2828
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
29-
from ._exceptions import APIStatusError
29+
from ._exceptions import GitpodError, APIStatusError
3030
from ._base_client import (
3131
DEFAULT_MAX_RETRIES,
3232
SyncAPIClient,
@@ -52,10 +52,12 @@ class Gitpod(SyncAPIClient):
5252
with_streaming_response: GitpodWithStreamedResponse
5353

5454
# client options
55+
bearer_token: str
5556

5657
def __init__(
5758
self,
5859
*,
60+
bearer_token: str | None = None,
5961
base_url: str | httpx.URL | None = None,
6062
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
6163
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -75,7 +77,18 @@ def __init__(
7577
# part of our public interface in the future.
7678
_strict_response_validation: bool = False,
7779
) -> None:
78-
"""Construct a new synchronous gitpod client instance."""
80+
"""Construct a new synchronous gitpod client instance.
81+
82+
This automatically infers the `bearer_token` argument from the `GITPOD_API_KEY` environment variable if it is not provided.
83+
"""
84+
if bearer_token is None:
85+
bearer_token = os.environ.get("GITPOD_API_KEY")
86+
if bearer_token is None:
87+
raise GitpodError(
88+
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the GITPOD_API_KEY environment variable"
89+
)
90+
self.bearer_token = bearer_token
91+
7992
if base_url is None:
8093
base_url = os.environ.get("GITPOD_BASE_URL")
8194
if base_url is None:
@@ -107,6 +120,12 @@ def __init__(
107120
def qs(self) -> Querystring:
108121
return Querystring(array_format="comma")
109122

123+
@property
124+
@override
125+
def auth_headers(self) -> dict[str, str]:
126+
bearer_token = self.bearer_token
127+
return {"Authorization": f"Bearer {bearer_token}"}
128+
110129
@property
111130
@override
112131
def default_headers(self) -> dict[str, str | Omit]:
@@ -119,6 +138,7 @@ def default_headers(self) -> dict[str, str | Omit]:
119138
def copy(
120139
self,
121140
*,
141+
bearer_token: str | None = None,
122142
base_url: str | httpx.URL | None = None,
123143
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
124144
http_client: httpx.Client | None = None,
@@ -152,6 +172,7 @@ def copy(
152172

153173
http_client = http_client or self._client
154174
return self.__class__(
175+
bearer_token=bearer_token or self.bearer_token,
155176
base_url=base_url or self.base_url,
156177
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
157178
http_client=http_client,
@@ -211,10 +232,12 @@ class AsyncGitpod(AsyncAPIClient):
211232
with_streaming_response: AsyncGitpodWithStreamedResponse
212233

213234
# client options
235+
bearer_token: str
214236

215237
def __init__(
216238
self,
217239
*,
240+
bearer_token: str | None = None,
218241
base_url: str | httpx.URL | None = None,
219242
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
220243
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -234,7 +257,18 @@ def __init__(
234257
# part of our public interface in the future.
235258
_strict_response_validation: bool = False,
236259
) -> None:
237-
"""Construct a new async gitpod client instance."""
260+
"""Construct a new async gitpod client instance.
261+
262+
This automatically infers the `bearer_token` argument from the `GITPOD_API_KEY` environment variable if it is not provided.
263+
"""
264+
if bearer_token is None:
265+
bearer_token = os.environ.get("GITPOD_API_KEY")
266+
if bearer_token is None:
267+
raise GitpodError(
268+
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the GITPOD_API_KEY environment variable"
269+
)
270+
self.bearer_token = bearer_token
271+
238272
if base_url is None:
239273
base_url = os.environ.get("GITPOD_BASE_URL")
240274
if base_url is None:
@@ -266,6 +300,12 @@ def __init__(
266300
def qs(self) -> Querystring:
267301
return Querystring(array_format="comma")
268302

303+
@property
304+
@override
305+
def auth_headers(self) -> dict[str, str]:
306+
bearer_token = self.bearer_token
307+
return {"Authorization": f"Bearer {bearer_token}"}
308+
269309
@property
270310
@override
271311
def default_headers(self) -> dict[str, str | Omit]:
@@ -278,6 +318,7 @@ def default_headers(self) -> dict[str, str | Omit]:
278318
def copy(
279319
self,
280320
*,
321+
bearer_token: str | None = None,
281322
base_url: str | httpx.URL | None = None,
282323
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
283324
http_client: httpx.AsyncClient | None = None,
@@ -311,6 +352,7 @@ def copy(
311352

312353
http_client = http_client or self._client
313354
return self.__class__(
355+
bearer_token=bearer_token or self.bearer_token,
314356
base_url=base_url or self.base_url,
315357
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
316358
http_client=http_client,

Diff for: tests/conftest.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ def pytest_collection_modifyitems(items: list[pytest.Function]) -> None:
2828

2929
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
3030

31+
bearer_token = "My Bearer Token"
32+
3133

3234
@pytest.fixture(scope="session")
3335
def client(request: FixtureRequest) -> Iterator[Gitpod]:
3436
strict = getattr(request, "param", True)
3537
if not isinstance(strict, bool):
3638
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
3739

38-
with Gitpod(base_url=base_url, _strict_response_validation=strict) as client:
40+
with Gitpod(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=strict) as client:
3941
yield client
4042

4143

@@ -45,5 +47,5 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncGitpod]:
4547
if not isinstance(strict, bool):
4648
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
4749

48-
async with AsyncGitpod(base_url=base_url, _strict_response_validation=strict) as client:
50+
async with AsyncGitpod(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=strict) as client:
4951
yield client

0 commit comments

Comments
 (0)