add initial support for REST

This commit is contained in:
Dave Horton
2021-02-18 15:57:46 -05:00
parent d0972145f0
commit 356ae693cc
6 changed files with 113 additions and 6 deletions
+1
View File
@@ -21,6 +21,7 @@ class WebhookResponse {
addVerb(verb, payload) {
validate(verb, payload);
this.payload.push({verb, ...payload});
return this;
}
}
+30
View File
@@ -0,0 +1,30 @@
const assert = require('assert');
class Calls {
constructor(accountSid, apiKey, opts) {
this.accountSid = accountSid;
this.apiKey = apiKey;
['post', 'put', 'get', 'del'].forEach((m) => this[m] = opts[m]);
}
async update(callSid, opts) {
const {call_hook, call_status, listen_status, mute_status, whisper} = opts;
assert.ok(call_hook || call_status || listen_status || mute_status || whisper,
`calls.update: invalid request ${JSON.stringify(opts)}`);
if (call_status) assert.ok(['completed', 'no-answer'].includes(call_status),
`invalid call_status: ${call_status}, must be 'completed' or 'no-answer'`);
if (mute_status) assert.ok(['mute', 'unmute'].includes(mute_status),
`invalid mute_status: ${mute_status}, must be 'mute' or 'unmute'`);
if (whisper) assert.ok(whisper.verb,
`invalid whisper: ${JSON.stringify(whisper)}, must be a 'play' or 'say' verb`);
await this.post(`Accounts/${this.accountSid}/Calls/${callSid}`, opts);
}
}
module.exports = Calls;
+18 -3
View File
@@ -1,13 +1,28 @@
const assert = require('assert');
const bent = require('bent');
const Calls = require('./calls');
class Jambonz {
constructor(accountSid, apiKey, opts) {
assert.ok(typeof accountSid === 'string', 'accountSid required');
assert.ok(typeof apiKey === 'string', 'apiKey required');
opts = opts || {};
this.endpoint = opts.endpoint;
assert.ok(opts.baseUrl, 'opts.baseUrl required when instantiating jambon REST client');
// TODO: test credentials, throw exception on failure
let baseUrl = opts.baseUrl;
if (opts.baseUrl.endsWith('/v1')) baseUrl = `${opts.baseUrl}/`;
else if (opts.baseUrl.endsWith('/v1/')) {}
else if (opts.baseUrl.endsWith('/')) baseUrl = `${opts.baseUrl}v1/`;
else baseUrl = `${opts.baseUrl}/v1/`;
const headers = {'Authorization': `Bearer ${apiKey}`};
const post = bent(baseUrl, 'POST', 'buffer', headers, 200, 201, 202);
const put = bent(baseUrl, 'POST', 'buffer', headers, 204);
const get = bent(baseUrl, 'GET', 'buffer', headers, 200);
const del = bent(baseUrl, 'DELETE', 'buffer', headers, 204);
this.calls = new Calls(accountSid, apiKey, {baseUrl, post, put, get, del});
}
}