Source: api-request-manager.ts

  1. /**
  2. * @fileoverview A library for making requests to the Box API.
  3. */
  4. // ------------------------------------------------------------------------------
  5. // Requirements
  6. // ------------------------------------------------------------------------------
  7. import { Promise } from 'bluebird';
  8. import { EventEmitter } from 'events';
  9. import errors from './util/errors';
  10. import { PassThrough } from 'stream';
  11. const APIRequest = require('./api-request');
  12. // -----------------------------------------------------------------------------
  13. // Typedefs
  14. // -----------------------------------------------------------------------------
  15. type Config = any /* FIXME */;
  16. // ------------------------------------------------------------------------------
  17. // Private
  18. // ------------------------------------------------------------------------------
  19. // ------------------------------------------------------------------------------
  20. // Public
  21. // ------------------------------------------------------------------------------
  22. /**
  23. * A library for communicating with the Box API.
  24. *
  25. * @param {Config} config SDK configuration object instance.
  26. * @param {EventEmitter} eventBus The event bus for SDK events
  27. * @constructor
  28. */
  29. class APIRequestManager {
  30. config: Config;
  31. eventBus: EventEmitter;
  32. constructor(config: Config, eventBus: EventEmitter) {
  33. this.config = config;
  34. this.eventBus = eventBus;
  35. }
  36. /**
  37. * Make a request to the API, and get the response via callback.
  38. *
  39. * @param {Object} options The request options
  40. * @returns {Promise<Response>} A promise resolving to the response object
  41. */
  42. makeRequest(options: any /* FIXME */) {
  43. // Add default APIRequestManager options to each request
  44. var requestConfig = this.config.extend({
  45. request: options,
  46. });
  47. // Make the request
  48. var apiRequest = new APIRequest(requestConfig, this.eventBus);
  49. return Promise.fromCallback((callback) =>
  50. apiRequest.execute(callback)
  51. ).catch((err) => errors.unwrapAndThrow(err));
  52. }
  53. /**
  54. * Make a request to the API, and return a read stream for the response.
  55. *
  56. * @param {Object} options The request options
  57. * @returns {Stream.Readable} The response stream
  58. */
  59. makeStreamingRequest(options: any /* FIXME */) {
  60. // Add default APIRequestManager options to each request
  61. var requestConfig = this.config.extend({
  62. request: options,
  63. });
  64. // Make the request
  65. var apiRequest = new APIRequest(requestConfig, this.eventBus);
  66. apiRequest.execute();
  67. var stream = apiRequest.getResponseStream();
  68. // The request is asynchronous, so we need to wait for the stream to be
  69. // available before we can pipe it to the pass-through stream.
  70. // If the stream is undefined, then the request failed and we should
  71. // propagate the error.
  72. if (
  73. stream &&
  74. requestConfig.disableStreamPassThrough !== true &&
  75. options.disableStreamPassThrough !== true
  76. ) {
  77. var passThrough = new PassThrough();
  78. stream.pipe(passThrough);
  79. return passThrough;
  80. }
  81. return stream;
  82. }
  83. }
  84. /**
  85. * @module box-node-sdk/lib/api-request-manager
  86. * @see {@Link APIRequestManager}
  87. */
  88. export = APIRequestManager;