1919_PROTOCOL_VERSION = "0.0.1"
2020
2121ModelType = Literal ["language" , "image" , "video" ]
22+ AuthMethod = Literal ["api-key" , "oidc" ]
2223
2324
2425class GatewayClient :
@@ -33,12 +34,14 @@ def __init__(
3334 self ,
3435 * ,
3536 base_url : str ,
36- api_key : str | None = None ,
37+ auth_token : str | None = None ,
38+ auth_method : AuthMethod | None = None ,
3739 headers : Mapping [str , str ] | None = None ,
3840 client : httpx .AsyncClient | None = None ,
3941 ) -> None :
4042 self .base_url = base_url
41- self .api_key = api_key
43+ self .auth_token = auth_token
44+ self .auth_method = auth_method
4245 self .headers = dict (headers or {})
4346 self ._http = client or httpx .AsyncClient (
4447 timeout = httpx .Timeout (timeout = 300.0 , connect = 10.0 ),
@@ -59,9 +62,9 @@ def origin_url(self, path: str) -> str:
5962 def protocol_headers (self ) -> dict [str , str ]:
6063 headers = dict (self .headers )
6164 headers ["ai-gateway-protocol-version" ] = _PROTOCOL_VERSION
62- if self .api_key :
63- headers ["Authorization" ] = f"Bearer { self .api_key } "
64- headers ["ai-gateway-auth-method" ] = "api-key"
65+ if self .auth_token and self . auth_method :
66+ headers ["Authorization" ] = f"Bearer { self .auth_token } "
67+ headers ["ai-gateway-auth-method" ] = self . auth_method
6568 return headers
6669
6770 def model_headers (
@@ -165,7 +168,7 @@ async def probe_model(self, model_id: str) -> None:
165168 auth_resp = await self .get ("v1/credits" , origin = True )
166169 if auth_resp .status_code in {401 , 403 }:
167170 raise errors .GatewayAuthenticationError .create_contextual (
168- api_key_provided = bool ( self .api_key ) ,
171+ auth_method = self .auth_method if self . auth_token else None ,
169172 status_code = auth_resp .status_code ,
170173 )
171174 if auth_resp .status_code != 200 :
@@ -239,7 +242,7 @@ async def raise_for_error(self, response: httpx.Response) -> None:
239242 raise errors .create_gateway_error (
240243 response_body = response .text ,
241244 status_code = response .status_code ,
242- api_key_provided = bool ( self .api_key ) ,
245+ auth_method = self .auth_method if self . auth_token else None ,
243246 )
244247
245248 async def iter_sse (
0 commit comments