box-node-sdk
Version:
Official SDK for Box Plaform APIs
78 lines • 3.18 kB
JavaScript
;
/**
* @fileoverview A library for making requests to the Box API.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const bluebird_1 = require("bluebird");
const errors_1 = __importDefault(require("./util/errors"));
const stream_1 = require("stream");
const APIRequest = require('./api-request');
// ------------------------------------------------------------------------------
// Private
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Public
// ------------------------------------------------------------------------------
/**
* A library for communicating with the Box API.
*
* @param {Config} config SDK configuration object instance.
* @param {EventEmitter} eventBus The event bus for SDK events
* @constructor
*/
class APIRequestManager {
constructor(config, eventBus) {
this.config = config;
this.eventBus = eventBus;
}
/**
* Make a request to the API, and get the response via callback.
*
* @param {Object} options The request options
* @returns {Promise<Response>} A promise resolving to the response object
*/
makeRequest(options /* FIXME */) {
// Add default APIRequestManager options to each request
var requestConfig = this.config.extend({
request: options,
});
// Make the request
var apiRequest = new APIRequest(requestConfig, this.eventBus);
return bluebird_1.Promise.fromCallback((callback) => apiRequest.execute(callback)).catch((err) => errors_1.default.unwrapAndThrow(err));
}
/**
* Make a request to the API, and return a read stream for the response.
*
* @param {Object} options The request options
* @returns {Stream.Readable} The response stream
*/
makeStreamingRequest(options /* FIXME */) {
// Add default APIRequestManager options to each request
var requestConfig = this.config.extend({
request: options,
});
// Make the request
var apiRequest = new APIRequest(requestConfig, this.eventBus);
apiRequest.execute();
var stream = apiRequest.getResponseStream();
// The request is asynchronous, so we need to wait for the stream to be
// available before we can pipe it to the pass-through stream.
// If the stream is undefined, then the request failed and we should
// propagate the error.
if (stream &&
requestConfig.disableStreamPassThrough !== true &&
options.disableStreamPassThrough !== true) {
var passThrough = new stream_1.PassThrough();
stream.pipe(passThrough);
return passThrough;
}
return stream;
}
}
module.exports = APIRequestManager;
//# sourceMappingURL=api-request-manager.js.map