UNPKG

6.96 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.getKernelModel = exports.shutdownKernel = exports.interruptKernel = exports.restartKernel = exports.startNew = exports.listRunning = exports.KERNEL_SERVICE_URL = void 0;
6const serverconnection_1 = require("../serverconnection");
7const coreutils_1 = require("@jupyterlab/coreutils");
8const validate_1 = require("./validate");
9/**
10 * The url for the kernel service.
11 */
12exports.KERNEL_SERVICE_URL = 'api/kernels';
13/**
14 * Fetch the running kernels.
15 *
16 * @param settings - The optional server settings.
17 *
18 * @returns A promise that resolves with the list of running kernels.
19 *
20 * #### Notes
21 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
22 *
23 * The promise is fulfilled on a valid response and rejected otherwise.
24 */
25async function listRunning(settings = serverconnection_1.ServerConnection.makeSettings()) {
26 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL);
27 const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings);
28 if (response.status !== 200) {
29 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
30 throw err;
31 }
32 const data = await response.json();
33 (0, validate_1.validateModels)(data);
34 return data;
35}
36exports.listRunning = listRunning;
37/**
38 * Start a new kernel.
39 *
40 * @param options - The options used to create the kernel.
41 *
42 * @returns A promise that resolves with a kernel connection object.
43 *
44 * #### Notes
45 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
46 *
47 * The promise is fulfilled on a valid response and rejected otherwise.
48 */
49async function startNew(options = {}, settings = serverconnection_1.ServerConnection.makeSettings()) {
50 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL);
51 const init = {
52 method: 'POST',
53 body: JSON.stringify(options)
54 };
55 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
56 if (response.status !== 201) {
57 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
58 throw err;
59 }
60 const data = await response.json();
61 (0, validate_1.validateModel)(data);
62 return data;
63}
64exports.startNew = startNew;
65/**
66 * Restart a kernel.
67 *
68 * #### Notes
69 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
70 *
71 * The promise is fulfilled on a valid response (and thus after a restart) and rejected otherwise.
72 */
73async function restartKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
74 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id), 'restart');
75 const init = { method: 'POST' };
76 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
77 if (response.status !== 200) {
78 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
79 throw err;
80 }
81 const data = await response.json();
82 (0, validate_1.validateModel)(data);
83}
84exports.restartKernel = restartKernel;
85/**
86 * Interrupt a kernel.
87 *
88 * #### Notes
89 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
90 *
91 * The promise is fulfilled on a valid response and rejected otherwise.
92 */
93async function interruptKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
94 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id), 'interrupt');
95 const init = { method: 'POST' };
96 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
97 if (response.status !== 204) {
98 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
99 throw err;
100 }
101}
102exports.interruptKernel = interruptKernel;
103/**
104 * Shut down a kernel.
105 *
106 * @param id - The id of the running kernel.
107 *
108 * @param settings - The server settings for the request.
109 *
110 * @returns A promise that resolves when the kernel is shut down.
111 *
112 *
113 * #### Notes
114 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
115 *
116 * The promise is fulfilled on a valid response and rejected otherwise.
117 */
118async function shutdownKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
119 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id));
120 const init = { method: 'DELETE' };
121 const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
122 if (response.status === 404) {
123 const msg = `The kernel "${id}" does not exist on the server`;
124 console.warn(msg);
125 }
126 else if (response.status !== 204) {
127 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
128 throw err;
129 }
130}
131exports.shutdownKernel = shutdownKernel;
132/**
133 * Get a full kernel model from the server by kernel id string.
134 *
135 * #### Notes
136 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/kernels) and validates the response model.
137 *
138 * The promise is fulfilled on a valid response and rejected otherwise.
139 */
140async function getKernelModel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
141 const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id));
142 const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings);
143 if (response.status === 404) {
144 return undefined;
145 }
146 else if (response.status !== 200) {
147 const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
148 throw err;
149 }
150 const data = await response.json();
151 (0, validate_1.validateModel)(data);
152 return data;
153}
154exports.getKernelModel = getKernelModel;
155//# sourceMappingURL=restapi.js.map
\No newline at end of file