1- from enum import Enum
21from inspect import isawaitable
3- from typing import (
4- Awaitable ,
5- Callable ,
6- Dict ,
7- Optional ,
8- Tuple ,
9- TypeVar ,
10- Union ,
11- overload ,
12- )
13-
14- from typing_extensions import assert_never
15-
16-
17- class AuthType (Enum ):
18- API_KEY = "API_KEY"
19- BEARER = "BEARER"
20-
2+ from typing import Awaitable , Callable , Dict , Optional , TypeVar , Union
213
224SyncAuthValue = Union [str , Callable [[], str ]]
235AsyncAuthValue = Union [SyncAuthValue , Callable [[], Awaitable [str ]]]
@@ -28,72 +10,78 @@ class AuthType(Enum):
2810)
2911
3012
31- @overload
32- def get_auth_value (auth_value : SyncAuthValue ) -> str : ...
33-
13+ def get_auth_value (auth_value : SyncAuthValue ) -> str :
14+ if isinstance (auth_value , str ):
15+ return auth_value
16+ if callable (auth_value ):
17+ return auth_value ()
18+ from typing import TYPE_CHECKING , assert_never
3419
35- @overload
36- def get_auth_value (
37- auth_value : AsyncAuthValue ,
38- ) -> Union [str , Awaitable [str ]]: ...
20+ if TYPE_CHECKING :
21+ assert_never (auth_value )
22+ raise TypeError (
23+ f"auth_value must be a string or a callable returning a string, got { type (auth_value ).__name__ } "
24+ )
3925
4026
41- def get_auth_value (
42- auth_value : Union [SyncAuthValue , AsyncAuthValue ]
43- ) -> Union [str , Awaitable [str ]]:
27+ async def aget_auth_value (auth_value : AsyncAuthValue ) -> str :
4428 if isinstance (auth_value , str ):
4529 return auth_value
46- elif callable (auth_value ):
47- return auth_value ()
48- else :
30+ if callable (auth_value ):
31+ result = auth_value ()
32+ return await result if isawaitable (result ) else result
33+ from typing import TYPE_CHECKING , assert_never
34+
35+ if TYPE_CHECKING :
4936 assert_never (auth_value )
37+ raise TypeError (
38+ f"auth_value must be a string or a callable, got { type (auth_value ).__name__ } "
39+ )
5040
5141
52- async def aget_auth_value (auth_value : AsyncAuthValue ) -> str :
53- processed_auth_value = get_auth_value (auth_value )
54- if isawaitable (processed_auth_value ):
55- return await processed_auth_value
56- return processed_auth_value
42+ def get_combined_auth_headers (
43+ * ,
44+ api_key : Optional [SyncAuthValue ] = None ,
45+ bearer_token : Optional [SyncAuthValue ] = None ,
46+ ) -> Dict [str , str ]:
47+ headers : Dict [str , str ] = {}
48+
49+ if api_key is not None :
50+ headers ["api-key" ] = get_auth_value (api_key )
5751
52+ if bearer_token is not None :
53+ bearer_str = get_auth_value (bearer_token )
54+ headers ["Authorization" ] = f"Bearer { bearer_str } "
5855
59- def _get_auth_headers (auth_type : AuthType , auth_value : str ) -> Dict [str , str ]:
60- if auth_type == AuthType .API_KEY :
61- return {"api-key" : auth_value }
62- elif auth_type == AuthType .BEARER :
63- return {"Authorization" : f"Bearer { auth_value } " }
64- else :
65- assert_never (auth_type )
56+ return headers
6657
6758
68- def get_auth_headers (
59+ async def aget_combined_auth_headers (
6960 * ,
70- auth_value : SyncAuthValue ,
71- auth_type : AuthType ,
61+ api_key : Optional [ AsyncAuthValue ] = None ,
62+ bearer_token : Optional [ AsyncAuthValue ] = None ,
7263) -> Dict [str , str ]:
73- processed_auth_value = get_auth_value ( auth_value )
74- return _get_auth_headers ( auth_type , processed_auth_value )
64+ """Get combined authentication headers from both api_key and bearer_token (async)."""
65+ headers : Dict [ str , str ] = {}
7566
67+ if api_key is not None :
68+ processed_api_key = await aget_auth_value (api_key )
69+ headers ["api-key" ] = processed_api_key
7670
77- async def aget_auth_headers (
78- auth_value : AsyncAuthValue ,
79- auth_type : AuthType ,
80- ) -> Dict [str , str ]:
81- processed_auth_value = await aget_auth_value (auth_value )
82- return _get_auth_headers (auth_type , processed_auth_value )
71+ if bearer_token is not None :
72+ processed_bearer_token = await aget_auth_value (bearer_token )
73+ headers ["Authorization" ] = f"Bearer { processed_bearer_token } "
74+
75+ return headers
8376
8477
85- def process_auth (
78+ def validate_auth (
8679 * ,
87- api_key : Optional [AuthValueT ] = None ,
88- bearer_token : Optional [AuthValueT ] = None ,
89- ) -> Tuple [AuthType , AuthValueT ]:
90- if api_key and bearer_token :
80+ api_key : Optional [AsyncAuthValue ] = None ,
81+ bearer_token : Optional [AsyncAuthValue ] = None ,
82+ ) -> None :
83+ """Validate that at least one authentication method is provided."""
84+ if not api_key and not bearer_token :
9185 raise ValueError (
92- "Either api_key or bearer_token must be provided, but not both "
86+ "At least one of api_key or bearer_token must be provided"
9387 )
94- elif api_key :
95- return AuthType .API_KEY , api_key
96- elif bearer_token :
97- return AuthType .BEARER , bearer_token
98- else :
99- raise ValueError ("Either api_key or bearer_token must be provided" )
0 commit comments