public function setUp()
 {
     $this->client = $this->getServiceBuilder()->get('s3');
     $this->client->getEventDispatcher()->removeSubscriber(BackoffPlugin::getExponentialBackoff());
     // disable retry
     $this->bucket = randBucket();
 }
Example #2
0
 public function testBuildAlternate()
 {
     $client = ClientBuilder::factory('Aws\\DynamoDb')->setConfigDefaults(array('scheme' => 'https', 'region' => 'us-west-1', 'service' => 'dynamodb', 'service.description' => $this->dynamoDbDescription))->setCredentialsResolver(new CredentialsOptionResolver(function (Collection $config) {
         return Credentials::factory($config->getAll(array_keys(Credentials::getConfigDefaults())));
     }))->addClientResolver(new BackoffOptionResolver(function () {
         return BackoffPlugin::getExponentialBackoff();
     }))->build();
     $this->assertInstanceOf('Aws\\DynamoDb\\DynamoDbClient', $client);
 }
 /**
  * Public constructor.
  *
  * Enables backoff support.
  *
  * @param string $baseUrl Base URL of the web service.
  * @param string $backoffRegex Optional error pattern to
  *                             search for in response bodies.
  */
 public function __construct($baseUrl = '', $backoffRegex = null)
 {
     if (is_string($backoffRegex)) {
         $backoffPlugin = new BackoffPlugin(new ResponseMatchesBackoffStrategy($backoffRegex));
     } else {
         $backoffPlugin = BackoffPlugin::getExponentialBackoff();
     }
     $config = array('request.options' => array('plugins' => array($backoffPlugin)));
     parent::__construct($baseUrl, $config);
 }
Example #4
0
 /**
  * Constructs a new instance of the {@see GitHub} class.
  *
  * @param string $user Your GitHub username.
  * @param string $pass Your GitHub password.
  */
 public function __construct($user, $pass)
 {
     $this->user = $user;
     $this->pass = $pass;
     $this->client = new Client('https://api.github.com', array('params.cache.override_ttl' => 3600));
     // Caching
     $this->client->addSubscriber(new CachePlugin(new DoctrineCacheAdapter(new PhpFileCache(VANITY_CACHE_DIR . '/github')), true));
     // Exponential backoff
     $this->client->addSubscriber(BackoffPlugin::getExponentialBackoff());
 }
 /**
  * Creates the GuzzleTeleporter
  *
  * @return GuzzleTeleporter
  */
 public static function create()
 {
     $client = new Client();
     $client->getEventDispatcher()->addListener('request.error', function (Event $event) {
         // override guzzle default behavior of throwing exceptions
         // when 4xx & 5xx responses are encountered
         $event->stopPropagation();
     }, -254);
     $client->addSubscriber(BackoffPlugin::getExponentialBackoff(5, array(500, 502, 503, 408)));
     return new static($client);
 }
 public function __construct(ClientInterface $client = null)
 {
     $this->client = $client;
     if (!$this->client) {
         $this->client = new Client();
         $this->client->getEventDispatcher()->addListener('request.error', function (Event $event) {
             // override guzzle default behavior of throwing exceptions
             // when 4xx & 5xx responses are encountered
             $event->stopPropagation();
         }, -254);
         $this->client->addSubscriber(BackoffPlugin::getExponentialBackoff(5, array(500, 502, 503, 408)));
     }
 }
 protected function getGuzzle(InputInterface $input)
 {
     if (null !== $this->guzzle) {
         return $this->guzzle;
     }
     if (!in_array($input->getArgument('es-scheme'), array('http', 'https'))) {
         throw new InvalidArgumentException("Elastic search scheme must be http or https");
     }
     $uri = sprintf('%s://%s:%s/%s', $input->getArgument('es-scheme'), rtrim($input->getArgument('es-host'), '/'), $input->getArgument('es-port'), $input->getArgument('es-index'));
     $this->guzzle = new Guzzle($uri);
     $this->guzzle->addSubscriber(BackoffPlugin::getExponentialBackoff());
     return $this->guzzle;
 }
