@@ -72,14 +72,39 @@ def start_matlab_proxy_for_testing():
7272 return url , matlab_proxy_base_url , headers
7373
7474
75+ def _start_matlab_proxy_using_jupyter (url , headers ):
76+ """
77+ Start matlab-proxy using jupyter server which started the current kernel
78+ process by sending HTTP request to the endpoint registered through
79+ jupyter-matlab-proxy.
80+
81+ Args:
82+ url (string): URL to send HTTP request
83+ headers (dict): HTTP headers required for the request
84+
85+ Returns:
86+ bool: True if jupyter server has successfully started matlab-proxy else False.
87+ """
88+ # This is content that is present in the matlab-proxy index.html page which
89+ # can be used to validate a proper response.
90+ matlab_proxy_index_page_identifier = "MWI_MATLAB_PROXY_IDENTIFIER"
91+
92+ # send request to the matlab-proxy endpoint to make sure it is available.
93+ # If matlab-proxy is not started, jupyter-server starts it at this point.
94+ resp = requests .get (url , headers = headers , verify = False )
95+ return (
96+ resp .status_code == requests .codes .OK
97+ and matlab_proxy_index_page_identifier in resp .text
98+ )
99+
100+
75101def start_matlab_proxy ():
76102 """
77103 Start matlab-proxy registered with the jupyter server which started the
78104 current kernel process.
79105
80106 Raises:
81107 MATLABConnectionError: Occurs when kernel is not started by jupyter server.
82- HTTPError: Occurs when kernel cannot connect with matlab-proxy.
83108
84109 Returns:
85110 Tuple (string, string, dict):
@@ -156,46 +181,27 @@ def start_matlab_proxy():
156181 base_url = nb_server ["base_url" ],
157182 )
158183
159- # Fetch JupyterHub API token for HTTP request authentication
160- # incase the jupyter server is started by JupyterHub.
161- jh_api_token = os .getenv ("JUPYTERHUB_API_TOKEN" )
162-
163- # set the token to be used during communication with Jupyter
164- # In environments where tokens are set for both nb_server & JupyterHub
165- # precedence is given to the nb_server token
166- if nb_server ["token" ]:
167- token = nb_server ["token" ]
168- elif jh_api_token :
169- token = jh_api_token
170- else :
171- token = None
172-
173- if token :
174- headers = {
175- "Authorization" : f"token { token } " ,
176- }
177- else :
178- headers = None
184+ available_tokens = {
185+ "jupyter_server" : nb_server .get ("token" ),
186+ "jupyterhub" : os .getenv ("JUPYTERHUB_API_TOKEN" ),
187+ "default" : None ,
188+ }
179189
180- # This is content that is present in the matlab-proxy index.html page which
181- # can be used to validate a proper response.
182- matlab_proxy_index_page_identifier = "MWI_MATLAB_PROXY_IDENTIFIER"
190+ for token in available_tokens .values ():
191+ if token :
192+ headers = {"Authorization" : f"token { token } " }
193+ else :
194+ headers = None
183195
184- # send request to the matlab-proxy endpoint to make sure it is available.
185- # If matlab-proxy is not started, jupyter-server starts it at this point.
186- resp = requests .get (url , headers = headers , verify = False )
187- if resp .status_code == requests .codes .OK :
188- # Verify that the returned value is correct
189- if matlab_proxy_index_page_identifier not in resp .text :
190- raise MATLABConnectionError (
191- """
196+ if _start_matlab_proxy_using_jupyter (url , headers ):
197+ return url , nb_server ["base_url" ], headers
198+
199+ raise MATLABConnectionError (
200+ """
192201 Error: MATLAB Kernel could not communicate with MATLAB.
193202 Reason: Possibly due to invalid jupyter security tokens.
194203 """
195- )
196- return url , nb_server ["base_url" ], headers
197- else :
198- resp .raise_for_status ()
204+ )
199205
200206
201207class MATLABKernel (ipykernel .kernelbase .Kernel ):
@@ -233,7 +239,7 @@ def __init__(self, *args, **kwargs):
233239 self .matlab_status ,
234240 self .matlab_proxy_has_error ,
235241 ) = mwi_comm_helpers .fetch_matlab_proxy_status (self .murl , self .headers )
236- except ( MATLABConnectionError , HTTPError ) as err :
242+ except MATLABConnectionError as err :
237243 self .startup_error = err
238244
239245 # ipykernel Interface API
0 commit comments