26
26
from ._version import __version__
27
27
from .resources import projects , environment_classes , personal_access_tokens
28
28
from ._streaming import Stream as Stream , AsyncStream as AsyncStream
29
- from ._exceptions import APIStatusError
29
+ from ._exceptions import GitpodError , APIStatusError
30
30
from ._base_client import (
31
31
DEFAULT_MAX_RETRIES ,
32
32
SyncAPIClient ,
@@ -52,10 +52,12 @@ class Gitpod(SyncAPIClient):
52
52
with_streaming_response : GitpodWithStreamedResponse
53
53
54
54
# client options
55
+ bearer_token : str
55
56
56
57
def __init__ (
57
58
self ,
58
59
* ,
60
+ bearer_token : str | None = None ,
59
61
base_url : str | httpx .URL | None = None ,
60
62
timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
61
63
max_retries : int = DEFAULT_MAX_RETRIES ,
@@ -75,7 +77,18 @@ def __init__(
75
77
# part of our public interface in the future.
76
78
_strict_response_validation : bool = False ,
77
79
) -> 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
+
79
92
if base_url is None :
80
93
base_url = os .environ .get ("GITPOD_BASE_URL" )
81
94
if base_url is None :
@@ -107,6 +120,12 @@ def __init__(
107
120
def qs (self ) -> Querystring :
108
121
return Querystring (array_format = "comma" )
109
122
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
+
110
129
@property
111
130
@override
112
131
def default_headers (self ) -> dict [str , str | Omit ]:
@@ -119,6 +138,7 @@ def default_headers(self) -> dict[str, str | Omit]:
119
138
def copy (
120
139
self ,
121
140
* ,
141
+ bearer_token : str | None = None ,
122
142
base_url : str | httpx .URL | None = None ,
123
143
timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
124
144
http_client : httpx .Client | None = None ,
@@ -152,6 +172,7 @@ def copy(
152
172
153
173
http_client = http_client or self ._client
154
174
return self .__class__ (
175
+ bearer_token = bearer_token or self .bearer_token ,
155
176
base_url = base_url or self .base_url ,
156
177
timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
157
178
http_client = http_client ,
@@ -211,10 +232,12 @@ class AsyncGitpod(AsyncAPIClient):
211
232
with_streaming_response : AsyncGitpodWithStreamedResponse
212
233
213
234
# client options
235
+ bearer_token : str
214
236
215
237
def __init__ (
216
238
self ,
217
239
* ,
240
+ bearer_token : str | None = None ,
218
241
base_url : str | httpx .URL | None = None ,
219
242
timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
220
243
max_retries : int = DEFAULT_MAX_RETRIES ,
@@ -234,7 +257,18 @@ def __init__(
234
257
# part of our public interface in the future.
235
258
_strict_response_validation : bool = False ,
236
259
) -> 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
+
238
272
if base_url is None :
239
273
base_url = os .environ .get ("GITPOD_BASE_URL" )
240
274
if base_url is None :
@@ -266,6 +300,12 @@ def __init__(
266
300
def qs (self ) -> Querystring :
267
301
return Querystring (array_format = "comma" )
268
302
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
+
269
309
@property
270
310
@override
271
311
def default_headers (self ) -> dict [str , str | Omit ]:
@@ -278,6 +318,7 @@ def default_headers(self) -> dict[str, str | Omit]:
278
318
def copy (
279
319
self ,
280
320
* ,
321
+ bearer_token : str | None = None ,
281
322
base_url : str | httpx .URL | None = None ,
282
323
timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
283
324
http_client : httpx .AsyncClient | None = None ,
@@ -311,6 +352,7 @@ def copy(
311
352
312
353
http_client = http_client or self ._client
313
354
return self .__class__ (
355
+ bearer_token = bearer_token or self .bearer_token ,
314
356
base_url = base_url or self .base_url ,
315
357
timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
316
358
http_client = http_client ,
0 commit comments