2626from ._version import __version__
2727from .resources import projects , environment_classes , personal_access_tokens
2828from ._streaming import Stream as Stream , AsyncStream as AsyncStream
29- from ._exceptions import APIStatusError
29+ from ._exceptions import GitpodError , APIStatusError
3030from ._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 ,
0 commit comments