Skip to content

Commit 929c5b9

Browse files
committed
feat: convert client suppliers to callables
1 parent 16b6528 commit 929c5b9

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/aws_encryption_sdk/keyrings/aws_kms/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def _do_aws_kms_decrypt(client_supplier, key_name, encrypted_data_key, encryptio
300300
301301
"""
302302
region = _region_from_key_id(encrypted_data_key.key_provider.key_info.decode("utf-8"))
303-
client = client_supplier.client(region)
303+
client = client_supplier(region)
304304
response = client.decrypt(
305305
CiphertextBlob=encrypted_data_key.encrypted_data_key,
306306
EncryptionContext=encryption_context,
@@ -324,7 +324,7 @@ def _do_aws_kms_encrypt(client_supplier, key_name, plaintext_data_key, encryptio
324324
Any errors encountered are passed up the chain without comment.
325325
"""
326326
region = _region_from_key_id(key_name)
327-
client = client_supplier.client(region)
327+
client = client_supplier(region)
328328
response = client.encrypt(
329329
KeyId=key_name,
330330
Plaintext=plaintext_data_key.data_key,
@@ -347,7 +347,7 @@ def _do_aws_kms_generate_data_key(client_supplier, key_name, encryption_context,
347347
348348
"""
349349
region = _region_from_key_id(key_name)
350-
client = client_supplier.client(region)
350+
client = client_supplier(region)
351351
response = client.generate_data_key(
352352
KeyId=key_name,
353353
NumberOfBytes=algorithm.kdf_input_len,

src/aws_encryption_sdk/keyrings/aws_kms/client_suppliers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class ClientSupplier(object):
3535
3636
"""
3737

38-
def client(self, region_name):
38+
def __call__(self, region_name):
3939
# type: (Union[None, str]) -> BaseClient
4040
"""Return a client for the requested region.
4141
4242
:rtype: BaseClient
4343
"""
44-
raise NotImplementedError("'ClientSupplier' does not implement 'client'")
44+
raise NotImplementedError("'ClientSupplier' is not callable")
4545

4646

4747
@attr.s
@@ -61,7 +61,7 @@ def __attrs_post_init__(self):
6161
"""Set up internal client cache."""
6262
self._cache = ClientCache(botocore_session=self._botocore_session)
6363

64-
def client(self, region_name):
64+
def __call__(self, region_name):
6565
# type: (Union[None, str]) -> BaseClient
6666
"""Return a client for the requested region.
6767
@@ -100,7 +100,7 @@ def client(self, region_name):
100100
if region_name not in self.allowed_regions:
101101
raise UnknownRegionError("Unable to provide client for region '{}'".format(region_name))
102102

103-
return self._supplier.client(region_name)
103+
return self._supplier(region_name)
104104

105105

106106
@attr.s
@@ -123,7 +123,7 @@ def __attrs_post_init__(self):
123123
"""Set up internal client supplier."""
124124
self._supplier = DefaultClientSupplier(botocore_session=self._botocore_session)
125125

126-
def client(self, region_name):
126+
def __call__(self, region_name):
127127
# type: (Union[None, str]) -> BaseClient
128128
"""Return a client for the requested region.
129129
@@ -133,4 +133,4 @@ def client(self, region_name):
133133
if region_name in self.denied_regions:
134134
raise UnknownRegionError("Unable to provide client for region '{}'".format(region_name))
135135

136-
return self._supplier.client(region_name)
136+
return self._supplier(region_name)

test/functional/keyrings/aws_kms/test_client_suppliers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_default_supplier_not_implemented():
1919
test = ClientSupplier()
2020

2121
with pytest.raises(NotImplementedError) as excinfo:
22-
test.client("region")
22+
test("region")
2323

2424
excinfo.match("'ClientSupplier' does not implement 'client'")
2525

@@ -30,7 +30,7 @@ def test_default_supplier_uses_cache():
3030
region = "us-west-2"
3131
expected = supplier._cache.client(region_name=region, service="kms")
3232

33-
test = supplier.client(region)
33+
test = supplier(region)
3434

3535
assert test is expected
3636

@@ -66,14 +66,14 @@ def test_allow_regions_supplier_invalid_parameters(kwargs):
6666
def test_allow_regions_supplier_allows_allowed_region():
6767
test = AllowRegionsClientSupplier(allowed_regions=["us-west-2", "us-east-2"])
6868

69-
assert test.client("us-west-2")
69+
assert test("us-west-2")
7070

7171

7272
def test_allow_regions_supplier_denied_not_allowed_region():
7373
test = AllowRegionsClientSupplier(allowed_regions=["us-west-2", "us-east-2"])
7474

7575
with pytest.raises(UnknownRegionError) as excinfo:
76-
test.client("ap-northeast-2")
76+
test("ap-northeast-2")
7777

7878
excinfo.match("Unable to provide client for region 'ap-northeast-2'")
7979

@@ -102,12 +102,12 @@ def test_deny_regions_supplier_denies_denied_region():
102102
test = DenyRegionsClientSupplier(denied_regions=["us-west-2", "us-east-2"])
103103

104104
with pytest.raises(UnknownRegionError) as excinfo:
105-
test.client("us-west-2")
105+
test("us-west-2")
106106

107107
excinfo.match("Unable to provide client for region 'us-west-2'")
108108

109109

110110
def test_deny_regions_supplier_allows_not_denied_region():
111111
test = DenyRegionsClientSupplier(denied_regions=["us-west-2", "us-east-2"])
112112

113-
assert test.client("ap-northeast-2")
113+
assert test("ap-northeast-2")

0 commit comments

Comments
 (0)