/** * 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; }
/** * 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; }
/** * @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']); }
/** * @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; }
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); }
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(); }
/** * 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); }
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; }
public function getDefaultOptions() { return $this->client->getDefaultOption(); }
/** * @param Client $client */ protected function copyClientDefaults(Client $client) { $this->request->setUrl($client->getBaseUrl() . $this->request->getUrl()); $this->request->addHeaders($client->getDefaultOption('headers')); }
/** * Prepares the query with the default parameters * * @param array $options * * @return array */ private function prepareQuery($options = []) { return array_merge($this->client->getDefaultOption('query'), $options); }
public function testAuthHeaderSet() { $api = new API('1234', '4321', $this->client); $headers = $this->client->getDefaultOption('headers'); $this->assertEquals('Bearer 1234 4321', $headers['Auth']); }