This is an automated email from the ASF dual-hosted git repository. bossenti pushed a commit to branch 1259-verify-authentication-on-startup-of-python-client in repository https://gitbox.apache.org/repos/asf/streampipes.git
commit 3092040c7bbcbc89a88e4298c1c0ee5511573d88 Author: bossenti <[email protected]> AuthorDate: Mon Feb 27 22:19:00 2023 +0100 tmp Signed-off-by: bossenti <[email protected]> --- .../streampipes/client/client.py | 29 ++++++++++++++++++++++ .../tests/client/test_endpoint.py | 2 ++ 2 files changed, 31 insertions(+) diff --git a/streampipes-client-python/streampipes/client/client.py b/streampipes-client-python/streampipes/client/client.py index 9f24dbd58..e24b6e3e4 100644 --- a/streampipes-client-python/streampipes/client/client.py +++ b/streampipes-client-python/streampipes/client/client.py @@ -110,6 +110,35 @@ class StreamPipesClient: self.dataStreamApi = DataStreamEndpoint(parent_client=self) self.versionApi = VersionEndpoint(parent_client=self) + self.server_version = self._get_server_version() + + def _get_server_version(self) -> str: + """Connects to the StreamPipes server and retrieves its version. + + In addition to querying the server version, this method implicitly checks the specified credentials. + + Returns + ------- + sp_version: str + version of the connected StreamPipes instance + + """ + + # retrieve metadata from the API via the Streampipes server + # as a side effect, the specified credentials are also tested to ensure that authentication is successful. + version_dict = self.versionApi.get(identifier="").to_dict(use_source_names=False) + + # remove SNAPSHOT-suffix if present + sp_version = version_dict["backend_version"].replace("-SNAPSHOT", "") + + logger.info( + "The StreamPipes version was successfully retrieved from the backend: %s. " + "By means of that, authentication via the provided credentials is also tested successfully.", + sp_version, + ) + + return sp_version + @staticmethod def _set_up_logging(logging_level: int) -> None: """Configures the logging behavior of the `StreamPipesClient`. diff --git a/streampipes-client-python/tests/client/test_endpoint.py b/streampipes-client-python/tests/client/test_endpoint.py index 16a2b6d9d..9db949509 100644 --- a/streampipes-client-python/tests/client/test_endpoint.py +++ b/streampipes-client-python/tests/client/test_endpoint.py @@ -418,6 +418,7 @@ class TestMessagingEndpoint(TestCase): ) ) + @patch("streampipes.client.client.Session", autospec=True) def test_messaging_endpoint_happy_path(self): demo_endpoint = MessagingEndpoint(parent_client=self.client) @@ -425,6 +426,7 @@ class TestMessagingEndpoint(TestCase): self.assertTrue(isinstance(demo_endpoint.broker, NatsBroker)) + @patch("streampipes.client.client.Session", autospec=True) def test_messaging_endpoint_missing_configure(self): demo_endpoint = MessagingEndpoint(parent_client=self.client)
