136 lines
4.7 KiB
JavaScript
136 lines
4.7 KiB
JavaScript
(function() {
|
|
var API_BASE = '/api/admin';
|
|
|
|
function headers(extra) {
|
|
return Object.assign({
|
|
'Accept': 'application/json',
|
|
}, extra || {});
|
|
}
|
|
|
|
async function request(path, options) {
|
|
var response = await fetch(API_BASE + path, Object.assign({
|
|
credentials: 'same-origin',
|
|
headers: headers(),
|
|
}, options || {}));
|
|
|
|
if (response.status === 204) {
|
|
return null;
|
|
}
|
|
|
|
var payload = null;
|
|
var text = '';
|
|
|
|
try {
|
|
payload = await response.json();
|
|
} catch (_error) {
|
|
text = await response.text();
|
|
}
|
|
|
|
if (!response.ok) {
|
|
var message = payload && payload.error
|
|
? payload.error.message
|
|
: payload && payload.message
|
|
? payload.message
|
|
: text
|
|
? text
|
|
: ('HTTP ' + response.status);
|
|
var error = new Error(message);
|
|
error.status = response.status;
|
|
error.payload = payload;
|
|
throw error;
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
function get(path) {
|
|
return request(path);
|
|
}
|
|
|
|
function post(path, body) {
|
|
return request(path, {
|
|
method: 'POST',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function patch(path, body) {
|
|
return request(path, {
|
|
method: 'PATCH',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function del(path) {
|
|
return request(path, { method: 'DELETE' });
|
|
}
|
|
|
|
function postBytes(path, bytes, fileName) {
|
|
return request(path, {
|
|
method: 'POST',
|
|
headers: headers(fileName ? { 'X-File-Name': fileName } : {}),
|
|
body: bytes,
|
|
});
|
|
}
|
|
|
|
window.CrankApi = {
|
|
listWorkspaces: function() {
|
|
return get('/workspaces');
|
|
},
|
|
listOperations: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/operations');
|
|
},
|
|
getOperation: function(workspaceId, operationId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId));
|
|
},
|
|
getOperationVersion: function(workspaceId, operationId, version) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId) + '/versions/' + encodeURIComponent(version));
|
|
},
|
|
createOperation: function(workspaceId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/operations', payload);
|
|
},
|
|
updateOperation: function(workspaceId, operationId, payload) {
|
|
return patch('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId), payload);
|
|
},
|
|
deleteOperation: function(workspaceId, operationId) {
|
|
return del('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId));
|
|
},
|
|
archiveOperation: function(workspaceId, operationId) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId) + '/archive', {});
|
|
},
|
|
createOperationVersion: function(workspaceId, operationId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId) + '/versions', payload);
|
|
},
|
|
uploadProtoFile: function(workspaceId, operationId, content, fileName) {
|
|
return postBytes(
|
|
'/workspaces/' + encodeURIComponent(workspaceId) + '/operations/' + encodeURIComponent(operationId) + '/descriptors/proto',
|
|
content,
|
|
fileName
|
|
);
|
|
},
|
|
listAgents: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/agents');
|
|
},
|
|
createAgent: function(workspaceId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/agents', payload);
|
|
},
|
|
getAgent: function(workspaceId, agentId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/agents/' + encodeURIComponent(agentId));
|
|
},
|
|
updateAgent: function(workspaceId, agentId, payload) {
|
|
return patch('/workspaces/' + encodeURIComponent(workspaceId) + '/agents/' + encodeURIComponent(agentId), payload);
|
|
},
|
|
deleteAgent: function(workspaceId, agentId) {
|
|
return del('/workspaces/' + encodeURIComponent(workspaceId) + '/agents/' + encodeURIComponent(agentId));
|
|
},
|
|
saveAgentBindings: function(workspaceId, agentId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/agents/' + encodeURIComponent(agentId) + '/bindings', payload);
|
|
},
|
|
publishAgent: function(workspaceId, agentId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/agents/' + encodeURIComponent(agentId) + '/publish', payload);
|
|
},
|
|
};
|
|
}());
|