コード例 #1
0
ファイル: Api.php プロジェクト: bein-sports/openl10n-sdk
 public function __construct(Config $config)
 {
     $defaultOptions = ['scheme' => 'http'];
     $requiredOptions = ['hostname'];
     $options = ['hostname' => $config->getHostname(), 'scheme' => $config->getUseSsl() ? 'https' : 'http', 'port' => $config->getPort()];
     $options = Collection::fromConfig($options, $defaultOptions, $requiredOptions);
     $this->client = new Client(['base_url' => ['{scheme}://{hostname}:{port}/api/', $options->toArray()], 'defaults' => ['auth' => [$config->getLogin(), $config->getPassword()], 'expect' => false, 'headers' => ['Accept' => 'application/json', 'User-Agent' => 'Openl10n ' . Client::getDefaultUserAgent()]]]);
     $this->registerDefaultEntryPoints();
 }
コード例 #2
0
 /**
  * Set adapter and create Guzzle base client.
  *
  * @return \GuzzleHttp\Client
  */
 private function loadBaseClient(array $settings = [])
 {
     // Force the authorization scheme to 'tevoauth'
     $settings['defaults']['auth'] = 'tevoauth';
     if ($this->_baseClientAdapter) {
         $settings['adapter'] = $this->_baseClientAdapter;
     }
     $this->_baseClient = new BaseClient($settings);
     // Attach the TEvoAuth subscriber to handle the required authorization
     $this->_baseClient->getEmitter()->attach(new TEvoAuth($this->_settings['apiToken'], $this->_settings['apiSecret']));
     // Don't need these any more
     unset($this->_settings['apiToken'], $this->_settings['apiSecret']);
     // Set a custom User Agent indicating which version of this library we are using.
     // Can’t set this in $settings['defaults'] above because we want to prepend the default.
     $this->_baseClient->setDefaultOption('headers/User-Agent', 'ticketevolution-php/' . self::VERSION . ' ' . $this->_baseClient->getDefaultUserAgent());
     return $this->_baseClient;
 }
コード例 #3
0
 public function testAddsDefaultUserAgentHeaderWithoutDefaultOptions()
 {
     $client = new Client();
     $this->assertEquals(['User-Agent' => Client::getDefaultUserAgent()], $client->getDefaultOption('headers'));
 }
コード例 #4
0
 private function createGuzzleRequest(Psr7Request $psrRequest, array $options)
 {
     $ringConfig = [];
     // Remove unsupported options.
     foreach (array_keys($options) as $key) {
         if (!isset(self::$validOptions[$key])) {
             unset($options[$key]);
         }
     }
     // Handle delay option.
     if (isset($options['delay'])) {
         $ringConfig['delay'] = $options['delay'];
         unset($options['delay']);
     }
     // Prepare sink option.
     if (isset($options['sink'])) {
         $ringConfig['save_to'] = $options['sink'] instanceof Psr7StreamInterface ? new GuzzleStream($options['sink']) : $options['sink'];
         unset($options['sink']);
     }
     // Ensure that all requests are async and lazy like Guzzle 6.
     $options['future'] = 'lazy';
     // Create the Guzzle 5 request from the provided PSR7 request.
     $request = $this->client->createRequest($psrRequest->getMethod(), $psrRequest->getUri(), $options);
     // For the request body, adapt the PSR stream to a Guzzle stream.
     $body = $psrRequest->getBody();
     if ($body->getSize() === 0) {
         $request->setBody(null);
     } else {
         $request->setBody(new GuzzleStream($body));
     }
     $request->setHeaders($psrRequest->getHeaders());
     $request->setHeader('User-Agent', 'aws-sdk-php/' . Sdk::VERSION . ' ' . Client::getDefaultUserAgent());
     // Make sure the delay is configured, if provided.
     if ($ringConfig) {
         foreach ($ringConfig as $k => $v) {
             $request->getConfig()->set($k, $v);
         }
     }
     return $request;
 }
コード例 #5
0
 private function createGuzzleRequest(Psr7Request $psrRequest, array $options)
 {
     $ringConfig = [];
     $statsCallback = isset($options['http_stats_receiver']) ? $options['http_stats_receiver'] : null;
     unset($options['http_stats_receiver']);
     // Remove unsupported options.
     foreach (array_keys($options) as $key) {
         if (!isset(self::$validOptions[$key])) {
             unset($options[$key]);
         }
     }
     // Handle delay option.
     if (isset($options['delay'])) {
         $ringConfig['delay'] = $options['delay'];
         unset($options['delay']);
     }
     // Prepare sink option.
     if (isset($options['sink'])) {
         $ringConfig['save_to'] = $options['sink'] instanceof Psr7StreamInterface ? new GuzzleStream($options['sink']) : $options['sink'];
         unset($options['sink']);
     }
     // Ensure that all requests are async and lazy like Guzzle 6.
     $options['future'] = 'lazy';
     // Create the Guzzle 5 request from the provided PSR7 request.
     $request = $this->client->createRequest($psrRequest->getMethod(), $psrRequest->getUri(), $options);
     if (is_callable($statsCallback)) {
         $request->getEmitter()->on('end', function (EndEvent $event) use($statsCallback) {
             $statsCallback($event->getTransferInfo());
         });
     }
     // For the request body, adapt the PSR stream to a Guzzle stream.
     $body = $psrRequest->getBody();
     if ($body->getSize() === 0) {
         $request->setBody(null);
     } else {
         $request->setBody(new GuzzleStream($body));
     }
     $request->setHeaders($psrRequest->getHeaders());
     $request->setHeader('User-Agent', $request->getHeader('User-Agent') . ' ' . Client::getDefaultUserAgent());
     // Make sure the delay is configured, if provided.
     if ($ringConfig) {
         foreach ($ringConfig as $k => $v) {
             $request->getConfig()->set($k, $v);
         }
     }
     return $request;
 }
