MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / api_keys_required

Function api_keys_required

camel/utils/commons.py:236–314  ·  view source on GitHub ↗

r"""A decorator to check if the required API keys are provided in the environment variables or as function arguments. Args: param_env_list (List[Tuple[Optional[str], str]]): A list of tuples where each tuple contains a function argument name (as the first ele

(
    param_env_list: List[Tuple[Optional[str], str]],
)

Source from the content-addressed store, hash-verified

234
235
236def api_keys_required(
237 param_env_list: List[Tuple[Optional[str], str]],
238) -> Callable[[F], F]:
239 r"""A decorator to check if the required API keys are provided in the
240 environment variables or as function arguments.
241
242 Args:
243 param_env_list (List[Tuple[Optional[str], str]]): A list of tuples
244 where each tuple contains a function argument name (as the first
245 element, or None) and the corresponding environment variable name
246 (as the second element) that holds the API key.
247
248 Returns:
249 Callable[[F], F]: The original function wrapped with the added check
250 for the required API keys.
251
252 Raises:
253 ValueError: If any of the required API keys are missing, either
254 from the function arguments or environment variables.
255
256 Example:
257 ::
258
259 @api_keys_required([
260 ('api_key_arg', 'API_KEY_1'),
261 ('another_key_arg', 'API_KEY_2'),
262 (None, 'API_KEY_3'),
263 ])
264 def some_api_function(api_key_arg=None, another_key_arg=None):
265 # Function implementation that requires API keys
266 """
267 import inspect
268
269 def decorator(func: F) -> F:
270 @wraps(func)
271 def wrapper(*args: Any, **kwargs: Any) -> Any:
272 signature = inspect.signature(func)
273 bound_arguments = signature.bind(*args, **kwargs)
274 bound_arguments.apply_defaults()
275 arguments = bound_arguments.arguments
276
277 missing_keys = []
278 for param_name, env_var_name in param_env_list:
279 if not isinstance(env_var_name, str):
280 raise TypeError(
281 f"Environment variable name must be a string, got"
282 f" {type(env_var_name)}"
283 )
284
285 value = None
286 if (
287 param_name
288 ): # If param_name is provided, check function argument first
289 if not isinstance(param_name, str):
290 raise TypeError(
291 f"Parameter name must be a string, "
292 f"got {type(param_name)}"
293 )

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected