Skip to content

Instantly share code, notes, and snippets.

@akirilyuk
Last active November 23, 2021 10:32
Show Gist options
  • Select an option

  • Save akirilyuk/e6c40348712f1559764d08f4806ab828 to your computer and use it in GitHub Desktop.

Select an option

Save akirilyuk/e6c40348712f1559764d08f4806ab828 to your computer and use it in GitHub Desktop.
const credentials = {
username: 'testName',
password: 'testPassword',
};
const exampleProjectId = 'myTestProjectId';
const exampleConversationId = 'myTestConversationId';
const exampleConsumerId = 'myTestConsumerId';
let constructorCalled = 0;
let authenticateCalled = 0;
const storage = {};
// do not change this, this is given by a third party
class ThirdPartyClient {
constructor({ username, password }) {
this.username = username;
this.password = password;
this.authenticated = false;
constructorCalled += 1;
}
checkAuth(){
if(!this.authenticated){
throw new Error('NOT AUTHENTICATED');
}
}
async authenticate() {
this.authenticated = true;
authenticateCalled += 1;
return true;
}
async get(location) {
this.checkAuth();
return storage[location];
}
async set(location, data) {
this.checkAuth();
storage[location] = data;
return data;
}
async delete(location) {
this.checkAuth();
delete storage[location];
return {};
}
}
// this one is the one you need to work with
class LivepersonClientProxy {
async getProject({ username, password, projectId }) {
const client = new ThirdPartyClient({ username, password });
await client.authenticate();
let project = await client.get(
`my-example-namespace/application-namespace/${projectId}`
);
if (!project) {
project = await client.set(
`my-example-namespace/application-namespace/${projectId}`,
{ default: 'data', id: projectId }
);
}
return project;
}
async getConversation({ username, password, projectId, conversationId }) {
const client = new ThirdPartyClient({ username, password });
await client.authenticate();
let conversation = await client.get(
`my-example-namespace/application-namespace/${projectId}/${conversationId}`
);
if (!conversation) {
conversation = await client.set(
`my-example-namespace/application-namespace/${projectId}/${conversationId}`,
{ conversation: 'data', id: conversationId }
);
}
return conversation;
}
async getConsumer({
username,
password,
projectId,
conversationId,
consumerId,
}) {
const client = new ThirdPartyClient({ username, password });
await client.authenticate();
let consumer = await client.get(
`my-example-namespace/application-namespace/${projectId}/${conversationId}/${consumerId}`
);
if (!consumer) {
consumer = await client.set(
`my-example-namespace/application-namespace/${projectId}/${conversationId}/${consumerId}`,
{ consumer: 'data', id: consumerId }
);
}
return consumer;
}
}
async function main() {
const proxy = new LivepersonClientProxy();
const project = await proxy.getProject({
...credentials,
projectId: exampleProjectId,
});
console.log(project);
const conversation = await proxy.getConversation({
...credentials,
projectId: project.id,
conversationId: exampleConversationId,
});
console.log(conversation);
const consumer = await proxy.getConsumer({
...credentials,
projectId: project.id,
conversationId: conversation.id,
consumerId: exampleConsumerId,
});
console.log(consumer);
console.log(constructorCalled === 1);
console.log(authenticateCalled === 1);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment