diff --git a/src/Http/PipedriveClient.php b/src/Http/PipedriveClient.php index ae00bff..3fe129d 100644 --- a/src/Http/PipedriveClient.php +++ b/src/Http/PipedriveClient.php @@ -29,9 +29,10 @@ class PipedriveClient implements Client * GuzzleClient constructor. * * @param $url - * @param $token + * @param $credentials + * @param array $requestOptions */ - public function __construct($url, $credentials) + public function __construct($url, $credentials, $requestOptions = []) { list($headers, $query) = [[], []]; @@ -42,14 +43,16 @@ public function __construct($url, $credentials) $query['api_token'] = $credentials; } - $this->client = new GuzzleClient( - [ - 'base_uri' => $url, - 'allow_redirects' => false, - 'headers' => $headers, - 'query' => $query - ] - ); + $config = [ + 'base_uri' => $url, + 'allow_redirects' => false, + 'headers' => $headers, + 'query' => $query + ]; + + $config = array_merge($config, $requestOptions); + + $this->client = new GuzzleClient($config); } /** @@ -70,7 +73,7 @@ public static function OAuth($url, $storage, $pipedrive) $token->refreshIfNeeded($pipedrive); - return new self($url, $token); + return new self($url, $token, $pipedrive->getRequestOptions()); } /** diff --git a/src/Http/PipedriveClient4.php b/src/Http/PipedriveClient4.php index 9f4198b..2e81164 100644 --- a/src/Http/PipedriveClient4.php +++ b/src/Http/PipedriveClient4.php @@ -33,15 +33,20 @@ class PipedriveClient4 implements Client * * @param $url * @param $token + * @param array $requestOptions */ - public function __construct($url, $token) + public function __construct($url, $token, $requestOptions = []) { + $defaults = [ + 'query' => ['api_token' => $token] + ]; + + $defaults = array_merge($defaults, $requestOptions); + $this->client = new GuzzleClient( [ 'base_url' => $url, - 'defaults' => [ - 'query' => ['api_token' => $token], - ] + 'defaults' => $defaults ] ); } diff --git a/src/Pipedrive.php b/src/Pipedrive.php index dc28e93..a24ce97 100644 --- a/src/Pipedrive.php +++ b/src/Pipedrive.php @@ -171,6 +171,13 @@ class Pipedrive */ protected $storage; + /** + * A list of request options. + * + * @var array + */ + protected $requestOptions; + public function isOauth() { return $this->isOauth; @@ -179,15 +186,20 @@ public function isOauth() /** * Pipedrive constructor. * - * @param $token + * @param string $token + * @param string $uri + * @param int $guzzleVersion + * @param array $requestOptions */ - public function __construct($token = '', $uri = 'https://api.pipedrive.com/v1/', $guzzleVersion = 6) + public function __construct($token = '', $uri = 'https://api.pipedrive.com/v1/', $guzzleVersion = 6, $requestOptions = []) { $this->token = $token; $this->baseURI = $uri; $this->guzzleVersion = $guzzleVersion; $this->isOauth = false; + + $this->requestOptions = is_array($requestOptions) ? $requestOptions : []; } /** @@ -198,9 +210,10 @@ public function __construct($token = '', $uri = 'https://api.pipedrive.com/v1/', */ public static function OAuth($config) { - $guzzleVersion = isset($config['guzzleVersion']) ? $config['guzzleVersion'] : 6; + $guzzleVersion = isset($config['guzzleVersion']) ? $config['guzzleVersion'] : 6; + $requestOptions = isset($config['requestOptions']) ? $config['requestOptions'] : []; - $new = new self('oauth', 'https://api.pipedrive.com/', $guzzleVersion); + $new = new self('oauth', 'https://api.pipedrive.com/', $guzzleVersion, $requestOptions); $new->isOauth = true; @@ -352,9 +365,9 @@ protected function getClient() if ($this->guzzleVersion >= 6) { return $this->isOauth() ? PipedriveClient::OAuth($this->getBaseURI(), $this->storage, $this) - : new PipedriveClient($this->getBaseURI(), $this->token); + : new PipedriveClient($this->getBaseURI(), $this->token, $this->requestOptions); } else { - return new PipedriveClient4($this->getBaseURI(), $this->token); + return new PipedriveClient4($this->getBaseURI(), $this->token, $this->requestOptions); } } @@ -388,6 +401,14 @@ public function setToken($token) $this->token = $token; } + /** + * Get a list of request options. + */ + public function getRequestOptions() + { + return $this->requestOptions; + } + /** * Any reading will return a resource. * diff --git a/src/PipedriveToken.php b/src/PipedriveToken.php index 1c25dda..01377cc 100644 --- a/src/PipedriveToken.php +++ b/src/PipedriveToken.php @@ -90,12 +90,16 @@ public function refreshIfNeeded($pipedrive) return; } - $client = new GuzzleClient([ + $config = [ 'auth' => [ $pipedrive->getClientId(), $pipedrive->getClientSecret() ] - ]); + ]; + + $config = array_merge($config, $pipedrive->getRequestOptions()); + + $client = new GuzzleClient($config); $response = $client->request('POST', 'https://oauth.pipedrive.com/oauth/token', [ 'form_params' => [