9
9
10
10
import attr
11
11
import six
12
- from attr .validators import deep_iterable , instance_of
12
+ from attr .validators import deep_iterable , instance_of , optional
13
13
from botocore .client import BaseClient
14
- from botocore .session import Session as BotocoreSession
15
14
16
15
from aws_encryption_sdk .exceptions import UnknownRegionError
17
16
from aws_encryption_sdk .internal .validators import value_is_not_a_string
18
17
19
18
from .client_cache import ClientCache
20
19
21
20
try : # Python 3.5.0 and 3.5.1 have incompatible typing modules
22
- from typing import Any , Dict , Union # noqa pylint: disable=unused-import
21
+ from typing import Callable , Union # noqa pylint: disable=unused-import
22
+
23
+ ClientSupplierType = Callable [[Union [None , str ]], BaseClient ]
23
24
except ImportError : # pragma: no cover
24
25
# We only actually need these imports when running the mypy checks
25
26
pass
26
27
27
28
_LOGGER = logging .getLogger (__name__ )
28
- __all__ = ("ClientSupplier" , "DefaultClientSupplier" , "AllowRegionsClientSupplier" , "DenyRegionsClientSupplier" )
29
+ __all__ = (
30
+ "ClientSupplier" ,
31
+ "ClientSupplierType" ,
32
+ "DefaultClientSupplier" ,
33
+ "AllowRegionsClientSupplier" ,
34
+ "DenyRegionsClientSupplier" ,
35
+ )
29
36
30
37
31
38
class ClientSupplier (object ):
@@ -55,19 +62,15 @@ class DefaultClientSupplier(ClientSupplier):
55
62
:type botocore_session: botocore.session.Session
56
63
"""
57
64
58
- _botocore_session = attr .ib (default = attr .Factory (BotocoreSession ), validator = instance_of (BotocoreSession ))
59
-
60
- def __attrs_post_init__ (self ):
61
- """Set up internal client cache."""
62
- self ._cache = ClientCache (botocore_session = self ._botocore_session )
65
+ _client_cache = attr .ib (default = attr .Factory (ClientCache ), validator = instance_of (ClientCache ))
63
66
64
67
def __call__ (self , region_name ):
65
68
# type: (Union[None, str]) -> BaseClient
66
69
"""Return a client for the requested region.
67
70
68
71
:rtype: BaseClient
69
72
"""
70
- return self ._cache .client (region_name = region_name , service = "kms" )
73
+ return self ._client_cache .client (region_name = region_name , service = "kms" )
71
74
72
75
73
76
@attr .s
@@ -77,20 +80,17 @@ class AllowRegionsClientSupplier(ClientSupplier):
77
80
.. versionadded:: 1.5.0
78
81
79
82
:param List[str] allowed_regions: Regions to allow
80
- :param botocore_session: botocore session to use when creating clients (optional)
81
- :type botocore_session: botocore.session.Session
83
+ :param ClientSupplier client_supplier: Client supplier to wrap (optional)
82
84
"""
83
85
84
86
allowed_regions = attr .ib (
85
87
validator = (deep_iterable (member_validator = instance_of (six .string_types )), value_is_not_a_string )
86
88
)
87
- _botocore_session = attr .ib (default = attr .Factory (BotocoreSession ), validator = instance_of (BotocoreSession ))
88
-
89
- def __attrs_post_init__ (self ):
90
- """Set up internal client supplier."""
91
- self ._supplier = DefaultClientSupplier (botocore_session = self ._botocore_session )
89
+ _client_supplier = attr .ib (
90
+ default = attr .Factory (DefaultClientSupplier ), validator = optional (instance_of (ClientSupplier ))
91
+ )
92
92
93
- def client (self , region_name ):
93
+ def __call__ (self , region_name ):
94
94
# type: (Union[None, str]) -> BaseClient
95
95
"""Return a client for the requested region.
96
96
@@ -100,7 +100,7 @@ def client(self, region_name):
100
100
if region_name not in self .allowed_regions :
101
101
raise UnknownRegionError ("Unable to provide client for region '{}'" .format (region_name ))
102
102
103
- return self ._supplier (region_name )
103
+ return self ._client_supplier (region_name )
104
104
105
105
106
106
@attr .s
@@ -110,18 +110,15 @@ class DenyRegionsClientSupplier(ClientSupplier):
110
110
.. versionadded:: 1.5.0
111
111
112
112
:param List[str] denied_regions: Regions to deny
113
- :param botocore_session: Botocore session to use when creating clients (optional)
114
- :type botocore_session: botocore.session.Session
113
+ :param ClientSupplier client_supplier: Client supplier to wrap (optional)
115
114
"""
116
115
117
116
denied_regions = attr .ib (
118
117
validator = (deep_iterable (member_validator = instance_of (six .string_types )), value_is_not_a_string )
119
118
)
120
- _botocore_session = attr .ib (default = attr .Factory (BotocoreSession ), validator = instance_of (BotocoreSession ))
121
-
122
- def __attrs_post_init__ (self ):
123
- """Set up internal client supplier."""
124
- self ._supplier = DefaultClientSupplier (botocore_session = self ._botocore_session )
119
+ _client_supplier = attr .ib (
120
+ default = attr .Factory (DefaultClientSupplier ), validator = optional (instance_of (ClientSupplier ))
121
+ )
125
122
126
123
def __call__ (self , region_name ):
127
124
# type: (Union[None, str]) -> BaseClient
@@ -133,4 +130,4 @@ def __call__(self, region_name):
133
130
if region_name in self .denied_regions :
134
131
raise UnknownRegionError ("Unable to provide client for region '{}'" .format (region_name ))
135
132
136
- return self ._supplier (region_name )
133
+ return self ._client_supplier (region_name )
0 commit comments