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