|
5 | 5 |
|
6 | 6 | import base64 |
7 | 7 | import json |
| 8 | +import os |
8 | 9 | from abc import ABC, abstractmethod |
9 | 10 | from datetime import datetime, timedelta |
10 | 11 | from typing import ( |
|
24 | 25 | import boto3 |
25 | 26 | from botocore.config import Config |
26 | 27 |
|
| 28 | +from aws_lambda_powertools.shared import constants |
| 29 | +from aws_lambda_powertools.shared.functions import resolve_max_age |
27 | 30 | from aws_lambda_powertools.utilities.parameters.types import TransformOptions |
28 | 31 |
|
29 | 32 | from .exceptions import GetParameterError, TransformParameterError |
|
35 | 38 | from mypy_boto3_ssm import SSMClient |
36 | 39 |
|
37 | 40 |
|
38 | | -DEFAULT_MAX_AGE_SECS = 5 |
| 41 | +DEFAULT_MAX_AGE_SECS = "5" |
| 42 | + |
39 | 43 | # These providers will be dynamically initialized on first use of the helper functions |
40 | 44 | DEFAULT_PROVIDERS: Dict[str, Any] = {} |
41 | 45 | TRANSFORM_METHOD_JSON = "json" |
@@ -77,7 +81,7 @@ def has_not_expired_in_cache(self, key: Tuple[str, TransformOptions]) -> bool: |
77 | 81 | def get( |
78 | 82 | self, |
79 | 83 | name: str, |
80 | | - max_age: int = DEFAULT_MAX_AGE_SECS, |
| 84 | + max_age: Optional[int] = None, |
81 | 85 | transform: TransformOptions = None, |
82 | 86 | force_fetch: bool = False, |
83 | 87 | **sdk_options, |
@@ -121,6 +125,9 @@ def get( |
121 | 125 | value: Optional[Union[str, bytes, dict]] = None |
122 | 126 | key = (name, transform) |
123 | 127 |
|
| 128 | + # If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS |
| 129 | + max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age) |
| 130 | + |
124 | 131 | if not force_fetch and self.has_not_expired_in_cache(key): |
125 | 132 | return self.store[key].value |
126 | 133 |
|
@@ -149,7 +156,7 @@ def _get(self, name: str, **sdk_options) -> Union[str, bytes]: |
149 | 156 | def get_multiple( |
150 | 157 | self, |
151 | 158 | path: str, |
152 | | - max_age: int = DEFAULT_MAX_AGE_SECS, |
| 159 | + max_age: Optional[int] = None, |
153 | 160 | transform: TransformOptions = None, |
154 | 161 | raise_on_transform_error: bool = False, |
155 | 162 | force_fetch: bool = False, |
@@ -186,6 +193,9 @@ def get_multiple( |
186 | 193 | """ |
187 | 194 | key = (path, transform) |
188 | 195 |
|
| 196 | + # If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS |
| 197 | + max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age) |
| 198 | + |
189 | 199 | if not force_fetch and self.has_not_expired_in_cache(key): |
190 | 200 | return self.store[key].value # type: ignore # need to revisit entire typing here |
191 | 201 |
|
|
0 commit comments