only configure the http client once we start using it

This commit is contained in:
Robin Appelman 2016-05-12 13:29:45 +02:00
parent 9b3eefbf95
commit ea0f09a7ed
1 changed files with 23 additions and 6 deletions

View File

@ -39,6 +39,7 @@ class Client implements IClient {
private $config;
/** @var ICertificateManager */
private $certificateManager;
private $configured = false;
/**
* @param IConfig $config
@ -51,13 +52,16 @@ class Client implements IClient {
$this->config = $config;
$this->client = $client;
$this->certificateManager = $certificateManager;
$this->setDefaultOptions();
}
/**
* Sets the default options to the client
*/
private function setDefaultOptions() {
if ($this->configured) {
return;
}
$this->configured = true;
// Either use user bundle or the system bundle if nothing is specified
if ($this->certificateManager->listCertificates() !== []) {
$this->client->setDefaultOption('verify', $this->certificateManager->getAbsoluteBundlePath());
@ -80,6 +84,7 @@ class Client implements IClient {
/**
* Get the proxy URI
*
* @return string
*/
private function getProxyUri() {
@ -99,6 +104,7 @@ class Client implements IClient {
/**
* Sends a GET request
*
* @param string $uri
* @param array $options Array such as
* 'query' => [
@ -126,6 +132,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function get($uri, array $options = []) {
$this->setDefaultOptions();
$response = $this->client->get($uri, $options);
$isStream = isset($options['stream']) && $options['stream'];
return new Response($response, $isStream);
@ -133,6 +140,7 @@ class Client implements IClient {
/**
* Sends a HEAD request
*
* @param string $uri
* @param array $options Array such as
* 'headers' => [
@ -155,12 +163,14 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function head($uri, $options = []) {
$this->setDefaultOptions();
$response = $this->client->head($uri, $options);
return new Response($response);
}
/**
* Sends a POST request
*
* @param string $uri
* @param array $options Array such as
* 'body' => [
@ -188,12 +198,14 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function post($uri, array $options = []) {
$this->setDefaultOptions();
$response = $this->client->post($uri, $options);
return new Response($response);
}
/**
* Sends a PUT request
*
* @param string $uri
* @param array $options Array such as
* 'body' => [
@ -221,12 +233,14 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function put($uri, array $options = []) {
$this->setDefaultOptions();
$response = $this->client->put($uri, $options);
return new Response($response);
}
/**
* Sends a DELETE request
*
* @param string $uri
* @param array $options Array such as
* 'body' => [
@ -254,6 +268,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function delete($uri, array $options = []) {
$this->setDefaultOptions();
$response = $this->client->delete($uri, $options);
return new Response($response);
}
@ -261,6 +276,7 @@ class Client implements IClient {
/**
* Sends a options request
*
* @param string $uri
* @param array $options Array such as
* 'body' => [
@ -288,6 +304,7 @@ class Client implements IClient {
* @throws \Exception If the request could not get completed
*/
public function options($uri, array $options = []) {
$this->setDefaultOptions();
$response = $this->client->options($uri, $options);
return new Response($response);
}