steadybit
Version:
Command-line interface to interact with the Steadybit API
240 lines • 10.3 kB
JavaScript
;
// 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) {
var _a, _b;
try {
const response = yield (0, http_1.executeApiCall)({
method: 'POST',
path: `/api/experiments/${encodeURIComponent(key)}/execute?forcePersist=true&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 (!allowParallelExecutions &&
((_b = (yield (0, errors_1.getExecutionErrorBody)(e))) === null || _b === void 0 ? void 0 : _b.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) {
var _a, _b;
try {
const response = yield (0, http_1.executeApiCall)({
method: 'POST',
path: `/api/experiments/execute?forcePersist=true&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 (!allowParallelExecutions &&
((_b = (yield (0, errors_1.getExecutionErrorBody)(e))) === null || _b === void 0 ? void 0 : _b.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