Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/compute/set-detailed-state.ts
Views: 687
/*1Set the state of a component. This is mainly used from the backend to convey information to user2about what is going on in a compute server.34Example use, where 'sk-eTUKbl2lkP9TgvFJ00001n' is a project api key.56curl -sk -u sk-eTUKbl2lkP9TgvFJ00001n: -d '{"id":"13","name":"foo","value":"bar389"}' -H 'Content-Type: application/json' https://cocalc.com/api/v2/compute/set-detailed-state7*/89import getProjectOrAccountId from "lib/account/get-account";10import setDetailedState from "@cocalc/server/compute/set-detailed-state";11import getParams from "lib/api/get-params";12import isCollaborator from "@cocalc/server/projects/is-collaborator";1314import { apiRoute, apiRouteOperation } from "lib/api";15import { OkStatus } from "lib/api/status";16import {17SetDetailedServerStateInputSchema,18SetDetailedServerStateOutputSchema,19} from "lib/api/schema/compute/set-detailed-state";2021async function handle(req, res) {22try {23res.json(await get(req));24} catch (err) {25res.json({ error: `${err.message}` });26return;27}28}2930async function get(req) {31// This is a bit complicated because it can be used by a project api key,32// in which case project_id must not be passed in, or it can be auth'd33// by a normal api key or account, in which case project_id must be passed in.34// TODO: I don't think this is ever in practice used by anything but a project -- maybe by35// account just for testing?36const project_or_account_id = await getProjectOrAccountId(req);37if (!project_or_account_id) {38throw Error("invalid auth");39}40const {41id,42name,43state,44extra,45timeout,46progress,47project_id: project_id0,48} = getParams(req);4950let project_id;51if (!project_id0) {52project_id = project_or_account_id;53} else {54if (55!(await isCollaborator({56account_id: project_or_account_id,57project_id: project_id0,58}))59) {60throw Error("must be a collaborator on project with compute server");61}62project_id = project_id0;63}6465await setDetailedState({66project_id,67id,68name,69state,70extra,71timeout,72progress,73});74return OkStatus;75}7677export default apiRoute({78setDetailedState: apiRouteOperation({79method: "POST",80openApiOperation: {81tags: ["Compute"],82},83})84.input({85contentType: "application/json",86body: SetDetailedServerStateInputSchema,87})88.outputs([89{90status: 200,91contentType: "application/json",92body: SetDetailedServerStateOutputSchema,93},94])95.handler(handle),96});979899