@@ -61,30 +61,38 @@ def __init__(self, **kwargs) -> None:
6161 super ().__init__ ()
6262 config = PodmanConfig ()
6363
64- api_kwargs = kwargs .copy ()
64+ self . api_kwargs = kwargs .copy ()
6565
66- if "connection" in api_kwargs :
67- connection = config .services [api_kwargs .get ("connection" )]
68- api_kwargs ["base_url" ] = connection .url .geturl ()
66+ if "connection" in self . api_kwargs :
67+ connection = config .services [self . api_kwargs .get ("connection" )]
68+ self . api_kwargs ["base_url" ] = connection .url .geturl ()
6969
7070 # Override configured identity, if provided in arguments
71- api_kwargs ["identity" ] = kwargs .get ("identity" , str (connection .identity ))
72- elif "base_url" not in api_kwargs :
71+ self . api_kwargs ["identity" ] = kwargs .get ("identity" , str (connection .identity ))
72+ elif "base_url" not in self . api_kwargs :
7373 path = str (Path (get_runtime_dir ()) / "podman" / "podman.sock" )
74- api_kwargs ["base_url" ] = "http+unix://" + path
75- self .api = APIClient (** api_kwargs )
74+ self .api_kwargs ["base_url" ] = "http+unix://" + path
7675
77- # Check if the connection to the Podman service is successful
7876 try :
79- SystemManager (client = self .api ).version ()
77+ self .api = APIClient (** self .api_kwargs )
78+ response = self .api .get ("_ping" )
79+
80+ if response .status_code != 200 :
81+ raise PodmanConnectionError (
82+ message = f"Unexpected response from Podman service: { response .status_code } " ,
83+ environment = os .environ ,
84+ host = self .api_kwargs .get ("base_url" ),
85+ original_error = None ,
86+ )
87+ except PodmanConnectionError :
88+ raise
8089 except Exception as e :
81- error_msg = "Failed to connect to Podman service"
8290 raise PodmanConnectionError (
83- message = error_msg ,
91+ message = f"Failed to connect to Podman service: { str ( e ) } " ,
8492 environment = os .environ ,
85- host = api_kwargs .get ("base_url" ),
93+ host = self . api_kwargs .get ("base_url" ),
8694 original_error = e ,
87- )
95+ ) from e
8896
8997 def __enter__ (self ) -> "PodmanClient" :
9098 return self
0 commit comments