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