cv-dialog-sdk
Version:
Catavolt Dialog Javascript API
181 lines (180 loc) • 10.3 kB
JavaScript
import { DefaultModelUtil } from '../models';
export class DialogService {
constructor(client, serverUrl, apiVersion) {
this.client = client;
this.apiVersion = apiVersion;
this.baseUrl = `${serverUrl}/${apiVersion}`;
}
// @TODO
addAttachment(tenantId, sessionId, dialogId, attachment) {
return Promise.resolve(null);
}
addClientListener(clientListener, locale) {
this.client.addClientListener(clientListener, locale);
}
removeClientListener(clientListener) {
this.client.removeClientListener(clientListener);
}
createSession(tenantId, login) {
return this.post(`tenants/${tenantId}/sessions`, login).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValueOrRedirect());
}
getSession(tenantId, sessionId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
deleteSession(tenantId, sessionId) {
return this.d3lete(`tenants/${tenantId}/sessions/${sessionId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getContent(tenantId, sessionId, contentId, readLargePropertyParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/content/${contentId}`, readLargePropertyParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getWorkbenches(tenantId, sessionId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/workbenches`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getWorkbench(tenantId, sessionId, workbenchId) {
return this.get(`tenants/{$tenantId}/sessions/{$sessionId}/workbenches/{$workbenchId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getRedirection(tenantId, sessionId, redirectionId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/redirections/${redirectionId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getDialog(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
deleteDialog(tenantId, sessionId, dialogId) {
return this.d3lete(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getActions(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/actions`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
performAction(tenantId, sessionId, dialogId, actionId, actionParameters) {
const encodedActionId = encodeURIComponent(actionId);
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/actions/${encodedActionId}`, actionParameters).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getWorkbenchActions(tenantId, sessionId, workbenchId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/workbenches/${workbenchId}/actions`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
performWorkbenchAction(tenantId, sessionId, workbenchId, actionId) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/workbenches/${workbenchId}/actions/${actionId}`, {}).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getRecord(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
putRecord(tenantId, sessionId, dialogId, record) {
return this.put(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record`, record).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValueOrRedirect());
}
getRecords(tenantId, sessionId, dialogId, queryParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/records`, queryParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getEditorProperty(tenantId, sessionId, dialogId, propertyName, readLargePropertyParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record/${propertyName}`, readLargePropertyParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getQueryProperty(tenantId, sessionId, dialogId, propertyName, readLargePropertyParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/records/${propertyName}`, readLargePropertyParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
writeProperty(tenantId, sessionId, dialogId, propertyName, writeLargePropertyParams) {
return this.put(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record/${propertyName}`, writeLargePropertyParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
propertyChange(tenantId, sessionId, dialogId, propertyName, sideEffectsParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record/${propertyName}/sideEffects`, sideEffectsParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getAvailableValues(tenantId, sessionId, dialogId, propertyName, availableValuesParams) {
return this.post(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/record/${propertyName}/availableValues`, availableValuesParams).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
openContentStream(tenantId, sessionId, contentId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/content/${contentId}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getMode(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/viewMode`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
changeMode(tenantId, sessionId, dialogId, mode) {
return this.put(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/viewMode/${mode}`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValueOrRedirect());
}
getView(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/view`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
changeView(tenantId, sessionId, dialogId, viewId) {
return this.put(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/selectedView/{viewId}`, {}).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
getViews(tenantId, sessionId, dialogId) {
return this.get(`tenants/${tenantId}/sessions/${sessionId}/dialogs/${dialogId}/availableViews`).then(jsonClientResponse => new DialogServiceResponse(jsonClientResponse).responseValue());
}
streamUrl(tentantId, sessionId, url) {
return this.stream(url);
}
isAnyUserInBriefcaseMode(tenantId) {
return this.client.isAnyUserInBriefcaseMode(tenantId);
}
isUserInBriefcaseMode(userInfo) {
return this.client.isUserInBriefcaseMode(userInfo);
}
get lastServiceActivity() {
return this.client.lastActivity;
}
/* Private methods */
get(path, queryParams) {
return this.client.getJson(`${this.baseUrl}`, path, queryParams);
}
post(path, body) {
return this.client.postJson(`${this.baseUrl}`, path, body);
}
d3lete(path) {
return this.client.deleteJson(`${this.baseUrl}`, path);
}
put(path, body) {
return this.client.putJson(`${this.baseUrl}`, path, body);
}
stream(url) {
return this.client.openStream(url);
}
}
class DialogServiceResponse {
constructor(clientResponse) {
this.clientResponse = clientResponse;
}
responseValue() {
return new Promise((resolve, reject) => {
if (this.hasError) {
reject(this.clientResponse.value);
}
else {
this.fullfillJsonToModel(this.clientResponse, resolve, reject);
}
});
}
responseValueOrRedirect() {
return new Promise((resolve, reject) => {
if (this.hasError) {
reject(this.clientResponse.value);
}
else if (this.hasValue) {
this.fullfillJsonToModel(this.clientResponse, resolve, reject);
}
else {
this.fullfillJsonToModel(this.clientResponse, resolve, reject);
}
});
}
assertNoError() {
return new Promise((resolve, reject) => {
if (this.hasError) {
reject(this.clientResponse.value);
}
else {
resolve(undefined);
}
});
}
get hasValue() {
return this.clientResponse.statusCode >= 200 && this.clientResponse.statusCode < 300;
}
get hasRedirection() {
return this.clientResponse.statusCode >= 300 && this.clientResponse.statusCode < 400;
}
get hasError() {
return this.clientResponse.statusCode >= 400;
}
fullfillJsonToModel(clientResponse, resolve, reject) {
DefaultModelUtil.jsonToModel(this.clientResponse.value)
.then(resolve)
.catch(reject);
}
}