1
0
mirror of synced 2024-11-21 21:06:07 +03:00

Additional request options

This commit is contained in:
Akolzin Dmitry 2020-08-21 10:24:55 +03:00 committed by GitHub
parent e3b452a0c2
commit 88cb6526b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 2 deletions

View File

@ -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)

View File

@ -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
*

View File

@ -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;
}
}

View 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;
}
}

View 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());
}
}