CloudSearchDomainClient.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Aws\CloudSearchDomain;
  3. use Aws\AwsClient;
  4. use Aws\CommandInterface;
  5. use GuzzleHttp\Psr7\Uri;
  6. use Psr\Http\Message\RequestInterface;
  7. use GuzzleHttp\Psr7;
  8. /**
  9. * This client is used to search and upload documents to an **Amazon CloudSearch** Domain.
  10. *
  11. * @method \Aws\Result search(array $args = [])
  12. * @method \GuzzleHttp\Promise\Promise searchAsync(array $args = [])
  13. * @method \Aws\Result suggest(array $args = [])
  14. * @method \GuzzleHttp\Promise\Promise suggestAsync(array $args = [])
  15. * @method \Aws\Result uploadDocuments(array $args = [])
  16. * @method \GuzzleHttp\Promise\Promise uploadDocumentsAsync(array $args = [])
  17. */
  18. class CloudSearchDomainClient extends AwsClient
  19. {
  20. public function __construct(array $args)
  21. {
  22. parent::__construct($args);
  23. $list = $this->getHandlerList();
  24. $list->appendBuild($this->searchByPost(), 'cloudsearchdomain.search_by_POST');
  25. }
  26. public static function getArguments()
  27. {
  28. $args = parent::getArguments();
  29. $args['endpoint']['required'] = true;
  30. $args['region']['default'] = function (array $args) {
  31. // Determine the region from the provided endpoint.
  32. // (e.g. http://search-blah.{region}.cloudsearch.amazonaws.com)
  33. return explode('.', new Uri($args['endpoint']))[1];
  34. };
  35. return $args;
  36. }
  37. /**
  38. * Use POST for search command
  39. *
  40. * Useful when query string is too long
  41. */
  42. private function searchByPost()
  43. {
  44. return static function (callable $handler) {
  45. return function (
  46. CommandInterface $c,
  47. RequestInterface $r = null
  48. ) use ($handler) {
  49. if ($c->getName() !== 'Search') {
  50. return $handler($c, $r);
  51. }
  52. return $handler($c, self::convertGetToPost($r));
  53. };
  54. };
  55. }
  56. /**
  57. * Converts default GET request to a POST request
  58. *
  59. * Avoiding length restriction in query
  60. *
  61. * @param RequestInterface $r GET request to be converted
  62. * @return RequestInterface $req converted POST request
  63. */
  64. public static function convertGetToPost(RequestInterface $r)
  65. {
  66. if ($r->getMethod() === 'POST') {
  67. return $r;
  68. }
  69. $query = $r->getUri()->getQuery();
  70. $req = $r->withMethod('POST')
  71. ->withBody(Psr7\stream_for($query))
  72. ->withHeader('Content-Length', strlen($query))
  73. ->withHeader('Content-Type', 'application/x-www-form-urlencoded')
  74. ->withUri($r->getUri()->withQuery(''));
  75. return $req;
  76. }
  77. }