From e73d86af35fb0523482ffed5df8940c222caf556 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Sun, 16 Feb 2020 23:45:12 +0530 Subject: [PATCH] test --- lib/resources/media.js | 109 +++++++++++++++++++++++++++++++++++++++++ lib/rest/client.js | 87 ++++++++++++++++++-------------- test/media.js | 26 ++++++++++ 3 files changed, 185 insertions(+), 37 deletions(-) create mode 100644 lib/resources/media.js create mode 100644 test/media.js diff --git a/lib/resources/media.js b/lib/resources/media.js new file mode 100644 index 0000000..69856d8 --- /dev/null +++ b/lib/resources/media.js @@ -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); + } +} diff --git a/lib/rest/client.js b/lib/rest/client.js index ba04f67..6d017e3 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -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); + }); }); - } - + }; } } diff --git a/test/media.js b/test/media.js new file mode 100644 index 0000000..bc33ad2 --- /dev/null +++ b/test/media.js @@ -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) + }) + }); +});