2929from dynamodb_encryption_sdk .transform import dict_to_ddb
3030
3131try : # Python 3.5.0 and 3.5.1 have incompatible typing modules
32- from typing import Any , Bool , Callable , Dict , Text # noqa pylint: disable=unused-import
32+ from typing import Any , Bool , Callable , Dict , Iterable , Text # noqa pylint: disable=unused-import
3333except ImportError : # pragma: no cover
3434 # We only actually need these imports when running the mypy checks
3535 pass
4141 "crypto_config_from_cache" ,
4242 "decrypt_get_item" ,
4343 "decrypt_multi_get" ,
44+ "decrypt_list_of_items" ,
4445 "decrypt_batch_get_item" ,
4546 "encrypt_put_item" ,
4647 "encrypt_batch_write_item" ,
@@ -171,6 +172,22 @@ def _item_transformer(crypto_transformer):
171172 return lambda x : x
172173
173174
175+ def decrypt_list_of_items (crypto_config , decrypt_method , items ):
176+ # type: (CryptoConfig, Callable, Iterable[Any]) -> Iterable[Any]
177+ # TODO: narrow this down
178+ """Iterate through a list of encrypted items, decrypting each item and yielding the plaintext item.
179+
180+ :param CryptoConfig crypto_config: :class:`CryptoConfig` to use
181+ :param callable decrypt_method: Method to use to decrypt items
182+ :param items: Iterable of encrypted items
183+ :return: Iterable of plaintext items
184+ """
185+ for value in items :
186+ yield decrypt_method (
187+ item = value , crypto_config = crypto_config .with_item (_item_transformer (decrypt_method )(value ))
188+ )
189+
190+
174191def decrypt_multi_get (decrypt_method , crypto_config_method , read_method , ** kwargs ):
175192 # type: (Callable, Callable, Callable, **Any) -> Dict
176193 # TODO: narrow this down
@@ -186,11 +203,9 @@ def decrypt_multi_get(decrypt_method, crypto_config_method, read_method, **kwarg
186203 validate_get_arguments (kwargs )
187204 crypto_config , ddb_kwargs = crypto_config_method (** kwargs )
188205 response = read_method (** ddb_kwargs )
189- for pos in range (len (response ["Items" ])):
190- response ["Items" ][pos ] = decrypt_method (
191- item = response ["Items" ][pos ],
192- crypto_config = crypto_config .with_item (_item_transformer (decrypt_method )(response ["Items" ][pos ])),
193- )
206+ response ["Items" ] = list (
207+ decrypt_list_of_items (crypto_config = crypto_config , decrypt_method = decrypt_method , items = response ["Items" ])
208+ )
194209 return response
195210
196211
0 commit comments