223 lines
8.3 KiB
JavaScript
223 lines
8.3 KiB
JavaScript
(function() {
|
|
var API_BASE = '/api/admin';
|
|
var AUTH_BASE = '/api/auth';
|
|
|
|
function headers(extra) {
|
|
return Object.assign({
|
|
'Accept': 'application/json',
|
|
}, extra || {});
|
|
}
|
|
|
|
async function request(path, options) {
|
|
var response = await fetch(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) {
|
|
if (response.status === 401 && window.CrankAuth && typeof window.CrankAuth.handleUnauthorized === 'function') {
|
|
window.CrankAuth.handleUnauthorized();
|
|
}
|
|
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(API_BASE + path);
|
|
}
|
|
|
|
function post(path, body) {
|
|
return request(API_BASE + path, {
|
|
method: 'POST',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function patch(path, body) {
|
|
return request(API_BASE + path, {
|
|
method: 'PATCH',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function del(path) {
|
|
return request(API_BASE + path, { method: 'DELETE' });
|
|
}
|
|
|
|
function query(params) {
|
|
var search = new URLSearchParams();
|
|
Object.keys(params || {}).forEach(function(key) {
|
|
var value = params[key];
|
|
if (value === undefined || value === null || value === '') {
|
|
return;
|
|
}
|
|
search.set(key, value);
|
|
});
|
|
var encoded = search.toString();
|
|
return encoded ? ('?' + encoded) : '';
|
|
}
|
|
|
|
function postBytes(path, bytes, fileName) {
|
|
return request(API_BASE + path, {
|
|
method: 'POST',
|
|
headers: headers(fileName ? { 'X-File-Name': fileName } : {}),
|
|
body: bytes,
|
|
});
|
|
}
|
|
|
|
window.CrankApi = {
|
|
login: function(payload) {
|
|
return request(AUTH_BASE + '/login', {
|
|
method: 'POST',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify(payload),
|
|
});
|
|
},
|
|
logout: function() {
|
|
return request(AUTH_BASE + '/logout', {
|
|
method: 'POST',
|
|
headers: headers({ 'Content-Type': 'application/json' }),
|
|
body: JSON.stringify({}),
|
|
});
|
|
},
|
|
getSession: function() {
|
|
return request(AUTH_BASE + '/session');
|
|
},
|
|
listWorkspaces: function() {
|
|
return get('/workspaces');
|
|
},
|
|
createWorkspace: function(payload) {
|
|
return post('/workspaces', payload);
|
|
},
|
|
getWorkspace: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId));
|
|
},
|
|
updateWorkspace: function(workspaceId, payload) {
|
|
return patch('/workspaces/' + encodeURIComponent(workspaceId), payload);
|
|
},
|
|
listMemberships: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/members');
|
|
},
|
|
listInvitations: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/invitations');
|
|
},
|
|
createInvitation: function(workspaceId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/invitations', payload);
|
|
},
|
|
deleteInvitation: function(workspaceId, invitationId) {
|
|
return del('/workspaces/' + encodeURIComponent(workspaceId) + '/invitations/' + encodeURIComponent(invitationId));
|
|
},
|
|
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);
|
|
},
|
|
listPlatformApiKeys: function(workspaceId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/platform-api-keys');
|
|
},
|
|
createPlatformApiKey: function(workspaceId, payload) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/platform-api-keys', payload);
|
|
},
|
|
revokePlatformApiKey: function(workspaceId, keyId) {
|
|
return post('/workspaces/' + encodeURIComponent(workspaceId) + '/platform-api-keys/' + encodeURIComponent(keyId) + '/revoke', {});
|
|
},
|
|
deletePlatformApiKey: function(workspaceId, keyId) {
|
|
return del('/workspaces/' + encodeURIComponent(workspaceId) + '/platform-api-keys/' + encodeURIComponent(keyId));
|
|
},
|
|
listLogs: function(workspaceId, params) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/logs' + query(params));
|
|
},
|
|
getLog: function(workspaceId, logId) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/logs/' + encodeURIComponent(logId));
|
|
},
|
|
getUsageOverview: function(workspaceId, params) {
|
|
return get('/workspaces/' + encodeURIComponent(workspaceId) + '/usage' + query(params));
|
|
},
|
|
getOperationUsage: function(workspaceId, operationId, params) {
|
|
return get(
|
|
'/workspaces/' + encodeURIComponent(workspaceId) + '/usage/operations/' + encodeURIComponent(operationId) + query(params)
|
|
);
|
|
},
|
|
getAgentUsage: function(workspaceId, agentId, params) {
|
|
return get(
|
|
'/workspaces/' + encodeURIComponent(workspaceId) + '/usage/agents/' + encodeURIComponent(agentId) + query(params)
|
|
);
|
|
},
|
|
};
|
|
|
|
}());
|