|
9 | 9 | logger = logging.getLogger(__name__)
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def botocache_context(cache=None, action_regex_to_cache=["List.*", "Get.*", "Describe.*"], |
13 |
| - call_log=False, |
14 |
| - supress_warning_message=False): |
| 12 | +def botocache_context( |
| 13 | + cache=None, |
| 14 | + action_regex_to_cache=["List.*", "Get.*", "Describe.*"], |
| 15 | + call_log=False, |
| 16 | + supress_warning_message=False, |
| 17 | +): |
15 | 18 |
|
16 | 19 | if not (isinstance(action_regex_to_cache, list)):
|
17 | 20 | action_regex_to_cache = [action_regex_to_cache]
|
18 | 21 |
|
19 | 22 | class BotoCache(BaseClient):
|
20 | 23 |
|
21 | 24 | def return_cache_key(self, operation_name, api_params):
|
22 |
| - cache_key = \ |
23 |
| - "{access_key}_{service}_{action}_{region}_{api_params}".format( |
24 |
| - # Access Key to identify the Principal |
25 |
| - access_key=self._request_signer._credentials.access_key, |
26 |
| - # Service for identifying which service is being queried |
27 |
| - service=self._service_model.service_name, |
28 |
| - # Action of the service |
29 |
| - action=operation_name, |
30 |
| - # Region where the call is being made |
31 |
| - region=self.meta.region_name, |
32 |
| - # Api Parameters. This takes care of pagination token, marker and other params. |
33 |
| - # The API Params dictionary is sorted before hashing |
34 |
| - api_params=str(OrderedDict(sorted(api_params.items())))) |
| 25 | + cache_key = "{access_key}_{service}_{action}_{region}_{api_params}".format( |
| 26 | + # Access Key to identify the Principal |
| 27 | + access_key=self._request_signer._credentials.access_key, |
| 28 | + # Service for identifying which service is being queried |
| 29 | + service=self._service_model.service_name, |
| 30 | + # Action of the service |
| 31 | + action=operation_name, |
| 32 | + # Region where the call is being made |
| 33 | + region=self.meta.region_name, |
| 34 | + # Api Parameters. This takes care of pagination token, marker and other params. |
| 35 | + # The API Params dictionary is sorted before hashing |
| 36 | + api_params=str(OrderedDict(sorted(api_params.items()))), |
| 37 | + ) |
35 | 38 | hash_gen = hashlib.sha256()
|
36 | 39 | hash_gen.update(cache_key.encode("utf-8"))
|
37 | 40 | return hash_gen.hexdigest()
|
38 | 41 |
|
39 | 42 | def _make_api_call(self, operation_name, api_params):
|
40 | 43 | if call_log:
|
41 |
| - logger.info("API Call Logger: Region - {region}, " |
42 |
| - "Service - {service}, " |
43 |
| - "Action - {action}, " |
44 |
| - "API Params - {api_params}".format(region=self.meta.region_name, |
45 |
| - service=self._service_model.service_name, |
46 |
| - action=operation_name, api_params=str(api_params))) |
47 |
| - if any([bool(re.match(regex, operation_name)) for regex in action_regex_to_cache]): |
| 44 | + logger.info( |
| 45 | + "API Call Logger: Region - {region}, " |
| 46 | + "Service - {service}, " |
| 47 | + "Action - {action}, " |
| 48 | + "API Params - {api_params}".format( |
| 49 | + region=self.meta.region_name, |
| 50 | + service=self._service_model.service_name, |
| 51 | + action=operation_name, |
| 52 | + api_params=str(api_params), |
| 53 | + ) |
| 54 | + ) |
| 55 | + if any( |
| 56 | + [ |
| 57 | + bool(re.match(regex, operation_name)) |
| 58 | + for regex in action_regex_to_cache |
| 59 | + ] |
| 60 | + ): |
48 | 61 | try:
|
49 | 62 | return self._make_cached_api_call(operation_name, api_params)
|
50 | 63 | except Exception as e:
|
51 | 64 | # In case of any errors with caching , normal make api will be called
|
52 | 65 | if not supress_warning_message:
|
53 |
| - logger.error("Error encountered : {}. Retrying the same call without cached context.".format(e)) |
| 66 | + logger.error( |
| 67 | + "Error encountered : {}. Retrying the same call without cached context.".format( |
| 68 | + e |
| 69 | + ) |
| 70 | + ) |
54 | 71 | return super()._make_api_call(operation_name, api_params)
|
55 | 72 |
|
56 | 73 | @cached(cache=cache, key=return_cache_key)
|
57 | 74 | def _make_cached_api_call(self, operation_name, api_params):
|
58 | 75 | return super()._make_api_call(operation_name, api_params)
|
59 | 76 |
|
60 |
| - return patch('botocore.client.BaseClient', new=BotoCache) |
61 |
| - |
| 77 | + return patch("botocore.client.BaseClient", new=BotoCache) |
0 commit comments