mirror of
https://github.com/donl/plivo-node.git
synced 2026-06-30 06:12:08 -06:00
test
This commit is contained in:
parent
c36e5ff06c
commit
e73d86af35
3 changed files with 185 additions and 37 deletions
109
lib/resources/media.js
Normal file
109
lib/resources/media.js
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import {
|
||||
extend,
|
||||
validate
|
||||
} from '../utils/common.js';
|
||||
import {
|
||||
PlivoResource,
|
||||
PlivoResourceInterface
|
||||
} from '../base';
|
||||
import * as _ from 'lodash';
|
||||
var fs = require('fs');
|
||||
|
||||
const clientKey = Symbol();
|
||||
const action = 'Media/';
|
||||
const idField = 'media_id';
|
||||
|
||||
/**
|
||||
* Represents a Message
|
||||
* @constructor
|
||||
* @param {function} client - make api call
|
||||
* @param {object} [data] - data of call
|
||||
*/
|
||||
export class Media extends PlivoResource {
|
||||
constructor(client, data = {}) {
|
||||
super(action, Media, idField, client);
|
||||
|
||||
if (idField in data) {
|
||||
this.id = data[idField];
|
||||
}
|
||||
|
||||
extend(this, data);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Represents a Media Interface
|
||||
* @constructor
|
||||
* @param {function} client - make api call
|
||||
* @param {object} [data] - data of call
|
||||
*/
|
||||
|
||||
export class MediaInterface extends PlivoResourceInterface {
|
||||
constructor(client, data = {}) {
|
||||
super(action, Media, idField, client);
|
||||
extend(this, data);
|
||||
this[clientKey] = client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload Media
|
||||
* @method
|
||||
* @fail {Error} return Error
|
||||
*/
|
||||
upload(files) {
|
||||
let errors = validate([{
|
||||
field: 'files',
|
||||
value: files,
|
||||
validators: ['isRequired']
|
||||
}]);
|
||||
|
||||
if (errors) {
|
||||
return errors;
|
||||
}
|
||||
params = {}
|
||||
for (index = 0; index < files.length; index++) {
|
||||
for (var key in files[index]) {
|
||||
params['file'] = {
|
||||
'value': fs.createReadStream(value),
|
||||
'options': {
|
||||
'filename': key,
|
||||
'contentType': null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return super.create(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Media by given id
|
||||
* @method
|
||||
* @param {string} media_id - id of media
|
||||
* @promise {object} return {@link Media} object if success
|
||||
* @fail {Error} return Error
|
||||
*/
|
||||
get(media_id) {
|
||||
let errors = validate([{
|
||||
field: 'media_id',
|
||||
value: media_id,
|
||||
validators: ['isRequired']
|
||||
}]);
|
||||
|
||||
if (errors) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
return super.get(media_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get All Media Detail
|
||||
* @method
|
||||
* @param {object} params - params to get all media details.
|
||||
* @promise {object[]} returns list of Media Object
|
||||
* @fail {Error} returns Error
|
||||
*/
|
||||
list(params) {
|
||||
return super.list(params);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { Request } from './request.js';
|
||||
import { camelCaseRequestWrapper } from './utils';
|
||||
import { name, version } from '../../package.json';
|
||||
import { Request } from "./request.js";
|
||||
import { camelCaseRequestWrapper } from "./utils";
|
||||
import { name, version } from "../../package.json";
|
||||
import { Phlo, PhloInterface } from "../resources/phlo";
|
||||
import { CallInterface } from "../resources/call";
|
||||
import { SubaccountInterface, AccountInterface } from "../resources/accounts";
|
||||
|
|
@ -15,14 +15,15 @@ import { RecordingInterface } from "../resources/recordings";
|
|||
import { Response } from "../utils/plivoxml";
|
||||
import { validateSignature } from "../utils/security";
|
||||
import { stringify } from "./../utils/jsonStrinfigier";
|
||||
import { MediaInterface } from "../resources/media.js";
|
||||
|
||||
exports.Response = function () {
|
||||
exports.Response = function() {
|
||||
return new Response();
|
||||
}
|
||||
};
|
||||
|
||||
exports.validateSignature = function (uri, nonce, signature, auth_token) {
|
||||
exports.validateSignature = function(uri, nonce, signature, auth_token) {
|
||||
return validateSignature(uri, nonce, signature, auth_token);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Plivo API client which can be used to access the Plivo APIs.
|
||||
|
|
@ -38,19 +39,25 @@ export class Client {
|
|||
authToken = authToken || process.env.PLIVO_AUTH_TOKEN;
|
||||
|
||||
if (authId == null) {
|
||||
throw (new Error('Please provide authId'));
|
||||
throw new Error("Please provide authId");
|
||||
}
|
||||
if (authToken == null) {
|
||||
throw (new Error('Please provide authToken'));
|
||||
throw new Error("Please provide authToken");
|
||||
}
|
||||
|
||||
options = Object.assign({}, {
|
||||
authId: authId,
|
||||
authToken: authToken,
|
||||
version: 'v1',
|
||||
url: 'https://api.plivo.com/v1/Account/' + authId,
|
||||
userAgent: `${'plivo-node'}/${version || 'Unknown Version'} (Node: ${process.version})`,
|
||||
}, options);
|
||||
options = Object.assign(
|
||||
{},
|
||||
{
|
||||
authId: authId,
|
||||
authToken: authToken,
|
||||
version: "v1",
|
||||
url: "https://api.plivo.com/v1/Account/" + authId,
|
||||
userAgent: `${"plivo-node"}/${version || "Unknown Version"} (Node: ${
|
||||
process.version
|
||||
})`
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
let client = camelCaseRequestWrapper(Request(options));
|
||||
|
||||
|
|
@ -65,6 +72,7 @@ export class Client {
|
|||
this.numbers = new NumberInterface(client);
|
||||
this.pricings = new PricingInterface(client);
|
||||
this.recordings = new RecordingInterface(client);
|
||||
this.media = new MediaInterface(client);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
|
@ -80,7 +88,6 @@ export class Client {
|
|||
*/
|
||||
export class PhloClient {
|
||||
constructor(authId, authToken, options) {
|
||||
|
||||
if (!(this instanceof PhloClient)) {
|
||||
return new PhloClient(authId, authToken, options);
|
||||
}
|
||||
|
|
@ -88,39 +95,45 @@ export class PhloClient {
|
|||
authToken = authToken || process.env.PLIVO_AUTH_TOKEN;
|
||||
|
||||
if (authId == null) {
|
||||
throw (new Error('Please provide authId'));
|
||||
throw new Error("Please provide authId");
|
||||
}
|
||||
if (authToken == null) {
|
||||
throw (new Error('Please provide authToken'));
|
||||
throw new Error("Please provide authToken");
|
||||
}
|
||||
|
||||
options = Object.assign({}, {
|
||||
authId: authId,
|
||||
authToken: authToken,
|
||||
version: 'v1',
|
||||
url: 'https://phlorunner.plivo.com/v1',
|
||||
userAgent: `${'plivo-node'}/${version || 'Unknown Version'} (Node: ${process.version})`,
|
||||
}, options);
|
||||
options = Object.assign(
|
||||
{},
|
||||
{
|
||||
authId: authId,
|
||||
authToken: authToken,
|
||||
version: "v1",
|
||||
url: "https://phlorunner.plivo.com/v1",
|
||||
userAgent: `${"plivo-node"}/${version || "Unknown Version"} (Node: ${
|
||||
process.version
|
||||
})`
|
||||
},
|
||||
options
|
||||
);
|
||||
|
||||
let client = camelCaseRequestWrapper(Request(options));
|
||||
|
||||
|
||||
this.phlo = function (phloId) {
|
||||
this.phlo = function(phloId) {
|
||||
let dd = new Phlo(client, { phloId: phloId, authId: authId });
|
||||
return dd;
|
||||
};
|
||||
|
||||
this.phlo.get = function (phloId) {
|
||||
this.phlo.get = function(phloId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let dd = new PhloInterface(client);
|
||||
dd.get(phloId).then(function (data) {
|
||||
data.authId = authId;
|
||||
resolve(data);
|
||||
}).catch(function (err) {
|
||||
reject(err);
|
||||
});
|
||||
dd.get(phloId)
|
||||
.then(function(data) {
|
||||
data.authId = authId;
|
||||
resolve(data);
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
test/media.js
Normal file
26
test/media.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import assert from 'assert';
|
||||
import sinon from 'sinon';
|
||||
import {
|
||||
Client
|
||||
} from '../lib/rest/client-test';
|
||||
import {
|
||||
PlivoGenericResponse
|
||||
} from '../lib/base.js';
|
||||
|
||||
let client = new Client('sampleid', 'sammpletoken', 'sampleproxy');
|
||||
|
||||
describe('MediaInterface', function () {
|
||||
|
||||
it('list media via interface', function () {
|
||||
return client.media.list()
|
||||
.then(function (media) {
|
||||
assert.notEqual(media.length, 0)
|
||||
})
|
||||
});
|
||||
it('should get media', function () {
|
||||
return client.media.get(1)
|
||||
.then(function (media) {
|
||||
assert.equal(media.media_id, 1)
|
||||
})
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue