Skip to content

Commit c2605bb

Browse files
committed
Refactoring
* Make TokenApiHandler derive from GraphConnectionHandler instead of using an internal reference.
1 parent dcd7680 commit c2605bb

File tree

1 file changed

+50
-55
lines changed

1 file changed

+50
-55
lines changed

src/hiro_graph_client/clientlib.py

+50-55
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def __init__(self,
132132
:param log_communication_on_error: Log socket communication when an error (status_code of HTTP Response) is
133133
detected. Default is not to do this.
134134
:param max_tries: Max tries for BACKOFF. Default is 2.
135-
:param abstract_api: Set all parameters by copying them from another instance. Overrides all other parameters.
135+
:param abstract_api: Set all parameters by copying them from the instance given by this parameter. Overrides
136+
all other parameters.
136137
"""
137138
self._root_url = getattr(abstract_api, '_root_url', root_url)
138139
self._session = getattr(abstract_api, '_session', session)
@@ -671,6 +672,8 @@ class GraphConnectionHandler(AbstractAPI):
671672

672673
_pool_block = False
673674

675+
_version_info: dict = None
676+
674677
def __init__(self,
675678
root_url: str = None,
676679
raise_exceptions: bool = True,
@@ -683,7 +686,8 @@ def __init__(self,
683686
log_communication_on_error: bool = None,
684687
max_tries: int = None,
685688
pool_maxsize: int = None,
686-
pool_block: bool = None):
689+
pool_block: bool = None,
690+
connection_handler=None):
687691
"""
688692
Constructor
689693
@@ -720,7 +724,11 @@ def __init__(self,
720724
Default is 10. *pool_maxsize* is ignored when *_session* is set.
721725
:param pool_block: Block any connections that exceed the pool_maxsize. Default is False: Allow more connections,
722726
but do not cache them. See requests.adapters.HTTPAdapter. *pool_block* is ignored when *_session* is set.
727+
:param connection_handler: Copy parameters from this already existing connection handler. Overrides all other
728+
parameters.
723729
"""
730+
root_url = getattr(connection_handler, '_root_url', root_url)
731+
724732
if not root_url:
725733
raise ValueError("'root_url' must not be empty.")
726734

@@ -732,19 +740,24 @@ def __init__(self,
732740
session = requests.Session()
733741
session.mount(root_url, adapter)
734742

735-
super().__init__(root_url=root_url,
736-
session=session,
737-
raise_exceptions=raise_exceptions,
738-
proxies=proxies,
739-
timeout=timeout,
740-
headers=headers,
741-
client_name=client_name,
742-
ssl_config=ssl_config,
743-
log_communication_on_error=log_communication_on_error,
744-
max_tries=max_tries)
743+
super().__init__(
744+
root_url=root_url,
745+
session=session,
746+
raise_exceptions=raise_exceptions,
747+
proxies=proxies,
748+
timeout=timeout,
749+
headers=headers,
750+
client_name=client_name,
751+
ssl_config=ssl_config,
752+
log_communication_on_error=log_communication_on_error,
753+
max_tries=max_tries,
754+
abstract_api=connection_handler
755+
)
756+
757+
self.custom_endpoints = getattr(connection_handler, '_custom_endpoints', custom_endpoints)
758+
self._version_info = getattr(connection_handler, '_version_info', None)
745759

746-
self._version_info = None
747-
self.custom_endpoints = custom_endpoints
760+
self.get_version()
748761

749762
@staticmethod
750763
def _remove_slash(endpoint: str) -> str:
@@ -840,13 +853,14 @@ def _construct_result(_endpoint: str, _protocol: str) -> Tuple[
840853
# REST API operations
841854
###############################################################################################################
842855

843-
def get_version(self) -> dict:
856+
def get_version(self, force_update: bool = False) -> dict:
844857
"""
845858
HIRO REST query API: `GET self._endpoint + '/api/version'`
846859
860+
:param force_update: Force updating the internal cache with version_info via API request.
847861
:return: The result payload
848862
"""
849-
if not self._version_info:
863+
if not self._version_info or force_update:
850864
url = self._root_url + '/api/version'
851865
self._version_info = self.get(url)
852866

@@ -857,14 +871,11 @@ def get_version(self) -> dict:
857871
# TokenApiHandler classes
858872
###################################################################################################################
859873

860-
class AbstractTokenApiHandler(AbstractAPI):
874+
class AbstractTokenApiHandler(GraphConnectionHandler):
861875
"""
862876
Root class for all TokenApiHandler classes. This adds token handling.
863877
"""
864878

865-
_connection_handler: GraphConnectionHandler
866-
"""Reference to an GraphConnectionHandler containing version information and requests.Session"""
867-
868879
def __init__(self,
869880
root_url: str = None,
870881
raise_exceptions: bool = True,
@@ -917,11 +928,12 @@ def __init__(self,
917928
:param pool_block: Block any connections that exceed the pool_maxsize. Default is False: Allow more connections,
918929
but do not cache them. See requests.adapters.HTTPAdapter. *pool_block* is ignored when
919930
*connection_handler* is set and already contains a _session.
920-
:param connection_handler: Use an already existing connection handler. This feature allows for several distinct
921-
TokenApiHandlers to operate on the same connection without querying unnecessary version information for
922-
each or building their own requests.Sessions. Overrides all other parameters.
931+
:param connection_handler: Copy all parameters from this already existing connection handler. This feature
932+
allows for several distinct TokenApiHandlers to operate on the same connection without querying
933+
unnecessary version information for each or building their own requests.Sessions. Overrides all other
934+
parameters.
923935
"""
924-
self._connection_handler = connection_handler or GraphConnectionHandler(
936+
super().__init__(
925937
root_url=root_url,
926938
raise_exceptions=raise_exceptions,
927939
proxies=proxies,
@@ -933,30 +945,10 @@ def __init__(self,
933945
log_communication_on_error=log_communication_on_error,
934946
max_tries=max_tries,
935947
pool_maxsize=pool_maxsize,
936-
pool_block=pool_block
948+
pool_block=pool_block,
949+
connection_handler=connection_handler
937950
)
938951

939-
super().__init__(abstract_api=self._connection_handler)
940-
941-
###############################################################################################################
942-
# Access connection handler
943-
###############################################################################################################
944-
945-
def get_api_endpoint_of(self, api_name: str) -> str:
946-
return self._connection_handler.get_api_endpoint_of(api_name)
947-
948-
def get_websocket_config(self, api_name: str) -> Tuple[
949-
str,
950-
str,
951-
Optional[str],
952-
Optional[int],
953-
Optional[dict]
954-
]:
955-
return self._connection_handler.get_websocket_config(api_name)
956-
957-
def get_version(self):
958-
return self._connection_handler.get_version()
959-
960952
###############################################################################################################
961953
# Token handling
962954
###############################################################################################################
@@ -1049,9 +1041,10 @@ def __init__(self,
10491041
:param pool_block: Block any connections that exceed the pool_maxsize. Default is False: Allow more connections,
10501042
but do not cache them. See requests.adapters.HTTPAdapter. *pool_block* is ignored when
10511043
*connection_handler* is set and already contains a _session.
1052-
:param connection_handler: Use an already existing connection handler. This feature allows for several distinct
1053-
TokenApiHandlers to operate on the same connection without querying unnecessary version information for
1054-
each or building their own requests.Sessions. Overrides all other connection specific parameters.
1044+
:param connection_handler: Copy all parameters from this already existing connection handler. This feature
1045+
allows for several distinct TokenApiHandlers to operate on the same connection without querying
1046+
unnecessary version information for each or building their own requests.Sessions. Overrides all other
1047+
parameters.
10551048
"""
10561049
super().__init__(
10571050
root_url=root_url,
@@ -1132,9 +1125,10 @@ def __init__(self,
11321125
:param pool_block: Block any connections that exceed the pool_maxsize. Default is False: Allow more connections,
11331126
but do not cache them. See requests.adapters.HTTPAdapter. *pool_block* is ignored when
11341127
*connection_handler* is set and already contains a _session.
1135-
:param connection_handler: Use an already existing connection handler. This feature allows for several distinct
1136-
TokenApiHandlers to operate on the same connection without querying unnecessary version information for
1137-
each or building their own requests.Sessions. Overrides all other connection specific parameters.
1128+
:param connection_handler: Copy all parameters from this already existing connection handler. This feature
1129+
allows for several distinct TokenApiHandlers to operate on the same connection without querying
1130+
unnecessary version information for each or building their own requests.Sessions. Overrides all other
1131+
parameters.
11381132
"""
11391133
super().__init__(
11401134
root_url=root_url,
@@ -1343,9 +1337,10 @@ def __init__(self,
13431337
:param pool_block: Block any connections that exceed the pool_maxsize. Default is False: Allow more connections,
13441338
but do not cache them. See requests.adapters.HTTPAdapter. *pool_block* is ignored when
13451339
*connection_handler* is set and already contains a _session.
1346-
:param connection_handler: Use an already existing connection handler. This feature allows for several distinct
1347-
TokenApiHandlers to operate on the same connection without querying unnecessary version information for
1348-
each or building their own requests.Sessions. Overrides all other connection specific parameters.
1340+
:param connection_handler: Copy all parameters from this already existing connection handler. This feature
1341+
allows for several distinct TokenApiHandlers to operate on the same connection without querying
1342+
unnecessary version information for each or building their own requests.Sessions. Overrides all other
1343+
parameters.
13491344
"""
13501345
super().__init__(
13511346
root_url=root_url,

0 commit comments

Comments
 (0)