コード例 #6
0
 /**
  * The client constructor accepts the following options:.
  *
  * - api_provider: (callable) An optional PHP callable that accepts a
  *   type, service, and version argument, and returns an array of
  *   corresponding configuration data. The type value can be one of api,
  *   waiter, or paginator.
  * - client: (GuzzleHttp\ClientInterface) Optional Guzzle client used to
  *   transfer requests over the wire. If you do not specify a client, the
  *   SDK will create a new client that uses a shared Ring HTTP handler
  *   with other clients.
  * - credentials:
  *   (array|Vws\Credentials\CredentialsInterface|bool|callable) An
  *   Vws\Credentials\CredentialsInterface object to use with each, an
  *   associative array of "username", "password", and "subscription_token" key value pairs,
  *   `false` to utilize null credentials, or a callable credentials
  *   provider function to create credentials using a function. If no
  *   credentials are provided, the SDK will attempt to load them from the
  *   environment.
  * - debug: (bool|resource) Set to true to display debug information
  *   when sending requests. Provide a stream resource to write debug
  *   information to a specific resource.
  * - endpoint: (string) The full URI of the webservice. This is only
  *   required when connecting to a custom endpoint (e.g., a local version
  *   of S3).
  * - endpoint_provider: (callable) An optional PHP callable that
  *   accepts a hash of options including a service and region key and
  *   returns a hash of endpoint data, of which the endpoint key is
  *   required.
  * - http: (array) Set to an array of Guzzle client request options
  *   (e.g., proxy, verify, etc.). See
  *   http://docs.guzzlephp.org/en/latest/clients.html#request-options for a
  *   list of available options.
  * - profile: (string) Allows you to specify which profile to use when
  *   credentials are created from the Vws credentials file in your HOME
  *   directory. This setting overrides the Vws_PROFILE environment
  *   variable. Note: Specifying "profile" will cause the "credentials" key
  *   to be ignored.
  * - region: (string, required) Region to connect to.
  * - retries: (int, default=int(3)) Configures the maximum number of
  *   allowed retries for a client (pass 0 to disable retries).
  * - retry_logger: (string|Psr\Log\LoggerInterface) When the string "debug"
  *   is provided, all retries will be logged to STDOUT. Provide a PSR-3
  *   logger to log retries to a specific logger instance.
  * - ringphp_handler: (callable) RingPHP handler used to transfer HTTP
  *   requests (see http://ringphp.readthedocs.org/en/latest/).
  * - scheme: (string, default=string(5) "https") URI scheme to use when
  *   connecting connect.
  * - service: (string, required) Name of the service to utilize. This
  *   value will be supplied by default.
  * - validate: (bool, default=bool(true)) Set to false to disable
  *   client-side parameter validation.
  * - version: (string, required) The version of the webservice to
  *   utilize (e.g., 2006-03-01).
  *
  * @param array $args Client configuration arguments.
  *
  * @throws \InvalidArgumentException if any required options are missing
  */
 public function __construct(array $args)
 {
     list($service, $exceptionClass) = $this->parseClass();
     if (!isset($args['service'])) {
         $args['service'] = manifest($service)['endpoint'];
     }
     if (!isset($args['exception_class'])) {
         $this->commandException = $exceptionClass;
     }
     $resolver = new ClientResolver(static::getArguments());
     $config = $resolver->resolve($args, $this->getEmitter());
     $this->api = $config['api'];
     $this->serializer = $config['serializer'];
     $this->errorParser = $config['error_parser'];
     $this->endpoint = $config['endpoint'];
     $this->credentials = $config['credentials'];
     $this->region = isset($config['region']) ? $config['region'] : null;
     $this->applyParser();
     parent::__construct($config['client'], $config['config']);
     // Make sure the user agent is prefixed by the SDK version.
     $client = $this->getHttpClient();
     $client->setDefaultOption('allow_redirects', false);
     $client->setDefaultOption('headers/User-Agent', 'vws-sdk-php/' . Sdk::VERSION . ' ' . Client::getDefaultUserAgent());
     if (isset($args['with_resolved'])) {
         /* @var callable $withResolved */
         $args['with_resolved']($config);
     }
 }
コード例 #7
0
 /**
  * Prepends the SDK's version number to the standard Guzzle string.
  *
  * @return string
  */
 public static function getDefaultUserAgent()
 {
     return sprintf("OpenStack/%f %s", Bootstrap::VERSION, Client::getDefaultUserAgent());
 }
コード例 #8
0
 public static function getDefaultUserAgent()
 {
     return 'OPCache/' . self::getDrupalModuleVersion() . ' ' . parent::getDefaultUserAgent();
 }