1515from __future__ import annotations
1616
1717from time import perf_counter_ns , sleep
18- from typing import TYPE_CHECKING , Callable , Optional
18+ from typing import TYPE_CHECKING , Callable , Optional , TypeVar , Type
1919
2020from aws_advanced_python_wrapper .host_availability import HostAvailability
2121from aws_advanced_python_wrapper .read_write_splitting_plugin import (
2828 from aws_advanced_python_wrapper .host_list_provider import HostListProviderService
2929 from aws_advanced_python_wrapper .pep249 import Connection
3030 from aws_advanced_python_wrapper .plugin_service import PluginService
31- from aws_advanced_python_wrapper .utils .properties import Properties
31+ from aws_advanced_python_wrapper .utils .properties import Properties , WrapperProperty
3232
3333from aws_advanced_python_wrapper .errors import AwsWrapperError
3434from aws_advanced_python_wrapper .hostinfo import HostInfo , HostRole
@@ -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 = EndpointBasedConnectionHandler . _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 = EndpointBasedConnectionHandler ._verify_parameter (
57+ WrapperProperties .SRW_CONNECT_RETRY_TIMEOUT_MS , props , int , lambda x : x > 0
58+ )
59+ self ._connect_retry_interval_ms : int = EndpointBasedConnectionHandler ._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,29 @@ def _create_host_info(self, endpoint, role: HostRole) -> HostInfo:
305276 host = host , port = port , role = role , availability = HostAvailability .AVAILABLE
306277 )
307278
279+ T = TypeVar ('T' )
280+
281+ @staticmethod
282+ def _verify_parameter (prop : WrapperProperty , props : Properties , expected_type : Type [T ], validator = None , required = False ):
283+ value = prop .get_type (props , expected_type )
284+ if required :
285+ if value is None :
286+ raise AwsWrapperError (
287+ Messages .get_formatted (
288+ "SimpleReadWriteSplittingPlugin.MissingRequiredConfigParameter" ,
289+ prop .name ,
290+ )
291+ )
292+
293+ if validator and not validator (value ):
294+ raise ValueError (
295+ Messages .get_formatted (
296+ "SimpleReadWriteSplittingPlugin.IncorrectConfiguration" ,
297+ prop .name ,
298+ )
299+ )
300+ return value
301+
308302 def _delay (self ):
309303 sleep (self ._connect_retry_interval_ms / 1000 )
310304
0 commit comments