@@ -72,14 +72,39 @@ def start_matlab_proxy_for_testing():
72
72
return url , matlab_proxy_base_url , headers
73
73
74
74
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
+
75
101
def start_matlab_proxy ():
76
102
"""
77
103
Start matlab-proxy registered with the jupyter server which started the
78
104
current kernel process.
79
105
80
106
Raises:
81
107
MATLABConnectionError: Occurs when kernel is not started by jupyter server.
82
- HTTPError: Occurs when kernel cannot connect with matlab-proxy.
83
108
84
109
Returns:
85
110
Tuple (string, string, dict):
@@ -156,46 +181,27 @@ def start_matlab_proxy():
156
181
base_url = nb_server ["base_url" ],
157
182
)
158
183
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
+ }
179
189
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
183
195
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
+ """
192
201
Error: MATLAB Kernel could not communicate with MATLAB.
193
202
Reason: Possibly due to invalid jupyter security tokens.
194
203
"""
195
- )
196
- return url , nb_server ["base_url" ], headers
197
- else :
198
- resp .raise_for_status ()
204
+ )
199
205
200
206
201
207
class MATLABKernel (ipykernel .kernelbase .Kernel ):
@@ -233,7 +239,7 @@ def __init__(self, *args, **kwargs):
233
239
self .matlab_status ,
234
240
self .matlab_proxy_has_error ,
235
241
) = mwi_comm_helpers .fetch_matlab_proxy_status (self .murl , self .headers )
236
- except ( MATLABConnectionError , HTTPError ) as err :
242
+ except MATLABConnectionError as err :
237
243
self .startup_error = err
238
244
239
245
# ipykernel Interface API
0 commit comments