diff --git a/podman/client.py b/podman/client.py index 546a7fd9..87c4d17b 100644 --- a/podman/client.py +++ b/podman/client.py @@ -61,30 +61,38 @@ def __init__(self, **kwargs) -> None: super().__init__() config = PodmanConfig() - api_kwargs = kwargs.copy() + self.api_kwargs = kwargs.copy() - if "connection" in api_kwargs: - connection = config.services[api_kwargs.get("connection")] - api_kwargs["base_url"] = connection.url.geturl() + if "connection" in self.api_kwargs: + connection = config.services[self.api_kwargs.get("connection")] + self.api_kwargs["base_url"] = connection.url.geturl() # Override configured identity, if provided in arguments - api_kwargs["identity"] = kwargs.get("identity", str(connection.identity)) - elif "base_url" not in api_kwargs: + self.api_kwargs["identity"] = kwargs.get("identity", str(connection.identity)) + elif "base_url" not in self.api_kwargs: path = str(Path(get_runtime_dir()) / "podman" / "podman.sock") - api_kwargs["base_url"] = "http+unix://" + path - self.api = APIClient(**api_kwargs) + self.api_kwargs["base_url"] = "http+unix://" + path - # Check if the connection to the Podman service is successful try: - SystemManager(client=self.api).version() + self.api = APIClient(**self.api_kwargs) + response = self.api.get("_ping") + + if response.status_code != 200: + raise PodmanConnectionError( + message=f"Unexpected response from Podman service: {response.status_code}", + environment=os.environ, + host=self.api_kwargs.get("base_url"), + original_error=None, + ) + except PodmanConnectionError: + raise except Exception as e: - error_msg = "Failed to connect to Podman service" raise PodmanConnectionError( - message=error_msg, + message=f"Failed to connect to Podman service: {str(e)}", environment=os.environ, - host=api_kwargs.get("base_url"), + host=self.api_kwargs.get("base_url"), original_error=e, - ) + ) from e def __enter__(self) -> "PodmanClient": return self