index.js
function (options, callback) {
    if (!callback && !promisesExist) {
      throw new Error('Callback must be provided if promises are unavailable');
    }

    var endpoint = '';
    if (options.endpoint) {
      endpoint = '/' + options.endpoint;
    }

    var query;
    var self = this;

    if (typeof options.query !== 'undefined' && typeof options.query === 'object') {
      if (Object.keys(options.query).length === 0) {
        if (callback) {
          return callback(new Error('Options object should not be empty'));
        }
        return Promise.reject(new Error('Options object should not be empty'));
      }

      options.query.api_key = this.apiKey;
      query = queryStringify(options.query);
    } else {
      query = queryStringify({
        api_key: self.apiKey
      });
    }

    var httpOptions = {
      httpService: this.httpService,
      request: {
        host: API_HOSTNAME,
        path: API_BASE_PATH + options.api + endpoint + query
      },
      timeout: this.timeout,
      fmt: options.query && options.query.fmt,
      https: this.https
    };

    var makeRequest = function (resolve, reject) {
      httpService.get(httpOptions, resolve, reject);
    };

    if (callback) {
      var resolve = function (res) {
        callback(null, res);
      };
      var reject = function (err) {
        callback(err);
      };
      makeRequest(resolve, reject);
    } else {
      if (!promisesExist) {
        throw new Error('Callback must be provided unless Promises are available');
      }
      return new Promise(function (resolve, reject) {
        makeRequest(resolve, reject);
      });
    }
  }
function (options, callback) {
    if (!callback && !promisesExist) {
      throw new Error('Callback must be provided if promises are unavailable');
    }

    var endpoint = '';
    if (options.endpoint) {
      endpoint = '/' + options.endpoint;
    }

    var query;
    var self = this;

    if (typeof options.query !== 'undefined' && typeof options.query === 'object') {
      if (Object.keys(options.query).length === 0) {
        if (callback) {
          return callback(new Error('Options object should not be empty'));
        }
        return Promise.reject(new Error('Options object should not be empty'));
      }

      options.query.api_key = this.apiKey;
      query = queryStringify(options.query);
    } else {
      query = queryStringify({
        api_key: self.apiKey
      });
    }

    var httpOptions = {
      httpService: this.httpService,
      request: {
        host: API_HOSTNAME,
        path: API_BASE_PATH + options.api + endpoint + query
      },
      timeout: this.timeout,
      fmt: options.query && options.query.fmt,
      https: this.https
    };

    var makeRequest = function (resolve, reject) {
      httpService.get(httpOptions, resolve, reject);
    };

    if (callback) {
      var resolve = function (res) {
        callback(null, res);
      };
      var reject = function (err) {
        callback(err);
      };
      makeRequest(resolve, reject);
    } else {
      if (!promisesExist) {
        throw new Error('Callback must be provided unless Promises are available');
      }
      return new Promise(function (resolve, reject) {
        makeRequest(resolve, reject);
      });
    }
  }
function ( options , callback ) { if ( ! callback && ! promisesExist ) { throw new Error ( 'Callback must be provided if promises are unavailable' ) ; } var endpoint = '' ; if ( options . endpoint ) { endpoint = '/' + options . endpoint ; } var query ; var self = this ; if ( typeof options . query !== 'undefined' && typeof options . query === 'object' ) { if ( Object . keys ( options . query ) . length === 0 ) { if ( callback ) { return callback ( new Error ( 'Options object should not be empty' ) ) ; } return Promise . reject ( new Error ( 'Options object should not be empty' ) ) ; } options . query . api_key = this . apiKey ; query = queryStringify ( options . query ) ; } else { query = queryStringify ( { api_key : self . apiKey } ) ; } var httpOptions = { httpService : this . httpService , request : { host : API_HOSTNAME , path : API_BASE_PATH + options . api + endpoint + query } , timeout : this . timeout , fmt : options . query && options . query . fmt , https : this . https } ; var makeRequest = function ( resolve , reject ) { httpService . get ( httpOptions , resolve , reject ) ; } ; if ( callback ) { var resolve = function ( res ) { callback ( null , res ) ; } ; var reject = function ( err ) { callback ( err ) ; } ; makeRequest ( resolve , reject ) ; } else { if ( ! promisesExist ) { throw new Error ( 'Callback must be provided unless Promises are available' ) ; } return new Promise ( function ( resolve , reject ) { makeRequest ( resolve , reject ) ; } ) ; } }
Prepares the HTTP request and query string for the Giphy API

@param options
options.endpoint {String} - The API endpoint e.g. search
options.query {String|Object} Query string parameters. If these are left
out then we default to an empty string. If this is passed as a string,
we default to the 'q' query string field used by Giphy.
Prepares the HTTP request and query string for the Giphy API