Example #8
0
 public function testConstructorCallsResolvers()
 {
     $config = new Collection(array(Options::ENDPOINT_PROVIDER => $this->getMock('Aws\\Common\\Region\\EndpointProviderInterface')));
     $signature = new SignatureV4();
     $credentials = new Credentials('test', '123');
     $config->set('client.resolvers', array(new BackoffOptionResolver(function () {
         return BackoffPlugin::getExponentialBackoff();
     })));
     $client = $this->getMockBuilder('Aws\\Common\\Client\\AbstractClient')->setConstructorArgs(array($credentials, $signature, $config))->getMockForAbstractClass();
     // Ensure that lazy resolvers were triggered
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\BackoffPlugin', $client->getConfig(Options::BACKOFF));
     // Ensure that the client removed the option
     $this->assertNull($config->get('client.resolvers'));
 }
 public function testCreatesDefaultExponentialBackoffPlugin()
 {
     $plugin = BackoffPlugin::getExponentialBackoff(3, array(204), array(10));
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\BackoffPlugin', $plugin);
     $strategy = $this->readAttribute($plugin, 'strategy');
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\HttpBackoffStrategy', $strategy);
     $this->assertEquals(array(204 => true), $this->readAttribute($strategy, 'errorCodes'));
     $strategy = $this->readAttribute($strategy, 'next');
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\TruncatedBackoffStrategy', $strategy);
     $this->assertEquals(3, $this->readAttribute($strategy, 'max'));
     $strategy = $this->readAttribute($strategy, 'next');
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\CurlBackoffStrategy', $strategy);
     $this->assertEquals(array(10 => true), $this->readAttribute($strategy, 'errorCodes'));
     $strategy = $this->readAttribute($strategy, 'next');
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\ExponentialBackoffStrategy', $strategy);
 }
 /**
  * @param array $config
  * @return Client
  */
 public static function factory($config = array())
 {
     $default = array('url' => self::DEFAULT_API_URL);
     $required = array('token');
     $config = Collection::fromConfig($config, $default, $required);
     $config['curl.options'] = array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0);
     $config['request.options'] = array('headers' => array('X-StorageApi-Token' => $config->get('token')));
     $client = new self($config->get('url'), $config);
     // Attach a service description to the client
     $description = ServiceDescription::factory(__DIR__ . '/service.json');
     $client->setDescription($description);
     $client->setBaseUrl($config->get('url'));
     // Setup exponential backoff
     $backoffPlugin = BackoffPlugin::getExponentialBackoff();
     $client->addSubscriber($backoffPlugin);
     return $client;
 }
 /**
  * Create Securepass client using Guzzle
  *
  * @param array $config Client Configuration options
  *
  */
 public function __construct($config = array())
 {
     $default = array('base_url' => self::BASE_URL);
     // The following values are required when creating the client
     $required = array('base_url', 'app_id', 'app_secret');
     $config = Collection::fromConfig($config, $default, $required);
     // Create a new Securepass client
     $this->client = new Client($config->get('base_url'), $config);
     // set Securepass deafult headers
     $this->client->setDefaultOption('headers/X-SecurePass-App-ID', $config->get('app_id'));
     $this->client->setDefaultOption('headers/X-SecurePass-App-Secret', $config->get('app_secret'));
     $this->client->setDefaultOption('headers/Content-type', 'application/json');
     // set Useragent
     $this->client->setUserAgent('SecurePass-PHP/1.0"');
     // load services description
     $this->setServiceDescription();
     // Use a static factory method to get a backoff plugin using the exponential backoff strategy
     $backoffPlugin = BackoffPlugin::getExponentialBackoff();
     // Add the backoff plugin to the client object
     $this->client->addSubscriber($backoffPlugin);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $repositoryName = $this->parseRepositoryName($input->getOption('repository'));
     $client = new Client($input->getOption('api-url') . '{?access_token}', array('access_token' => $input->getOption('access-token'), 'request.options' => array('Content-Type' => 'application/json')));
     $client->addSubscriber(BackoffPlugin::getExponentialBackoff());
     $postData = $this->generatePostData($input);
     if (!isset($postData['coverage'])) {
         $output->write(sprintf('Notifying that no code coverage data is available for repository "%s" and revision "%s"... ', $repositoryName, $postData['revision']));
     } else {
         $output->write(sprintf('Uploading code coverage for repository "%s" and revision "%s"... ', $repositoryName, $postData['revision']));
     }
     try {
         $client->post('repositories/' . $repositoryName . '/data/code-coverage{?access_token}', null, json_encode($postData))->send();
         $output->writeln('Done');
         return 0;
     } catch (BadResponseException $ex) {
         $output->writeln("<error>Failed</error>");
         if ($ex instanceof ClientErrorResponseException) {
             $output->writeln('<error>' . $ex->getResponse()->getBody(true) . '</error>');
             return 1;
         }
         throw $ex;
     }
 }
Example #13
0
 /**
  * Returns the default exponential backoff plugin for a client
  *
  * @return BackoffOptionResolver
  */
 protected function getDefaultBackoffResolver()
 {
     return new BackoffOptionResolver(function () {
         return BackoffPlugin::getExponentialBackoff();
     });
 }
