-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathrestapi.ts
144 lines (134 loc) · 4.26 KB
/
restapi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ServerConnection } from '../serverconnection';
import { Session } from '.';
import { URLExt } from '@jupyterlab/coreutils';
import { updateLegacySessionModel, validateModel } from './validate';
type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
/**
* The url for the session service.
*/
export const SESSION_SERVICE_URL = 'api/sessions';
/**
* List the running sessions.
*/
export async function listRunning(
settings: ServerConnection.ISettings = ServerConnection.makeSettings()
): Promise<Session.IModel[]> {
const url = URLExt.join(settings.baseUrl, SESSION_SERVICE_URL);
const response = await ServerConnection.makeRequest(url, {}, settings);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
if (!Array.isArray(data)) {
throw new Error('Invalid Session list');
}
data.forEach(m => {
updateLegacySessionModel(m);
validateModel(m);
});
return data;
}
/**
* Get a session url.
*/
export function getSessionUrl(baseUrl: string, id: string): string {
const servicesBase = URLExt.join(baseUrl, SESSION_SERVICE_URL);
const result = URLExt.join(servicesBase, id);
if (!result.startsWith(servicesBase)) {
throw new Error('Can only be used for services requests');
}
return result;
}
/**
* Shut down a session by id.
*/
export async function shutdownSession(
id: string,
settings: ServerConnection.ISettings = ServerConnection.makeSettings()
): Promise<void> {
const url = getSessionUrl(settings.baseUrl, id);
const init = { method: 'DELETE' };
const response = await ServerConnection.makeRequest(url, init, settings);
if (response.status === 404) {
const data = await response.json();
const msg =
data.message ?? `The session "${id}"" does not exist on the server`;
console.warn(msg);
} else if (response.status === 410) {
throw new ServerConnection.ResponseError(
response,
'The kernel was deleted but the session was not'
);
} else if (response.status !== 204) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
}
/**
* Get a full session model from the server by session id string.
*/
export async function getSessionModel(
id: string,
settings: ServerConnection.ISettings = ServerConnection.makeSettings()
): Promise<Session.IModel> {
const url = getSessionUrl(settings.baseUrl, id);
const response = await ServerConnection.makeRequest(url, {}, settings);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
updateLegacySessionModel(data);
validateModel(data);
return data;
}
/**
* Create a new session, or return an existing session if the session path
* already exists.
*/
export async function startSession(
options: Session.ISessionOptions,
settings: ServerConnection.ISettings = ServerConnection.makeSettings()
): Promise<Session.IModel> {
const url = URLExt.join(settings.baseUrl, SESSION_SERVICE_URL);
const init = {
method: 'POST',
body: JSON.stringify(options)
};
const response = await ServerConnection.makeRequest(url, init, settings);
if (response.status !== 201) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
updateLegacySessionModel(data);
validateModel(data);
return data;
}
/**
* Send a PATCH to the server, updating the session path or the kernel.
*/
export async function updateSession(
model: Pick<Session.IModel, 'id'> & DeepPartial<Omit<Session.IModel, 'id'>>,
settings: ServerConnection.ISettings = ServerConnection.makeSettings()
): Promise<Session.IModel> {
const url = getSessionUrl(settings.baseUrl, model.id);
const init = {
method: 'PATCH',
body: JSON.stringify(model)
};
const response = await ServerConnection.makeRequest(url, init, settings);
if (response.status !== 200) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}
const data = await response.json();
updateLegacySessionModel(data);
validateModel(data);
return data;
}