UNPKG

streamer-client

Version:

A node.js client for the Duke OIT Streamer API

79 lines (72 loc) 2.05 kB
var Promise = require('bluebird'); var request = require('request'); var qs = require('qs'); /** * Creates a dispatcher which will augment the parameters passed to request with * the authKey: authValue mutation. * @class * @classdesc A HTTP wrapper for the Steamer API * @param {String} authKey The key for request authentication * @param {(String|Object)} authValue The value for request authentication */ function Dispatcher(authKey, authValue) { /** * The key for the request options hash * @type {String} */ this.authKey = authKey; /** * The value for the request options hash * @type {(String|Object)} */ this.authValue = authValue; } /** * The root Url for the current version of the Streamer API. * @type {String} */ Dispatcher.ROOT_URL = 'https://streamer.oit.duke.edu'; /** * Creates a Streamer API Url by concatenating the ROOT_URL with path provided. * @param {String} path The path * @return {String} The url */ Dispatcher.prototype.url = function(path) { return Dispatcher.ROOT_URL + path; }; /** * Dispatches a request to the Streamer API. The request parameters are passed to * the request module. * @param {Object} params The params for request * @return {Promise} The response for the request */ Dispatcher.prototype.dispatch = function(params) { params.qs[this.authKey] = this.authValue; return new Promise(function(resolve, reject) { request(params, function(err, res, payload) { if (err) { return reject(err); } return resolve(payload); }); }); }; /** * Dispatches a GET request to the Streamer API. * @param {String} path The path of the API * @param {Object} [query] The query params * @return {Promise} The response for the request */ Dispatcher.prototype.get = function(path, query) { var params = { method: 'GET', url: this.url(path), qs: {}, json: true }; if (query) { params.qs = query; } return this.dispatch(params); }; module.exports = Dispatcher;