iotdata.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var AWS = require('../core');
  2. /**
  3. * @api private
  4. */
  5. var blobPayloadOutputOps = [
  6. 'deleteThingShadow',
  7. 'getThingShadow',
  8. 'updateThingShadow'
  9. ];
  10. /**
  11. * Constructs a service interface object. Each API operation is exposed as a
  12. * function on service.
  13. *
  14. * ### Sending a Request Using IotData
  15. *
  16. * ```javascript
  17. * var iotdata = new AWS.IotData({endpoint: 'my.host.tld'});
  18. * iotdata.getThingShadow(params, function (err, data) {
  19. * if (err) console.log(err, err.stack); // an error occurred
  20. * else console.log(data); // successful response
  21. * });
  22. * ```
  23. *
  24. * ### Locking the API Version
  25. *
  26. * In order to ensure that the IotData object uses this specific API,
  27. * you can construct the object by passing the `apiVersion` option to the
  28. * constructor:
  29. *
  30. * ```javascript
  31. * var iotdata = new AWS.IotData({
  32. * endpoint: 'my.host.tld',
  33. * apiVersion: '2015-05-28'
  34. * });
  35. * ```
  36. *
  37. * You can also set the API version globally in `AWS.config.apiVersions` using
  38. * the **iotdata** service identifier:
  39. *
  40. * ```javascript
  41. * AWS.config.apiVersions = {
  42. * iotdata: '2015-05-28',
  43. * // other service API versions
  44. * };
  45. *
  46. * var iotdata = new AWS.IotData({endpoint: 'my.host.tld'});
  47. * ```
  48. *
  49. * @note You *must* provide an `endpoint` configuration parameter when
  50. * constructing this service. See {constructor} for more information.
  51. *
  52. * @!method constructor(options = {})
  53. * Constructs a service object. This object has one method for each
  54. * API operation.
  55. *
  56. * @example Constructing a IotData object
  57. * var iotdata = new AWS.IotData({endpoint: 'my.host.tld'});
  58. * @note You *must* provide an `endpoint` when constructing this service.
  59. * @option (see AWS.Config.constructor)
  60. *
  61. * @service iotdata
  62. * @version 2015-05-28
  63. */
  64. AWS.util.update(AWS.IotData.prototype, {
  65. /**
  66. * @api private
  67. */
  68. validateService: function validateService() {
  69. if (!this.config.endpoint || this.config.endpoint.indexOf('{') >= 0) {
  70. var msg = 'AWS.IotData requires an explicit ' +
  71. '`endpoint\' configuration option.';
  72. throw AWS.util.error(new Error(),
  73. {name: 'InvalidEndpoint', message: msg});
  74. }
  75. },
  76. /**
  77. * @api private
  78. */
  79. setupRequestListeners: function setupRequestListeners(request) {
  80. request.addListener('validateResponse', this.validateResponseBody);
  81. if (blobPayloadOutputOps.indexOf(request.operation) > -1) {
  82. request.addListener('extractData', AWS.util.convertPayloadToString);
  83. }
  84. },
  85. /**
  86. * @api private
  87. */
  88. validateResponseBody: function validateResponseBody(resp) {
  89. var body = resp.httpResponse.body.toString() || '{}';
  90. var bodyCheck = body.trim();
  91. if (!bodyCheck || bodyCheck.charAt(0) !== '{') {
  92. resp.httpResponse.body = '';
  93. }
  94. }
  95. });