diff --git a/README.md b/README.md index e5e1006..58575d1 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,23 @@ if ($response->isSuccessful() && 201 === $response->getStatusCode()) { } ``` +### Set custom headers and client timeout +```php +$client = new \RetailCrm\ApiClient( + 'https://demo.retailcrm.ru', + 'T9DMPvuNt7FQJMszHUdG8Fkt6xHsqngH', + \RetailCrm\ApiClient::V4 +); + +$options = new \RetailCrm\Http\RequestOptions( + ['X-Rlimit-Token' => 'example_token'], // array of custom headers + 10 // client timeout (in seconds) +); + +$client->request->setOptions($options); +``` + + ### Documentation * [English](https://help.retailcrm.pro/Developers) diff --git a/lib/RetailCrm/Client/AbstractLoader.php b/lib/RetailCrm/Client/AbstractLoader.php index c021a1a..ea5062d 100755 --- a/lib/RetailCrm/Client/AbstractLoader.php +++ b/lib/RetailCrm/Client/AbstractLoader.php @@ -15,6 +15,7 @@ namespace RetailCrm\Client; use RetailCrm\Http\Client; +use RetailCrm\Http\RequestOptions; /** * PHP version 5.4 @@ -67,6 +68,16 @@ abstract class AbstractLoader $this->siteCode = $site; } + /** + * Set request options + * + * @param RequestOptions $options + */ + public function setOptions(RequestOptions $options) + { + $this->client->setOptions($options); + } + /** * Set site * diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index f0c2840..58f8f49 100755 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -37,6 +37,7 @@ class Client protected $url; protected $defaultParameters; + protected $options; /** * Client constructor. @@ -57,6 +58,7 @@ class Client $this->url = $url; $this->defaultParameters = $defaultParameters; + $this->options = new RequestOptions(); } /** @@ -112,8 +114,12 @@ class Client curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30); - curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($curlHandler, CURLOPT_TIMEOUT, $this->options->getClientTimeout()); + curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, $this->options->getClientTimeout()); + + if ($this->options->getHeaders()) { + curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $this->options->getHttpHeaders()); + } if (self::METHOD_POST === $method) { curl_setopt($curlHandler, CURLOPT_POST, true); @@ -168,4 +174,14 @@ class Client ) { return $this->makeRawRequest($path, $method, $parameters, $fullPath)->asJsonResponse(); } + + /** + * Set request options + * + * @param RequestOptions $options + */ + public function setOptions(RequestOptions $options) + { + $this->options = $options; + } } diff --git a/lib/RetailCrm/Http/RequestOptions.php b/lib/RetailCrm/Http/RequestOptions.php new file mode 100644 index 0000000..486b9de --- /dev/null +++ b/lib/RetailCrm/Http/RequestOptions.php @@ -0,0 +1,101 @@ + + * @license https://opensource.org/licenses/MIT MIT License + * @link https://help.retailcrm.ru/Developers/ApiVersion5 + */ + +namespace RetailCrm\Http; + +/** + * PHP version 5.4 + * + * Request options class + * + * @category RetailCrm + * @package RetailCrm + * @author RetailCrm + * @license https://opensource.org/licenses/MIT MIT License + * @link https://help.retailcrm.ru/Developers/ApiVersion5 + */ +class RequestOptions +{ + /** @var array */ + private $headers; + + /** @var int */ + private $clientTimeout; + + /** + * Init request options. + * + * @param array $headers + * @param int $clientTimeout + */ + public function __construct($headers = array(), $clientTimeout = 30) + { + $this->headers = $headers; + $this->clientTimeout = $clientTimeout; + } + + /** + * @return array + */ + public function getHeaders() + { + return $this->headers; + } + + /** + * @return array + */ + public function getHttpHeaders() + { + $headers = []; + + foreach ($this->headers as $header => $value) { + $headers[] = sprintf("%s: %s", $header, $value); + } + + return $headers; + } + + /** + * @param array $headers + * + * @return self + */ + public function setHeaders($headers) + { + $this->headers = $headers; + + return $this; + } + + /** + * @return int + */ + public function getClientTimeout() + { + return $this->clientTimeout; + } + + /** + * @param int $clientTimeout + * + * @return self + */ + public function setClientTimeout($clientTimeout) + { + $this->clientTimeout = $clientTimeout; + + return $this; + } +} diff --git a/tests/RetailCrm/Tests/Http/RequestOptionsTest.php b/tests/RetailCrm/Tests/Http/RequestOptionsTest.php new file mode 100644 index 0000000..f4b671c --- /dev/null +++ b/tests/RetailCrm/Tests/Http/RequestOptionsTest.php @@ -0,0 +1,25 @@ + 'Test']; + $options = new RequestOptions($headers); + + self::assertEquals('X-Test-Header: Test', $options->getHttpHeaders()[0]); + self::assertEquals('Test', $options->getHeaders()['X-Test-Header']); + } + + public function testGetClientTimeout() + { + $options = new RequestOptions([], 10); + + self::assertEquals(10, $options->getClientTimeout()); + } +}