@@ -63,6 +63,7 @@ class Codex(SyncAPIClient):
63
63
with_streaming_response : CodexWithStreamedResponse
64
64
65
65
# client options
66
+ auth_token : str | None
66
67
api_key : str | None
67
68
access_key : str | None
68
69
@@ -71,6 +72,7 @@ class Codex(SyncAPIClient):
71
72
def __init__ (
72
73
self ,
73
74
* ,
75
+ auth_token : str | None = None ,
74
76
api_key : str | None = None ,
75
77
access_key : str | None = None ,
76
78
environment : Literal ["production" , "staging" , "local" ] | NotGiven = NOT_GIVEN ,
@@ -94,6 +96,8 @@ def __init__(
94
96
_strict_response_validation : bool = False ,
95
97
) -> None :
96
98
"""Construct a new synchronous Codex client instance."""
99
+ self .auth_token = auth_token
100
+
97
101
self .api_key = api_key
98
102
99
103
self .access_key = access_key
@@ -151,7 +155,14 @@ def qs(self) -> Querystring:
151
155
@property
152
156
@override
153
157
def auth_headers (self ) -> dict [str , str ]:
154
- return {** self ._authenticated_api_key , ** self ._public_access_key }
158
+ return {** self ._http_bearer , ** self ._authenticated_api_key , ** self ._public_access_key }
159
+
160
+ @property
161
+ def _http_bearer (self ) -> dict [str , str ]:
162
+ auth_token = self .auth_token
163
+ if auth_token is None :
164
+ return {}
165
+ return {"Authorization" : f"Bearer { auth_token } " }
155
166
156
167
@property
157
168
def _authenticated_api_key (self ) -> dict [str , str ]:
@@ -178,6 +189,11 @@ def default_headers(self) -> dict[str, str | Omit]:
178
189
179
190
@override
180
191
def _validate_headers (self , headers : Headers , custom_headers : Headers ) -> None :
192
+ if self .auth_token and headers .get ("Authorization" ):
193
+ return
194
+ if isinstance (custom_headers .get ("Authorization" ), Omit ):
195
+ return
196
+
181
197
if self .api_key and headers .get ("X-API-Key" ):
182
198
return
183
199
if isinstance (custom_headers .get ("X-API-Key" ), Omit ):
@@ -189,12 +205,13 @@ def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
189
205
return
190
206
191
207
raise TypeError (
192
- '"Could not resolve authentication method. Expected either api_key or access_key to be set. Or for one of the `X-API-Key` or `X-Access-Key` headers to be explicitly omitted"'
208
+ '"Could not resolve authentication method. Expected one of auth_token, api_key or access_key to be set. Or for one of the `Authorization`, `X-API-Key` or `X-Access-Key` headers to be explicitly omitted"'
193
209
)
194
210
195
211
def copy (
196
212
self ,
197
213
* ,
214
+ auth_token : str | None = None ,
198
215
api_key : str | None = None ,
199
216
access_key : str | None = None ,
200
217
environment : Literal ["production" , "staging" , "local" ] | None = None ,
@@ -231,6 +248,7 @@ def copy(
231
248
232
249
http_client = http_client or self ._client
233
250
return self .__class__ (
251
+ auth_token = auth_token or self .auth_token ,
234
252
api_key = api_key or self .api_key ,
235
253
access_key = access_key or self .access_key ,
236
254
base_url = base_url or self .base_url ,
@@ -291,6 +309,7 @@ class AsyncCodex(AsyncAPIClient):
291
309
with_streaming_response : AsyncCodexWithStreamedResponse
292
310
293
311
# client options
312
+ auth_token : str | None
294
313
api_key : str | None
295
314
access_key : str | None
296
315
@@ -299,6 +318,7 @@ class AsyncCodex(AsyncAPIClient):
299
318
def __init__ (
300
319
self ,
301
320
* ,
321
+ auth_token : str | None = None ,
302
322
api_key : str | None = None ,
303
323
access_key : str | None = None ,
304
324
environment : Literal ["production" , "staging" , "local" ] | NotGiven = NOT_GIVEN ,
@@ -322,6 +342,8 @@ def __init__(
322
342
_strict_response_validation : bool = False ,
323
343
) -> None :
324
344
"""Construct a new async AsyncCodex client instance."""
345
+ self .auth_token = auth_token
346
+
325
347
self .api_key = api_key
326
348
327
349
self .access_key = access_key
@@ -379,7 +401,14 @@ def qs(self) -> Querystring:
379
401
@property
380
402
@override
381
403
def auth_headers (self ) -> dict [str , str ]:
382
- return {** self ._authenticated_api_key , ** self ._public_access_key }
404
+ return {** self ._http_bearer , ** self ._authenticated_api_key , ** self ._public_access_key }
405
+
406
+ @property
407
+ def _http_bearer (self ) -> dict [str , str ]:
408
+ auth_token = self .auth_token
409
+ if auth_token is None :
410
+ return {}
411
+ return {"Authorization" : f"Bearer { auth_token } " }
383
412
384
413
@property
385
414
def _authenticated_api_key (self ) -> dict [str , str ]:
@@ -406,6 +435,11 @@ def default_headers(self) -> dict[str, str | Omit]:
406
435
407
436
@override
408
437
def _validate_headers (self , headers : Headers , custom_headers : Headers ) -> None :
438
+ if self .auth_token and headers .get ("Authorization" ):
439
+ return
440
+ if isinstance (custom_headers .get ("Authorization" ), Omit ):
441
+ return
442
+
409
443
if self .api_key and headers .get ("X-API-Key" ):
410
444
return
411
445
if isinstance (custom_headers .get ("X-API-Key" ), Omit ):
@@ -417,12 +451,13 @@ def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
417
451
return
418
452
419
453
raise TypeError (
420
- '"Could not resolve authentication method. Expected either api_key or access_key to be set. Or for one of the `X-API-Key` or `X-Access-Key` headers to be explicitly omitted"'
454
+ '"Could not resolve authentication method. Expected one of auth_token, api_key or access_key to be set. Or for one of the `Authorization`, `X-API-Key` or `X-Access-Key` headers to be explicitly omitted"'
421
455
)
422
456
423
457
def copy (
424
458
self ,
425
459
* ,
460
+ auth_token : str | None = None ,
426
461
api_key : str | None = None ,
427
462
access_key : str | None = None ,
428
463
environment : Literal ["production" , "staging" , "local" ] | None = None ,
@@ -459,6 +494,7 @@ def copy(
459
494
460
495
http_client = http_client or self ._client
461
496
return self .__class__ (
497
+ auth_token = auth_token or self .auth_token ,
462
498
api_key = api_key or self .api_key ,
463
499
access_key = access_key or self .access_key ,
464
500
base_url = base_url or self .base_url ,
0 commit comments