Exemplo n.º 1
0
 /**
  * Get a configured client, creating one if one hasn't been set by
  * setClient().
  *
  * @return Client
  */
 public function getClient()
 {
     if (!$this->client) {
         $this->client = new Client();
         $headers = $this->client->getDefaultOption('headers');
         $headers['User-Agent'] = 'PkpPlnBot 1.0; http://pkp.sfu.ca';
         $this->client->setDefaultOption('headers', $headers);
     }
     return $this->client;
 }
Exemplo n.º 2
0
 /**
  * Wait for a container to finish
  *
  * @param \Docker\Container $container
  * @param integer|null      $timeout
  *
  * @throws \Docker\Exception\UnexpectedStatusCodeException
  *
  * @return \Docker\Manager\ContainerManager
  */
 public function wait(Container $container, $timeout = null)
 {
     $response = $this->client->post(['/containers/{id}/wait', ['id' => $container->getId()]], ['timeout' => null === $timeout ? $this->client->getDefaultOption('timeout') : $timeout]);
     if ($response->getStatusCode() !== "200") {
         throw UnexpectedStatusCodeException::fromResponse($response);
     }
     $container->setExitCode($response->json()['StatusCode']);
     $this->inspect($container);
     return $this;
 }
Exemplo n.º 3
0
 /**
  * @param array $credentials
  * @dataProvider credentialsProvider
  */
 public function testAuthenticateClient($credentials)
 {
     $client = new Client();
     $auth = new Basic($credentials);
     $auth->authenticateClient(new RestClient($client));
     self::assertEquals(['test', 'pass'], $client->getDefaultOption('auth'));
     $request = $client->createRequest('GET', '/');
     self::assertArrayHasKey('Authorization', $request->getHeaders());
     self::assertEquals(['Basic dGVzdDpwYXNz'], $request->getHeaders()['Authorization']);
 }
Exemplo n.º 4
0
 /**
  * @param $capability
  * @param array $options
  * @throws Exceptions\CapabilityUnavailable
  * @throws Exceptions\RETSException
  * @return \GuzzleHttp\Message\ResponseInterface
  */
 protected function request($capability, $options = [])
 {
     $url = $this->capabilities->get($capability);
     if (!$url) {
         throw new CapabilityUnavailable("'{$capability}' tried but no valid endpoint was found.  Did you forget to Login()?");
     }
     if (!array_key_exists('headers', $options)) {
         $options['headers'] = [];
     }
     // Guzzle 5 changed the order that headers are added to the request, so we'll do this manually
     $options['headers'] = array_merge($this->client->getDefaultOption('headers'), $options['headers']);
     // user-agent authentication
     if ($this->configuration->getUserAgentPassword()) {
         $ua_digest = $this->configuration->userAgentDigestHash($this);
         $options['headers'] = array_merge($options['headers'], ['RETS-UA-Authorization' => 'Digest ' . $ua_digest]);
     }
     $options = array_merge($options, ['cookies' => $this->cookie_jar]);
     $this->debug("Sending HTTP Request for {$url} ({$capability})", $options);
     if (array_key_exists('query', $options)) {
         $this->last_request_url = $url . '?' . \http_build_query($options['query']);
     } else {
         $this->last_request_url = $url;
     }
     /** @var \GuzzleHttp\Message\ResponseInterface $response */
     if ($this->configuration->readOption('use_post_method')) {
         $this->debug('Using POST method per use_post_method option');
         $query = array_key_exists('query', $options) ? $options['query'] : null;
         $response = $this->client->post($url, array_merge($options, ['body' => $query]));
     } else {
         $response = $this->client->get($url, $options);
     }
     $this->last_response = $response;
     $cookie = $response->getHeader('Set-Cookie');
     if ($cookie) {
         if (preg_match('/RETS-Session-ID\\=(.*?)(\\;|\\s+|$)/', $cookie, $matches)) {
             $this->rets_session_id = $matches[1];
         }
     }
     if ($response->getHeader('Content-Type') == 'text/xml' and $capability != 'GetObject') {
         $xml = $response->xml();
         if ($xml and isset($xml['ReplyCode'])) {
             $rc = (string) $xml['ReplyCode'];
             // 20201 - No records found - not exception worthy in my mind
             if ($rc != "0" and $rc != "20201") {
                 throw new RETSException($xml['ReplyText'], (int) $xml['ReplyCode']);
             }
         }
     }
     $this->debug('Response: HTTP ' . $response->getStatusCode());
     return $response;
 }
