|
1 | | -import os |
2 | | -from functools import lru_cache, wraps |
| 1 | +from functools import wraps |
3 | 2 | from typing import Any, Callable, Dict, Type |
4 | 3 |
|
| 4 | +from aws_lambda_env_modeler.modeler_impl import __get_environment_variables_impl |
5 | 5 | from aws_lambda_env_modeler.types import Model |
6 | 6 |
|
7 | 7 |
|
8 | | -def get_environment_variables(model: Type[Model]) -> Model: |
9 | | - """ |
10 | | - This function receives a model of type Model, uses it to validate the environment variables, and returns the |
11 | | - validated model. |
12 | | -
|
13 | | - Args: |
14 | | - model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. |
15 | | -
|
16 | | - Returns: |
17 | | - Model: An instance of the provided model filled with the values of the validated environment variables. |
18 | | - """ |
19 | | - return __parse_model(model) |
20 | | - |
21 | | - |
22 | 8 | def init_environment_variables(model: Type[Model]): |
23 | 9 | """ |
24 | | - A decorator function for AWS Lambda handler functions that initializes environment variables based on the given Pydantic model before executing |
25 | | - the decorated function. The decorator validates the environment variables according to the model structure before |
26 | | - running the handler. |
| 10 | + A decorator for AWS Lambda handler functions. It initializes and validates environment variables based on the provided Pydantic model before the execution of the decorated function. |
| 11 | + It uses LRU Cache by model class type to optimize parsing time. Cache can be disabled by setting the environment variable 'LAMBDA_ENV_MODELER_DISABLE_CACHE' to FALSE (default: cache is enabled) |
27 | 12 |
|
28 | 13 | Args: |
29 | | - model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. |
| 14 | + model (Type[Model]): A Pydantic model that outlines the structure and types of the expected environment variables. |
30 | 15 |
|
31 | 16 | Returns: |
32 | | - Callable: A decorated function that first initializes the environment variables and then runs the function. |
| 17 | + Callable: A decorated function that first initializes and validates the environment variables, then executes the original function. |
| 18 | +
|
| 19 | + Raises: |
| 20 | + ValueError: If the environment variables do not align with the model's structure or fail validation. |
33 | 21 | """ |
34 | 22 |
|
35 | 23 | def decorator(lambda_handler_function: Callable): |
36 | 24 | @wraps(lambda_handler_function) |
37 | 25 | def wrapper(event: Dict[str, Any], context, **kwargs): |
38 | | - __parse_model(model) |
| 26 | + # Initialize and validate environment variables before executing the lambda handler function |
| 27 | + __get_environment_variables_impl(model) |
39 | 28 | return lambda_handler_function(event, context, **kwargs) |
40 | 29 |
|
41 | 30 | return wrapper |
42 | 31 |
|
43 | 32 | return decorator |
44 | 33 |
|
45 | 34 |
|
46 | | -@lru_cache |
47 | | -def __parse_model(model: Type[Model]) -> Model: |
| 35 | +def get_environment_variables(model: Type[Model]) -> Model: |
48 | 36 | """ |
49 | | - A helper function to validate and parse environment variables based on a given Pydantic model. This function is |
50 | | - also cached to improve performance in successive calls. |
| 37 | + Retrieves and validates environment variables based on the provided Pydantic model. |
| 38 | + It uses LRU Cache by model class type to optimize parsing time. Cache can be disabled by setting the environment variable 'LAMBDA_ENV_MODELER_DISABLE_CACHE' to FALSE (default: cache is enabled) |
| 39 | + It's recommended to use anywhere in the function's after init_environment_variables decorator was used on the handler function. |
51 | 40 |
|
52 | 41 | Args: |
53 | | - model (Type[Model]): A Pydantic model that defines the structure and types of the expected environment variables. |
| 42 | + model (Type[Model]): A Pydantic model that outlines the structure and types of the expected environment variables. |
54 | 43 |
|
55 | 44 | Returns: |
56 | | - Model: An instance of the provided model filled with the values of the validated environment variables. |
| 45 | + Model: An instance of the provided model populated with the values of the validated environment variables. |
57 | 46 |
|
58 | 47 | Raises: |
59 | | - ValueError: If the environment variables do not match the structure of the model or cannot be validated. |
| 48 | + ValueError: If the environment variables do not align with the model's structure or fail validation. |
60 | 49 | """ |
61 | | - try: |
62 | | - return model.model_validate(os.environ) |
63 | | - except Exception as exc: |
64 | | - raise ValueError(f'failed to load environment variables, exception={str(exc)}') from exc |
| 50 | + return __get_environment_variables_impl(model) |
0 commit comments