public function testThrowsExceptionWhenAttemptingToMutateCredentialsOrRegion() { $client = CloudSearchDomainClient::factory(array('base_url' => 'example.com')); $this->setExpectedException('BadMethodCallException'); $client->setRegion('us-west-2'); $this->setExpectedException('BadMethodCallException'); $client->setCredentaisl(new Credentials('foo', 'bar')); }
/** * Create a CloudSearchDomainClient for a particular domain to do searching * and document uploads. * * @param string $domainName Name of the CloudSearch domain. * @param array $config Config options for the CloudSearchDomainClient * * @return CloudSearchDomainClient */ public function getDomainClient($domainName, array $config = []) { $config['endpoint'] = $this->describeDomains(['DomainNames' => [$domainName]])->getPath('DomainStatusList/0/SearchService/Endpoint'); if (!isset($config['scheme'])) { $config['scheme'] = 'https'; } // Create an absolute URI for the endpoint. $config['endpoint'] = $config['scheme'] . '://' . $config['endpoint']; return CloudSearchDomainClient::factory($config); }
public function testSignsRequests() { $mock = new MockPlugin(array(new Response(200), new Response(200))); $config = array(Opt::BASE_URL => 'foo.us-east-1.cloudsearch.amazonaws.com', Opt::VALIDATION => false, Opt::CREDENTIALS => new Credentials('foo', 'bar')); $getAuthorizationHeader = function (Event $event) use(&$auth) { $auth = (string) $event['request']->getHeader('Authorization'); }; $client1 = CloudSearchDomainClient::factory($config); $client1->addSubscriber($mock); $client1->getEventDispatcher()->addListener('request.before_send', $getAuthorizationHeader, -999); $client1->search(); $this->assertNotEmpty($auth); $config[Opt::CREDENTIALS] = false; $client2 = CloudSearchDomainClient::factory($config); $client2->addSubscriber($mock); $client2->getEventDispatcher()->addListener('request.before_send', $getAuthorizationHeader, -999); $client2->search(); $this->assertEmpty($auth); }
public function build() { // Resolve configuration $config = Collection::fromConfig($this->config, array_merge(self::$commonConfigDefaults, $this->configDefaults), $this->configRequirements); $endpoint = $config['endpoint'] ?: $config[Options::BASE_URL]; // Make sure endpoint is correctly set if (!$endpoint) { throw new InvalidArgumentException('You must provide the endpoint for the CloudSearch domain.'); } if (strpos($endpoint, 'http') !== 0) { $endpoint = $config[Options::SCHEME] . '://' . $endpoint; $config['endpoint'] = $endpoint; $config[Options::BASE_URL] = $endpoint; } // Determine the region from the endpoint $endpoint = Url::factory($endpoint); list(, $region) = explode('.', $endpoint->getHost()); $config[Options::REGION] = $config[Options::SIGNATURE_REGION] = $region; // Create dependencies $exceptionParser = new JsonQueryExceptionParser(); $description = ServiceDescription::factory(sprintf($config->get(Options::SERVICE_DESCRIPTION), $config->get(Options::VERSION))); $signature = $this->getSignature($description, $config); $credentials = $this->getCredentials($config); // Resolve backoff strategy $backoff = $config->get(Options::BACKOFF); if ($backoff === null) { $backoff = new BackoffPlugin(new TruncatedBackoffStrategy(3, new ThrottlingErrorChecker($exceptionParser, new CurlBackoffStrategy(null, new HttpBackoffStrategy(array(500, 503, 509), new ExponentialBackoffStrategy()))))); $config->set(Options::BACKOFF, $backoff); } if ($backoff) { $this->addBackoffLogger($backoff, $config); } // Create client $client = new CloudSearchDomainClient($credentials, $signature, $config); $client->setDescription($description); // Add exception marshaling so that more descriptive exception are thrown $client->addSubscriber(new ExceptionListener(new NamespaceExceptionFactory($exceptionParser, __NAMESPACE__ . '\\Exception', __NAMESPACE__ . '\\Exception\\CloudSearchDomainException'))); // Add the UserAgentPlugin to append to the User-Agent header of requests $client->addSubscriber(new UserAgentListener()); // Filters used for the cache plugin $client->getConfig()->set('params.cache.key_filter', 'header=date,x-amz-date,x-amz-security-token,x-amzn-authorization'); // Disable parameter validation if needed if ($config->get(Options::VALIDATION) === false) { $params = $config->get('command.params') ?: array(); $params['command.disable_validation'] = true; $config->set('command.params', $params); } return $client; }
<?php require_once 'vendor/autoload.php'; use Aws\CloudSearchDomain\CloudSearchDomainClient; $csDomainClient = CloudSearchDomainClient::factory(['endpoint' => 'http://search-my-search-wzfnq73vja7om3qna37iaupkgm.us-east-1.cloudsearch.amazonaws.com', 'credentials' => false, 'version' => '2013-01-01']); $suggest = $csDomainClient->suggest(['query' => 'articl', 'suggester' => 'title_suggestions']); print_r($suggest['suggest']);
/** * Create a CloudSearchDomainClient for a particular domain to do searching * and document uploads. * * @param string $domainName Name of the domain for which to create a domain client. * @param array $config Config options for the CloudSearchDomainClient * * @return CloudSearchDomainClient * @link http://docs.aws.amazon.com/aws-sdk-php/guide/latest/configuration.html#client-configuration-options */ public function getDomainClient($domainName, array $config = array()) { // Determine the Domain client's endpoint $config['endpoint'] = $this->describeDomains(array('DomainNames' => array($domainName)))->getPath('DomainStatusList/0/SearchService/Endpoint'); return CloudSearchDomainClient::factory($config); }
public function connect() { $this->_connection = CloudSearchDomainClient::factory($this->config); $this->connected = true; }
<?php require_once 'vendor/autoload.php'; use Aws\CloudSearchDomain\CloudSearchDomainClient; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); $csDomainClient = CloudSearchDomainClient::factory(['endpoint' => 'http://doc-my-search-wzfnq73vja7om3qna37iaupkgm.us-east-1.cloudsearch.amazonaws.com', 'credentials' => ['key' => getenv('AWS_KEY'), 'secret' => getenv('AWS_SECRET')], 'version' => '2013-01-01']); $batch = [['type' => 'add', 'id' => '1', 'fields' => ['title' => 'Article 1', 'body' => 'This is the first example.', 'date' => '2015-01-22T10:03:06Z']], ['type' => 'add', 'id' => '2', 'fields' => ['title' => 'Article 2', 'body' => 'The second example is presented.', 'date' => '2015-01-23T10:03:06Z']], ['type' => 'add', 'id' => '3', 'fields' => ['title' => 'Article 3', 'body' => 'Third article.', 'date' => '2015-01-25T10:03:06Z']]]; $result = $csDomainClient->uploadDocuments(['documents' => json_encode($batch), 'contentType' => 'application/json']); print_r($result);
/** * Upload the documents. * * @param $documents */ private function uploadDocuments($documents) { $args = ['contentType' => 'application/json', 'documents' => json_encode($documents)]; $this->pushClient->uploadDocuments($args); }
protected function _factoryEndpointClient($type, $domain) { $var = sprintf('_%sClient', $type); $this->{$var} = false; $key = sprintf('%s_%s_%s_endpoint', ConnectionManager::getSourceName($this), $domain, $type); $key = preg_replace('/[^A-Za-z0-9_\\-.+]/', '_', $key); $endpoint = Cache::read($key, '_cake_model_'); if (empty($endpoint) && ($client = $this->_factoryClient())) { $result = $client->describeDomains(['DomainNames' => [$domain]]); $service = Inflector::camelize($type) . 'Service'; $endpoint = $result['DomainStatusList'][0][$service]['Endpoint']; Cache::write($key, $endpoint, '_cake_model_'); } if (!empty($endpoint)) { $this->{$var} = CloudSearchDomainClient::factory(['base_url' => $endpoint]); } return $this->{$var}; }