@@ -757,22 +757,25 @@ async def _is_retryable(self, method: str, exception: Exception) -> bool:
757
757
758
758
async def gateway_request (endpoint : str , ** kwargs : ty .Any ) -> HTTPResponse :
759
759
"""Make an async request to kernel gateway endpoint, returns a response"""
760
- kwargs = GatewayClient .instance ().load_connection_args (** kwargs )
760
+ gateway_client = GatewayClient .instance ()
761
+ kwargs = gateway_client .load_connection_args (** kwargs )
761
762
rhc = RetryableHTTPClient ()
762
763
try :
763
764
response = await rhc .fetch (endpoint , ** kwargs )
764
- GatewayClient . instance () .emit (
765
+ gateway_client .emit (
765
766
data = {STATUS_KEY : SUCCESS_STATUS , STATUS_CODE_KEY : 200 , MESSAGE_KEY : "success" }
766
767
)
767
768
# Trap a set of common exceptions so that we can inform the user that their Gateway url is incorrect
768
769
# or the server is not running.
769
770
# NOTE: We do this here since this handler is called during the server's startup and subsequent refreshes
770
771
# of the tree view.
771
772
except HTTPClientError as e :
772
- GatewayClient . instance () .emit (
773
+ gateway_client .emit (
773
774
data = {STATUS_KEY : ERROR_STATUS , STATUS_CODE_KEY : e .code , MESSAGE_KEY : str (e .message )}
774
775
)
775
- error_reason = f"Exception while attempting to connect to Gateway server url '{ GatewayClient .instance ().url } '"
776
+ error_reason = (
777
+ f"Exception while attempting to connect to Gateway server url '{ gateway_client .url } '"
778
+ )
776
779
error_message = e .message
777
780
if e .response :
778
781
try :
@@ -788,38 +791,39 @@ async def gateway_request(endpoint: str, **kwargs: ty.Any) -> HTTPResponse:
788
791
"Ensure gateway url is valid and the Gateway instance is running." ,
789
792
) from e
790
793
except ConnectionError as e :
791
- GatewayClient . instance () .emit (
794
+ gateway_client .emit (
792
795
data = {STATUS_KEY : ERROR_STATUS , STATUS_CODE_KEY : 503 , MESSAGE_KEY : str (e )}
793
796
)
794
797
raise web .HTTPError (
795
798
503 ,
796
- f"ConnectionError was received from Gateway server url '{ GatewayClient . instance () .url } '. "
799
+ f"ConnectionError was received from Gateway server url '{ gateway_client .url } '. "
797
800
"Check to be sure the Gateway instance is running." ,
798
801
) from e
799
802
except gaierror as e :
800
- GatewayClient . instance () .emit (
803
+ gateway_client .emit (
801
804
data = {STATUS_KEY : ERROR_STATUS , STATUS_CODE_KEY : 404 , MESSAGE_KEY : str (e )}
802
805
)
803
806
raise web .HTTPError (
804
807
404 ,
805
- f"The Gateway server specified in the gateway_url '{ GatewayClient . instance () .url } ' doesn't "
808
+ f"The Gateway server specified in the gateway_url '{ gateway_client .url } ' doesn't "
806
809
f"appear to be valid. Ensure gateway url is valid and the Gateway instance is running." ,
807
810
) from e
808
811
except Exception as e :
809
- GatewayClient . instance () .emit (
812
+ gateway_client .emit (
810
813
data = {STATUS_KEY : ERROR_STATUS , STATUS_CODE_KEY : 505 , MESSAGE_KEY : str (e )}
811
814
)
812
815
logging .getLogger ("ServerApp" ).error (
813
- f"Exception while trying to launch kernel via Gateway URL { GatewayClient .instance ().url } , { e } " ,
816
+ "Exception while trying to launch kernel via Gateway URL %s: %s" ,
817
+ gateway_client .url ,
814
818
e ,
815
819
)
816
820
raise e
817
821
818
- if GatewayClient . instance () .accept_cookies :
822
+ if gateway_client .accept_cookies :
819
823
# Update cookies on GatewayClient from server if configured.
820
824
cookie_values = response .headers .get ("Set-Cookie" )
821
825
if cookie_values :
822
826
cookie : SimpleCookie = SimpleCookie ()
823
827
cookie .load (cookie_values )
824
- GatewayClient . instance () .update_cookies (cookie )
828
+ gateway_client .update_cookies (cookie )
825
829
return response
0 commit comments