@@ -128,27 +128,37 @@ def start(self) -> 'Container':
128128 if self ._container is not None :
129129 return self
130130
131- try :
132- # Try to pull the latest image
133- self ._docker_client .images .pull (self ._image_name , self ._image_tag )
134- except errors .APIError as e :
135- # Check if we already have the image locally
136- try :
137- image_name = f"{ self ._image_name } :{ self ._image_tag } "
138- self ._docker_client .images .get (image_name )
139- # If we get here, the image exists locally, so we can continue
140- logging .warning (f"Warning: Could not pull image: { e } . Using locally cached image." )
141- except errors .ImageNotFound :
142- # If the image isn't available locally either, we can't continue
143- raise RuntimeError (f'Could not pull image and no local image available: { e } ' ) from e
144-
131+ # Fix too-many-try-statements by breaking into separate methods
132+ self ._pull_or_get_image ()
145133 self ._cleanup_existing_containers ()
146134 self ._create_container ()
147135 self ._fetch_mapped_port ()
148136 self ._wait_for_http ('/api/v1/ping' , timeout = 20 )
149137
150138 return self
151139
140+ def _pull_or_get_image (self ) -> None :
141+ """Pull the image or use local image if available."""
142+ try :
143+ # Try to pull the latest image
144+ self ._docker_client .images .pull (self ._image_name , self ._image_tag )
145+ except errors .APIError as e :
146+ # Handle the API error
147+ self ._handle_image_pull_error (e )
148+
149+ def _handle_image_pull_error (self , error ):
150+ """Handle error when pulling image fails by checking for local version."""
151+ # Check if we already have the image locally
152+ image_name = f"{ self ._image_name } :{ self ._image_tag } "
153+ try :
154+ self ._docker_client .images .get (image_name )
155+ except errors .ImageNotFound :
156+ # If the image isn't available locally either, we can't continue
157+ raise RuntimeError (
158+ f'Could not pull image and no local image available: { error } ' ) from error
159+ # If we get here, the image exists locally, so we can continue
160+ logging .warning ("Warning: Could not pull image: %s. Using locally cached image." , error )
161+
152162 def stop (self ) -> None :
153163 self ._stop_and_remove_container ()
154164
@@ -159,16 +169,16 @@ def _stop_and_remove_container(self) -> None:
159169 try :
160170 self ._container .stop ()
161171 except errors .NotFound as e :
162- logging .warning (f' Warning: Container not found while stopping: { e } ' )
172+ logging .warning (" Warning: Container not found while stopping: %s" , e )
163173 except errors .APIError as e :
164- logging .warning (f' Warning: API error while stopping container: { e } ' )
174+ logging .warning (" Warning: API error while stopping container: %s" , e )
165175
166176 try :
167177 self ._container .remove ()
168178 except errors .NotFound as e :
169- logging .warning (f' Warning: Container not found while removing: { e } ' )
179+ logging .warning (" Warning: Container not found while removing: %s" , e )
170180 except errors .APIError as e :
171- logging .warning (f' Warning: API error while removing container: { e } ' )
181+ logging .warning (" Warning: API error while removing container: %s" , e )
172182
173183 self ._container = None
174184 self ._mapped_port = None
@@ -192,24 +202,28 @@ def _wait_for_http(self, path: str, timeout: int) -> None:
192202 if time .time () - start_time >= timeout :
193203 break
194204
195- response = None
196- status_code = None
197- try :
198- response = requests .get (url , timeout = 2 )
199- except requests .RequestException :
200- pass
201-
202- if response is not None :
203- status_code = response .status_code
204-
205- if response is not None and status_code == HTTPStatus .OK :
205+ # Extract request into a helper method to reduce try statements
206+ if self ._check_endpoint_available (url ):
206207 return
207208
208209 time .sleep (0.5 )
209210
210211 self ._stop_and_remove_container ()
211212 raise TimeoutError (f'Service failed to become ready within { timeout } seconds' )
212213
214+ def _check_endpoint_available (self , url : str ) -> bool :
215+ """Check if an HTTP endpoint is available."""
216+ try :
217+ response = requests .get (url , timeout = 2 )
218+ except requests .RequestException :
219+ return False
220+ return self ._check_response_ok (response )
221+
222+ # pylint: disable=R6301
223+ def _check_response_ok (self , response ) -> bool :
224+ """Check if the HTTP response indicates success."""
225+ return response is not None and response .status_code == HTTPStatus .OK
226+
213227 def with_api_token (self , token : str ) -> 'Container' :
214228 self ._api_token = token
215229 return self
0 commit comments