1717from asknews_sdk .errors import raise_from_response
1818from asknews_sdk .response import APIResponse , AsyncAPIResponse
1919from asknews_sdk .security import (
20+ APIKey ,
2021 AsyncTokenLoadHook ,
2122 AsyncTokenSaveHook ,
2223 OAuth2ClientCredentials ,
@@ -47,6 +48,7 @@ def __init__(
4748 client_id : Optional [str ],
4849 client_secret : Optional [str ],
4950 scopes : Optional [Set [str ]],
51+ api_key : Optional [str ],
5052 base_url : str ,
5153 token_url : str ,
5254 verify_ssl : bool ,
@@ -64,6 +66,7 @@ def __init__(
6466 self .client_id = client_id
6567 self .client_secret = client_secret
6668 self .scopes = {"offline" , "openid" , * (scopes or set ())}
69+ self .api_key = api_key
6770 self .base_url = base_url
6871 self .token_url = token_url
6972 self .verify_ssl = verify_ssl
@@ -74,18 +77,25 @@ def __init__(
7477
7578 if auth :
7679 if auth is CLIENT_DEFAULT :
77- assert self .client_id and self .client_secret , (
78- "client_id and client_secret are required for client credentials"
79- )
80-
81- self ._client_auth = OAuth2ClientCredentials (
82- client_id = self .client_id ,
83- client_secret = self .client_secret ,
84- token_url = self .token_url ,
85- scopes = self .scopes ,
86- _token_load_hook = _token_load_hook ,
87- _token_save_hook = _token_save_hook ,
88- )
80+ if self .api_key is not None :
81+ self ._client_auth = APIKey (
82+ api_key = self .api_key ,
83+ )
84+ elif self .client_id is not None and self .client_secret is not None :
85+ self ._client_auth = OAuth2ClientCredentials (
86+ client_id = self .client_id ,
87+ client_secret = self .client_secret ,
88+ token_url = self .token_url ,
89+ scopes = self .scopes ,
90+ _token_load_hook = _token_load_hook ,
91+ _token_save_hook = _token_save_hook ,
92+ )
93+ else :
94+ raise ValueError (
95+ "Either api_key or client_id and client_secret are "
96+ "required for authentication. To explicitly disable "
97+ "authentication, set auth=None." ,
98+ )
8999 else :
90100 assert not isinstance (auth , Sentinel )
91101 self ._client_auth = auth
@@ -140,11 +150,13 @@ class APIClient(BaseAPIClient[Client, APIResponse]):
140150 Sync HTTP API Client
141151
142152 :param client_id: Client ID
143- :type client_id: str
153+ :type client_id: Optional[ str]
144154 :param client_secret: Client secret
145- :type client_secret: str
155+ :type client_secret: Optional[ str]
146156 :param scopes: OAuth scopes
147- :type scopes: Set[str]
157+ :type scopes: Optional[Set[str]]
158+ :param api_key: API key
159+ :type api_key: Optional[str]
148160 :param base_url: Base URL
149161 :type base_url: str
150162 :param token_url: Token URL
@@ -166,6 +178,7 @@ def __init__(
166178 client_id : Optional [str ],
167179 client_secret : Optional [str ],
168180 scopes : Optional [Set [str ]],
181+ api_key : Optional [str ],
169182 base_url : str ,
170183 token_url : str ,
171184 verify_ssl : bool = True ,
@@ -184,6 +197,7 @@ def __init__(
184197 client_id = client_id ,
185198 client_secret = client_secret ,
186199 scopes = scopes ,
200+ api_key = api_key ,
187201 base_url = base_url ,
188202 token_url = token_url ,
189203 verify_ssl = verify_ssl ,
@@ -286,11 +300,13 @@ class AsyncAPIClient(BaseAPIClient[AsyncClient, AsyncAPIResponse]):
286300 Base Async HTTP API Client
287301
288302 :param client_id: Client ID
289- :type client_id: str
303+ :type client_id: Optional[ str]
290304 :param client_secret: Client secret
291- :type client_secret: str
305+ :type client_secret: Optional[ str]
292306 :param scopes: OAuth scopes
293- :type scopes: Set[str]
307+ :type scopes: Optional[Set[str]]
308+ :param api_key: API key
309+ :type api_key: Optional[str]
294310 :param base_url: Base URL
295311 :type base_url: str
296312 :param token_url: Token URL
@@ -312,6 +328,7 @@ def __init__(
312328 client_id : Optional [str ],
313329 client_secret : Optional [str ],
314330 scopes : Optional [Set [str ]],
331+ api_key : Optional [str ],
315332 base_url : str ,
316333 token_url : str ,
317334 verify_ssl : bool = True ,
@@ -330,6 +347,7 @@ def __init__(
330347 client_id = client_id ,
331348 client_secret = client_secret ,
332349 scopes = scopes ,
350+ api_key = api_key ,
333351 base_url = base_url ,
334352 token_url = token_url ,
335353 verify_ssl = verify_ssl ,
@@ -393,7 +411,7 @@ async def request(
393411 :param stream_type: Stream type
394412 :type stream_type: StreamType
395413 :return: AsyncAPIResponse object
396- :rtype: APIResponse
414+ :rtype: AsyncAPIResponse
397415 """
398416 response : Response = await self ._client .send (
399417 self .build_api_request (
0 commit comments