@jupyterlab/services
Version:
Client APIs for the Jupyter services REST APIs
264 lines • 11.1 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.KernelAPIClient = exports.KERNEL_SERVICE_URL = void 0;
exports.listRunning = listRunning;
exports.startNew = startNew;
exports.restartKernel = restartKernel;
exports.interruptKernel = interruptKernel;
exports.shutdownKernel = shutdownKernel;
exports.getKernelModel = getKernelModel;
const serverconnection_1 = require("../serverconnection");
const coreutils_1 = require("@jupyterlab/coreutils");
const validate_1 = require("./validate");
/**
* The url for the kernel service.
*/
exports.KERNEL_SERVICE_URL = 'api/kernels';
/**
* Fetch the running kernels.
*
* @param settings - The optional server settings.
*
* @returns A promise that resolves with the list of running kernels.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async function listRunning(settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL);
const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings);
if (response.status !== 200) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
(0, validate_1.validateModels)(data);
return data;
}
/**
* Start a new kernel.
*
* @param options - The options used to create the kernel.
*
* @returns A promise that resolves with a kernel connection object.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async function startNew(options = {}, settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL);
const init = {
method: 'POST',
body: JSON.stringify(options)
};
const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
if (response.status !== 201) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
(0, validate_1.validateModel)(data);
return data;
}
/**
* Restart a kernel.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response (and thus after a restart) and rejected otherwise.
*/
async function restartKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id), 'restart');
const init = { method: 'POST' };
const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
if (response.status !== 200) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
(0, validate_1.validateModel)(data);
}
/**
* Interrupt a kernel.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async function interruptKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id), 'interrupt');
const init = { method: 'POST' };
const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
if (response.status !== 204) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
}
/**
* Shut down a kernel.
*
* @param id - The id of the running kernel.
*
* @param settings - The server settings for the request.
*
* @returns A promise that resolves when the kernel is shut down.
*
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async function shutdownKernel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id));
const init = { method: 'DELETE' };
const response = await serverconnection_1.ServerConnection.makeRequest(url, init, settings);
if (response.status === 404) {
const msg = `The kernel "${id}" does not exist on the server`;
console.warn(msg);
}
else if (response.status !== 204) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
}
/**
* Get a full kernel model from the server by kernel id string.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async function getKernelModel(id, settings = serverconnection_1.ServerConnection.makeSettings()) {
const url = coreutils_1.URLExt.join(settings.baseUrl, exports.KERNEL_SERVICE_URL, encodeURIComponent(id));
const response = await serverconnection_1.ServerConnection.makeRequest(url, {}, settings);
if (response.status === 404) {
return undefined;
}
else if (response.status !== 200) {
const err = await serverconnection_1.ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
(0, validate_1.validateModel)(data);
return data;
}
/**
* The kernel API client.
*
* #### Notes
* Use this class to interact with the Jupyter Server Kernel API.
* This class adheres to the Jupyter Server API endpoints.
*/
class KernelAPIClient {
/**
* Create a new kernel API client.
*
* @param options - The options used to create the client.
*/
constructor(options = {}) {
var _a;
this.serverSettings =
(_a = options.serverSettings) !== null && _a !== void 0 ? _a : serverconnection_1.ServerConnection.makeSettings();
}
/**
* List the running kernels.
*
* @returns A promise that resolves with the list of running kernel models.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async listRunning() {
return listRunning(this.serverSettings);
}
/**
* Get a kernel model.
*
* @param id - The id of the kernel of interest.
*
* @returns A promise that resolves with the kernel model.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
* If the kernel does not exist on the server, the promise is resolved with `undefined`.
*/
async getModel(id) {
return getKernelModel(id, this.serverSettings);
}
/**
* Start a new kernel.
*
* @param options - The options used to create the kernel.
*
* @returns A promise that resolves with the kernel model.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async startNew(options = {}) {
return startNew(options, this.serverSettings);
}
/**
* Restart a kernel.
*
* @param id - The id of the kernel to restart.
*
* @returns A promise that resolves when the kernel is restarted.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async restart(id) {
return restartKernel(id, this.serverSettings);
}
/**
* Interrupt a kernel.
*
* @param id - The id of the kernel to interrupt.
*
* @returns A promise that resolves when the kernel is interrupted.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async interrupt(id) {
return interruptKernel(id, this.serverSettings);
}
/**
* Shut down a kernel by id.
*
* @param id - The id of the kernel to shut down.
*
* @returns A promise that resolves when the kernel is shut down.
*
* #### Notes
* 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.
*
* The promise is fulfilled on a valid response and rejected otherwise.
*/
async shutdown(id) {
return shutdownKernel(id, this.serverSettings);
}
}
exports.KernelAPIClient = KernelAPIClient;
//# sourceMappingURL=restapi.js.map
;