Skip to content

Commit bb030ab

Browse files
committed
refactor: config properties retrieval and validation
1 parent b17206b commit bb030ab

2 files changed

Lines changed: 52 additions & 62 deletions

File tree

aws_advanced_python_wrapper/simple_read_write_splitting_plugin.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,24 @@ class EndpointBasedConnectionHandler(ConnectionHandler):
4141
"""Endpoint based implementation of connection handling logic."""
4242

4343
def __init__(self, plugin_service: PluginService, props: Properties):
44-
srw_read_endpoint = WrapperProperties.SRW_READ_ENDPOINT.get(props)
45-
if srw_read_endpoint is None:
46-
raise AwsWrapperError(
47-
Messages.get_formatted(
48-
"SimpleReadWriteSplittingPlugin.MissingRequiredConfigParameter",
49-
WrapperProperties.SRW_READ_ENDPOINT.name,
50-
)
51-
)
52-
self._read_endpoint: str = srw_read_endpoint
53-
54-
srw_write_endpoint = WrapperProperties.SRW_WRITE_ENDPOINT.get(props)
55-
if srw_write_endpoint is None:
56-
raise AwsWrapperError(
57-
Messages.get_formatted(
58-
"SimpleReadWriteSplittingPlugin.MissingRequiredConfigParameter",
59-
WrapperProperties.SRW_WRITE_ENDPOINT.name,
60-
)
61-
)
62-
self._write_endpoint: str = srw_write_endpoint
44+
self._read_endpoint: str = EndpointBasedConnectionHandler._verify_parameter(
45+
WrapperProperties.SRW_READ_ENDPOINT, props, str, required=True
46+
)
47+
self._write_endpoint: str = EndpointBasedConnectionHandler._verify_parameter(
48+
WrapperProperties.SRW_WRITE_ENDPOINT, props, str, required=True
49+
)
6350

64-
self._verify_new_connections: bool = (
65-
WrapperProperties.SRW_VERIFY_NEW_CONNECTIONS.get_bool(props)
51+
self._verify_new_connections: bool = self._verify_parameter(
52+
WrapperProperties.SRW_VERIFY_NEW_CONNECTIONS, props, bool
6653
)
67-
if self._verify_new_connections is True:
68-
srw_connect_retry_timeout_ms: int = (
69-
WrapperProperties.SRW_CONNECT_RETRY_TIMEOUT_MS.get_int(props)
70-
)
71-
if srw_connect_retry_timeout_ms <= 0:
72-
raise ValueError(
73-
Messages.get_formatted(
74-
"SimpleReadWriteSplittingPlugin.IncorrectConfiguration",
75-
WrapperProperties.SRW_CONNECT_RETRY_TIMEOUT_MS.name,
76-
)
77-
)
78-
self._connect_retry_timeout_ms: int = srw_connect_retry_timeout_ms
7954

80-
srw_connect_retry_interval_ms: int = (
81-
WrapperProperties.SRW_CONNECT_RETRY_INTERVAL_MS.get_int(props)
55+
if self._verify_new_connections:
56+
self._connect_retry_timeout_ms: int = self._verify_parameter(
57+
WrapperProperties.SRW_CONNECT_RETRY_TIMEOUT_MS, props, int, lambda x: x > 0
58+
)
59+
self._connect_retry_interval_ms: int = self._verify_parameter(
60+
WrapperProperties.SRW_CONNECT_RETRY_INTERVAL_MS, props, int, lambda x: x > 0
8261
)
83-
if srw_connect_retry_interval_ms <= 0:
84-
raise ValueError(
85-
Messages.get_formatted(
86-
"SimpleReadWriteSplittingPlugin.IncorrectConfiguration",
87-
WrapperProperties.SRW_CONNECT_RETRY_INTERVAL_MS.name,
88-
)
89-
)
90-
self._connect_retry_interval_ms: int = srw_connect_retry_interval_ms
9162

9263
self._verify_opened_connection_type: Optional[HostRole] = (
9364
EndpointBasedConnectionHandler._parse_connection_type(
@@ -305,6 +276,27 @@ def _create_host_info(self, endpoint, role: HostRole) -> HostInfo:
305276
host=host, port=port, role=role, availability=HostAvailability.AVAILABLE
306277
)
307278

279+
@staticmethod
280+
def _verify_parameter(prop, props, expected_type, validator=None, required=False):
281+
value = prop.get_type(props, expected_type)
282+
if required:
283+
if value is None:
284+
raise AwsWrapperError(
285+
Messages.get_formatted(
286+
"SimpleReadWriteSplittingPlugin.MissingRequiredConfigParameter",
287+
prop.name,
288+
)
289+
)
290+
291+
if validator and not validator(value):
292+
raise ValueError(
293+
Messages.get_formatted(
294+
"SimpleReadWriteSplittingPlugin.IncorrectConfiguration",
295+
prop.name,
296+
)
297+
)
298+
return value
299+
308300
def _delay(self):
309301
sleep(self._connect_retry_interval_ms / 1000)
310302

aws_advanced_python_wrapper/utils/properties.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
1415
import copy
15-
from typing import Any, Dict, Optional
16+
from typing import Any, Dict, Optional, TypeVar, Type
1617
from urllib.parse import unquote
1718

1819
from aws_advanced_python_wrapper.errors import AwsWrapperError
@@ -25,6 +26,8 @@ def put_if_absent(self, key: str, value: Any):
2526
self[key] = value
2627

2728

29+
T = TypeVar('T')
30+
2831
class WrapperProperty:
2932
def __init__(
3033
self, name: str, description: str, default_value: Optional[Any] = None
@@ -41,34 +44,29 @@ def get(self, props: Properties) -> Optional[str]:
4144
return props.get(self.name, self.default_value)
4245
return props.get(self.name)
4346

47+
def get_typed(self, props: Properties, type_class: Type[T]) -> T:
48+
value = props.get(self.name, self.default_value) if self.default_value else props.get(self.name)
49+
if value is None:
50+
return -1 if type_class in (int, float) else None
51+
if type_class == bool:
52+
if isinstance(value, bool):
53+
return value
54+
return value.lower() == "true" if isinstance(value, str) else bool(value)
55+
return type_class(value)
56+
4457
def get_or_default(self, props: Properties) -> str:
4558
if not self.default_value:
4659
raise ValueError(f"No default value found for property {self}")
4760
return props.get(self.name, self.default_value)
4861

4962
def get_int(self, props: Properties) -> int:
50-
if self.default_value:
51-
return int(props.get(self.name, self.default_value))
52-
53-
val = props.get(self.name)
54-
return int(val) if val else -1
63+
return self.get_typed(props, int)
5564

5665
def get_float(self, props: Properties) -> float:
57-
if self.default_value:
58-
return float(props.get(self.name, self.default_value))
59-
60-
val = props.get(self.name)
61-
return float(val) if val else -1
66+
return self.get_typed(props, float)
6267

6368
def get_bool(self, props: Properties) -> bool:
64-
if not self.default_value:
65-
value = props.get(self.name)
66-
else:
67-
value = props.get(self.name, self.default_value)
68-
if isinstance(value, bool):
69-
return value
70-
else:
71-
return value is not None and value.lower() == "true"
69+
return self.get_typed(props, bool)
7270

7371
def set(self, props: Properties, value: Any):
7472
props[self.name] = value

0 commit comments

Comments
 (0)