Exemplo n.º 5
0
 public function testAuthenticateClient()
 {
     $client = new Client();
     $restClient = new RestClient($client);
     $auth = new OAuth10(['oauth_api' => ['credentials' => ['#data' => '{"oauth_token": "token", "oauth_token_secret": "token_secret"}', 'appKey' => 'aaa', '#appSecret' => 'bbb']]]);
     $auth->authenticateClient($restClient);
     self::assertEquals('oauth', $client->getDefaultOption('auth'));
     $request = $restClient->createRequest(['endpoint' => '/']);
     $mock = new Mock([new Response(200, [], Stream::factory('{}'))]);
     $client->getEmitter()->attach($mock);
     $history = new History();
     $client->getEmitter()->attach($history);
     $restClient->download($request);
     $authHeader = $history->getLastRequest()->getHeaders()['Authorization'][0];
     self::assertRegexp('/^OAuth oauth_consumer_key="aaa", oauth_nonce="([0-9a-zA-Z]*)", oauth_signature="([0-9a-zA-Z%]*)", oauth_signature_method="HMAC-SHA1", oauth_timestamp="([0-9]{10})", oauth_token="token", oauth_version="1.0"$/', $authHeader);
 }
Exemplo n.º 6
0
 public function testUsesProxyEnvironmentVariables()
 {
     $client = new Client();
     $this->assertNull($client->getDefaultOption('proxy'));
     putenv('HTTP_PROXY=127.0.0.1');
     $client = new Client();
     $this->assertEquals(['http' => '127.0.0.1'], $client->getDefaultOption('proxy'));
     putenv('HTTPS_PROXY=127.0.0.2');
     $client = new Client();
     $this->assertEquals(['http' => '127.0.0.1', 'https' => '127.0.0.2'], $client->getDefaultOption('proxy'));
     putenv('HTTP_PROXY=');
     putenv('HTTPS_PROXY=');
 }
 /**
  * Remove the token for the given user in storage.
  *
  * @return void
  */
 public function removeToken()
 {
     $this->client->getDefaultOption('cookies')->clear();
 }
Exemplo n.º 8
0
 /**
  * Returns the specified request option or all options if none specified.
  *
  * @param null $keyOrPath
  *
  * @return array|mixed|null
  */
 public function getRequestOption($keyOrPath = null)
 {
     return $this->guzzleClient->getDefaultOption($keyOrPath);
 }
Exemplo n.º 9
0
 public function testUsesProxyEnvironmentVariables()
 {
     $http = isset($_SERVER['HTTP_PROXY']) ? $_SERVER['HTTP_PROXY'] : null;
     $https = isset($_SERVER['HTTPS_PROXY']) ? $_SERVER['HTTPS_PROXY'] : null;
     unset($_SERVER['HTTP_PROXY']);
     unset($_SERVER['HTTPS_PROXY']);
     $client = new Client();
     $this->assertNull($client->getDefaultOption('proxy'));
     $_SERVER['HTTP_PROXY'] = '127.0.0.1';
     $client = new Client();
     $this->assertEquals(['http' => '127.0.0.1'], $client->getDefaultOption('proxy'));
     $_SERVER['HTTPS_PROXY'] = '127.0.0.2';
     $client = new Client();
     $this->assertEquals(['http' => '127.0.0.1', 'https' => '127.0.0.2'], $client->getDefaultOption('proxy'));
     $_SERVER['HTTP_PROXY'] = $http;
     $_SERVER['HTTPS_PROXY'] = $https;
 }
Exemplo n.º 10
0
 public function getDefaultOptions()
 {
     return $this->client->getDefaultOption();
 }
Exemplo n.º 11
0
 /**
  * @param Client $client
  */
 protected function copyClientDefaults(Client $client)
 {
     $this->request->setUrl($client->getBaseUrl() . $this->request->getUrl());
     $this->request->addHeaders($client->getDefaultOption('headers'));
 }
Exemplo n.º 12
0
 /**
  * Prepares the query with the default parameters
  *
  * @param array $options
  *
  * @return array
  */
 private function prepareQuery($options = [])
 {
     return array_merge($this->client->getDefaultOption('query'), $options);
 }
Exemplo n.º 13
0
 public function testAuthHeaderSet()
 {
     $api = new API('1234', '4321', $this->client);
     $headers = $this->client->getDefaultOption('headers');
     $this->assertEquals('Bearer 1234 4321', $headers['Auth']);
 }