Example #14
0
 /**
  * Register the default subscribers for Guzzle
  *
  * @param array $options
  */
 public function registerGuzzleSubscribers(array $options)
 {
     $acceptJsonHeaderPlugin = new AcceptJsonHeaderPlugin();
     $this->addSubscriber($acceptJsonHeaderPlugin);
     $backoffPlugin = BackoffPlugin::getExponentialBackoff(5);
     $this->addSubscriber($backoffPlugin);
     if (array_key_exists('token', $options) && $options['token'] instanceof ApiToken) {
         $apiTokenPlugin = new ApiTokenPlugin($options['token']);
         $this->addSubscriber($apiTokenPlugin);
     }
 }
Example #15
0
 /**
  * Construct the Guzzle Http Client with the exponential retry plugin
  *
  * @access protected
  * @return \Guzzle|Http\Client
  */
 protected function getClient()
 {
     $retries = 5;
     $httpCodes = null;
     $curlCodes = null;
     $plugin = BackoffPlugin::getExponentialBackoff($retries, $httpCodes, $curlCodes);
     $client = new Client($this->baseUrl);
     $client->addSubscriber($plugin);
     return $client;
 }
Example #16
0
 public function testAddsDefaultBackoffPluginIfNeeded()
 {
     $config = array('service' => 'dynamodb', 'region' => 'us-east-1', 'service.description' => array('signatureVersion' => 'v2', 'regions' => array('us-east-1' => array('https' => true, 'hostname' => 'foo.com'))));
     // Ensure that a default plugin is set
     $client = ClientBuilder::factory('Aws\\DynamoDb')->setConfig($config)->build();
     $this->assertInstanceOf('Guzzle\\Plugin\\Backoff\\BackoffPlugin', $client->getConfig(Options::BACKOFF));
     // Ensure that the plugin is set
     $this->assertTrue($this->hasSubscriber($client, $client->getConfig(Options::BACKOFF)));
     // Ensure that a specific plugin can be used
     $plugin = BackoffPlugin::getExponentialBackoff();
     $config[Options::BACKOFF] = $plugin;
     $client = ClientBuilder::factory('Aws\\DynamoDb')->setConfig($config)->build();
     $this->assertSame($plugin, $client->getConfig(Options::BACKOFF));
     // Ensure that the plugin is set
     $this->assertTrue($this->hasSubscriber($client, $plugin));
 }
Example #17
0
 /**
  * Performs the building logic using all of the parameters that have been
  * set and falling back to default values. Returns an instantiate service
  * client with credentials prepared and plugins attached.
  *
  * @return AwsClientInterface
  * @throws InvalidArgumentException
  */
 public function build()
 {
     // Resolve configuration
     $config = Collection::fromConfig($this->config, array_merge(self::$commonConfigDefaults, $this->configDefaults), self::$commonConfigRequirements + $this->configRequirements);
     // Set values from the service description
     $signature = $this->getSignature($this->updateConfigFromDescription($config), $config);
     // Resolve credentials
     if (!($credentials = $config->get('credentials'))) {
         $credentials = Credentials::factory($config);
     }
     $backoff = $config->get(Options::BACKOFF);
     if ($backoff === null) {
         $backoff = BackoffPlugin::getExponentialBackoff();
         $config->set(Options::BACKOFF, $backoff);
     }
     if ($backoff) {
         $this->addBackoffLogger($backoff, $config);
     }
     // Determine service and class name
     $clientClass = 'Aws\\Common\\Client\\DefaultClient';
     if ($this->clientNamespace) {
         $serviceName = substr($this->clientNamespace, strrpos($this->clientNamespace, '\\') + 1);
         $clientClass = $this->clientNamespace . '\\' . $serviceName . 'Client';
     }
     /** @var $client AwsClientInterface */
     $client = new $clientClass($credentials, $signature, $config);
     $client->setDescription($config->get(Options::SERVICE_DESCRIPTION));
     // Add exception marshaling so that more descriptive exception are thrown
     if ($this->clientNamespace) {
         $exceptionFactory = new NamespaceExceptionFactory($this->exceptionParser ?: new DefaultXmlExceptionParser(), "{$this->clientNamespace}\\Exception", "{$this->clientNamespace}\\Exception\\{$serviceName}Exception");
         $client->addSubscriber(new ExceptionListener($exceptionFactory));
     }
     // 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');
     // Set the iterator resource factory based on the provided iterators config
     $client->setResourceIteratorFactory(new AwsResourceIteratorFactory($this->iteratorsConfig, new ResourceIteratorClassFactory($this->clientNamespace . '\\Iterator')));
     // 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;
 }
 private function getMocks()
 {
     $config = new Collection();
     $plugin = BackoffPlugin::getExponentialBackoff();
     $resolver = new BackoffOptionResolver(function ($config, $client = null) use($plugin) {
         return $plugin;
     });
     return array($config, $plugin, $resolver);
 }