UNPKG

steadybit

Version:

Command-line interface to interact with the Steadybit API

246 lines 10.7 kB
"use strict"; // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2022 Steadybit GmbH var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeExperiment = executeExperiment; exports.upsertAndExecuteExperiment = upsertAndExecuteExperiment; exports.getExperimentExecutionUsingUrl = getExperimentExecutionUsingUrl; exports.fetchExperiment = fetchExperiment; exports.updateExperiment = updateExperiment; exports.removeExperiment = removeExperiment; exports.upsertExperiment = upsertExperiment; exports.fetchExperiments = fetchExperiments; exports.fetchExecutionsForExperiment = fetchExecutionsForExperiment; exports.getExperimentExecution = getExperimentExecution; const errors_1 = require("../errors"); const http_1 = require("../api/http"); const confirm_1 = require("../prompt/confirm"); function executeExperiment(key_1, yes_1) { return __awaiter(this, arguments, void 0, function* (key, yes, allowParallelExecutions = false, forcePersist = true) { var _a, _b, _c; try { const response = yield (0, http_1.executeApiCall)({ method: 'POST', path: `/api/experiments/${encodeURIComponent(key)}/execute?forcePersist=${String(forcePersist)}&allowParallel=${String(allowParallelExecutions)}`, }); let uiLocation = 'please update your platform to get the UI location'; const body = yield response.text(); if (body && body.length > 0) { const json = JSON.parse(body); uiLocation = json.uiLocation; } return { location: (_a = response.headers.get('Location')) !== null && _a !== void 0 ? _a : '', uiLocation }; } catch (e) { if (((_b = e.response) === null || _b === void 0 ? void 0 : _b.status) === 422) { throw e; } if (!allowParallelExecutions && ((_c = (yield (0, errors_1.getExecutionErrorBody)(e))) === null || _c === void 0 ? void 0 : _c.type) === 'https://steadybit.com/problems/another-experiment-running-exception' && (yes || (yield (0, confirm_1.confirm)(`There is already an experiment running. Do you want to start ${key} in parallel?`, { defaultYes: false, defaultWhenNonInteractive: false, })))) { // try again, but run in parallel return executeExperiment(key, yes, true); } throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to run experiment (%s)', key); } }); } function upsertAndExecuteExperiment(experiment_1) { return __awaiter(this, arguments, void 0, function* (experiment, allowParallelExecutions = false, forcePersist = true) { var _a, _b, _c; try { const response = yield (0, http_1.executeApiCall)({ method: 'POST', path: `/api/experiments/execute?forcePersist=${String(forcePersist)}&allowParallel=${String(allowParallelExecutions)}`, body: experiment, }); let uiLocation = 'please update your platform to get the UI location'; let key = experiment.key; let executionId = undefined; const body = yield response.text(); if (body && body.length > 0) { const json = JSON.parse(body); uiLocation = json.uiLocation; key = json.key; executionId = json.executionId; } return { key, location: (_a = response.headers.get('Location')) !== null && _a !== void 0 ? _a : `/api/experiments/executions/${executionId}`, uiLocation, }; } catch (e) { if (((_b = e.response) === null || _b === void 0 ? void 0 : _b.status) === 422) { throw e; } if (!allowParallelExecutions && ((_c = (yield (0, errors_1.getExecutionErrorBody)(e))) === null || _c === void 0 ? void 0 : _c.type) === 'https://steadybit.com/problems/another-experiment-running-exception' && (yield (0, confirm_1.confirm)(`There is already an experiment running. Do you want to start ${experiment.key || experiment.name || 'the experiment'} in parallel?`, { defaultYes: false, defaultWhenNonInteractive: false, }))) { // try again, but run in parallel return upsertAndExecuteExperiment(experiment, true); } throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to save and run the experiment. HTTP request failed.'); } }); } function getExperimentExecutionUsingUrl(url) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield (0, http_1.executeApiCall)({ method: 'GET', path: url, fullyQualifiedUrl: true, }); return (yield response.json()); } catch (e) { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to get experiment run '); } }); } function fetchExperiment(key) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const response = yield (0, http_1.executeApiCall)({ method: 'GET', path: `/api/experiments/${encodeURIComponent(key)}`, }); const experiment = (yield response.json()); delete experiment.version; // We remove the version (as this makes things complicated to use). Will be removed from API in the future. return experiment; } catch (e) { if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) { throw (0, errors_1.abortExecution)('Experiment %s not found.', key); } else { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to get the experiment. HTTP request failed.'); } } }); } function updateExperiment(key, experiment) { return __awaiter(this, void 0, void 0, function* () { try { yield (0, http_1.executeApiCall)({ method: 'POST', path: `/api/experiments/${encodeURIComponent(key)}`, body: experiment, }); } catch (e) { if (e.response.status === 404) { throw (0, errors_1.abortExecution)('Experiment %s not found.', key); } else { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to save the experiment. HTTP request failed.'); } } }); } function removeExperiment(key) { return __awaiter(this, void 0, void 0, function* () { try { yield (0, http_1.executeApiCall)({ method: 'DELETE', path: `/api/experiments/${encodeURIComponent(key)}`, }); } catch (e) { if (e.response.status === 404) { throw (0, errors_1.abortExecution)('Experiment %s not found.', key); } else { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to delete the experiment. HTTP request failed.'); } } }); } function upsertExperiment(experiment) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield (0, http_1.executeApiCall)({ method: 'POST', path: '/api/experiments', body: experiment, }); const location = response.headers.get('Location'); const key = location === null || location === void 0 ? void 0 : location.substring(location.lastIndexOf('/') + 1); return { created: response.status === 201, key }; } catch (e) { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to save the experiment. HTTP request failed.'); } }); } function fetchExperiments(teamKey) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield (0, http_1.executeApiCall)({ method: 'GET', path: '/api/experiments', queryParameters: { team: teamKey, }, }); return (yield response.json()); } catch (e) { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to get the experiments. HTTP request failed.'); } }); } function fetchExecutionsForExperiment(key) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield (0, http_1.executeApiCall)({ method: 'GET', path: `/api/experiments/${encodeURIComponent(key)}/executions`, }); return (yield response.json()); } catch (e) { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to get the executions. HTTP request failed.'); } }); } function getExperimentExecution(id_1) { return __awaiter(this, arguments, void 0, function* (id, abortOnError = true) { try { const response = yield (0, http_1.executeApiCall)({ method: 'GET', path: `/api/experiments/executions/${id}`, }); return (yield response.json()); } catch (e) { if (abortOnError) { throw yield (0, errors_1.abortExecutionWithError)(e, 'Failed to get experiment run '); } else { throw e; } } }); } //# sourceMappingURL=api.js.map