cloudsearchdomain.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var AWS = require('../core');
  2. /**
  3. * Constructs a service interface object. Each API operation is exposed as a
  4. * function on service.
  5. *
  6. * ### Sending a Request Using CloudSearchDomain
  7. *
  8. * ```javascript
  9. * var csd = new AWS.CloudSearchDomain({endpoint: 'my.host.tld'});
  10. * csd.search(params, function (err, data) {
  11. * if (err) console.log(err, err.stack); // an error occurred
  12. * else console.log(data); // successful response
  13. * });
  14. * ```
  15. *
  16. * ### Locking the API Version
  17. *
  18. * In order to ensure that the CloudSearchDomain object uses this specific API,
  19. * you can construct the object by passing the `apiVersion` option to the
  20. * constructor:
  21. *
  22. * ```javascript
  23. * var csd = new AWS.CloudSearchDomain({
  24. * endpoint: 'my.host.tld',
  25. * apiVersion: '2013-01-01'
  26. * });
  27. * ```
  28. *
  29. * You can also set the API version globally in `AWS.config.apiVersions` using
  30. * the **cloudsearchdomain** service identifier:
  31. *
  32. * ```javascript
  33. * AWS.config.apiVersions = {
  34. * cloudsearchdomain: '2013-01-01',
  35. * // other service API versions
  36. * };
  37. *
  38. * var csd = new AWS.CloudSearchDomain({endpoint: 'my.host.tld'});
  39. * ```
  40. *
  41. * @note You *must* provide an `endpoint` configuration parameter when
  42. * constructing this service. See {constructor} for more information.
  43. *
  44. * @!method constructor(options = {})
  45. * Constructs a service object. This object has one method for each
  46. * API operation.
  47. *
  48. * @example Constructing a CloudSearchDomain object
  49. * var csd = new AWS.CloudSearchDomain({endpoint: 'my.host.tld'});
  50. * @note You *must* provide an `endpoint` when constructing this service.
  51. * @option (see AWS.Config.constructor)
  52. *
  53. * @service cloudsearchdomain
  54. * @version 2013-01-01
  55. */
  56. AWS.util.update(AWS.CloudSearchDomain.prototype, {
  57. /**
  58. * @api private
  59. */
  60. validateService: function validateService() {
  61. if (!this.config.endpoint || this.config.endpoint.indexOf('{') >= 0) {
  62. var msg = 'AWS.CloudSearchDomain requires an explicit ' +
  63. '`endpoint\' configuration option.';
  64. throw AWS.util.error(new Error(),
  65. {name: 'InvalidEndpoint', message: msg});
  66. }
  67. },
  68. /**
  69. * @api private
  70. */
  71. setupRequestListeners: function setupRequestListeners(request) {
  72. request.removeListener('validate',
  73. AWS.EventListeners.Core.VALIDATE_CREDENTIALS
  74. );
  75. request.onAsync('validate', this.validateCredentials);
  76. request.addListener('validate', this.updateRegion);
  77. if (request.operation === 'search') {
  78. request.addListener('build', this.convertGetToPost);
  79. }
  80. },
  81. /**
  82. * @api private
  83. */
  84. validateCredentials: function(req, done) {
  85. if (!req.service.api.signatureVersion) return done(); // none
  86. req.service.config.getCredentials(function(err) {
  87. if (err) {
  88. req.removeListener('sign', AWS.EventListeners.Core.SIGN);
  89. }
  90. done();
  91. });
  92. },
  93. /**
  94. * @api private
  95. */
  96. convertGetToPost: function(request) {
  97. var httpRequest = request.httpRequest;
  98. if (httpRequest.method === 'POST') {
  99. return;
  100. }
  101. // convert queries to POST to avoid length restrictions
  102. var path = httpRequest.path.split('?');
  103. httpRequest.method = 'POST';
  104. httpRequest.path = path[0];
  105. httpRequest.body = path[1];
  106. httpRequest.headers['Content-Length'] = httpRequest.body.length;
  107. httpRequest.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  108. },
  109. /**
  110. * @api private
  111. */
  112. updateRegion: function updateRegion(request) {
  113. var endpoint = request.httpRequest.endpoint.hostname;
  114. var zones = endpoint.split('.');
  115. request.httpRequest.region = zones[1] || request.httpRequest.region;
  116. }
  117. });