Additional request options
This commit is contained in:
parent
e3b452a0c2
commit
88cb6526b0
17
README.md
17
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)
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
101
lib/RetailCrm/Http/RequestOptions.php
Normal file
101
lib/RetailCrm/Http/RequestOptions.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 5.4
|
||||
*
|
||||
* Request options
|
||||
*
|
||||
* @category RetailCrm
|
||||
* @package RetailCrm
|
||||
* @author RetailCrm <integration@retailcrm.ru>
|
||||
* @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 <integration@retailcrm.ru>
|
||||
* @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;
|
||||
}
|
||||
}
|
25
tests/RetailCrm/Tests/Http/RequestOptionsTest.php
Normal file
25
tests/RetailCrm/Tests/Http/RequestOptionsTest.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Tests\Http;
|
||||
|
||||
use RetailCrm\Http\RequestOptions;
|
||||
use RetailCrm\Test\TestCase;
|
||||
|
||||
class RequestOptionsTest extends TestCase
|
||||
{
|
||||
public function testGetHeaders()
|
||||
{
|
||||
$headers = ['X-Test-Header' => '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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user