Status code for response models, PSR fixes
This commit is contained in:
parent
a520bdfd44
commit
70d9d31a02
@ -13,13 +13,14 @@
|
||||
|
||||
namespace RetailCrm\Mg\Bot;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RetailCrm\Common\Exception\InvalidJsonException;
|
||||
use RetailCrm\Common\Url;
|
||||
use RetailCrm\Common\Serializer;
|
||||
use RetailCrm\Mg\Bot\Model\Request\GetFileRequest;
|
||||
use RetailCrm\Mg\Bot\Model\Request\UploadFileByUrlRequest;
|
||||
use RetailCrm\Mg\Bot\Model\Response\FullFileResponse;
|
||||
use RetailCrm\Mg\Bot\Model\Response\ListResponse;
|
||||
use RetailCrm\Mg\Bot\Model\Response\UploadFileResponse;
|
||||
|
||||
/**
|
||||
* PHP version 7.0
|
||||
@ -85,15 +86,16 @@ class Client
|
||||
$request
|
||||
);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
$data = json_decode((string) $response->getBody(), true);
|
||||
|
||||
if (json_last_error() == JSON_ERROR_NONE) {
|
||||
if ($arrayOfObjects) {
|
||||
return new ListResponse($responseType, $data);
|
||||
return new ListResponse($responseType, $data, $statusCode);
|
||||
} else {
|
||||
$obj = Serializer::deserialize($data, $responseType, Serializer::S_ARRAY);
|
||||
|
||||
if ($response->getStatusCode() >= 400
|
||||
if ($statusCode >= 400
|
||||
&& method_exists($obj, 'setErrors')
|
||||
&& method_exists($obj, 'getErrors')
|
||||
&& count(call_user_func([$obj, 'getErrors'])) == 0
|
||||
@ -101,6 +103,10 @@ class Client
|
||||
call_user_func_array([$obj, 'setErrors'], [['Status Code ' . $response->getStatusCode()]]);
|
||||
}
|
||||
|
||||
if (method_exists($obj, 'setStatusCode')) {
|
||||
call_user_func_array([$obj, 'setStatusCode'], [$statusCode]);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
} else {
|
||||
@ -487,13 +493,24 @@ class Client
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return Model\Response\UploadFileResponse
|
||||
* @return Model\Response\UploadFileResponse|null
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function uploadFile(string $filename)
|
||||
{
|
||||
return $this->client->postFile($filename);
|
||||
$response = $this->client->postFile($filename);
|
||||
|
||||
if ($response instanceof ResponseInterface) {
|
||||
$obj = Serializer::deserialize(
|
||||
(string) $response->getBody(),
|
||||
self::getResponseClass('UploadFileResponse')
|
||||
);
|
||||
|
||||
return $obj instanceof UploadFileResponse ? $obj : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,6 @@ use RetailCrm\Common\Exception\LimitException;
|
||||
use InvalidArgumentException;
|
||||
use RetailCrm\Common\Serializer;
|
||||
use RetailCrm\Common\Url;
|
||||
use RetailCrm\Mg\Bot\Model\Response\UploadFileResponse;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
@ -177,11 +176,11 @@ class HttpClient
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return UploadFileResponse
|
||||
* @return ResponseInterface|null
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function postFile(string $filename): UploadFileResponse
|
||||
public function postFile(string $filename)
|
||||
{
|
||||
if (!file_exists($filename)) {
|
||||
throw new \InvalidArgumentException("File doesn't exist");
|
||||
@ -206,12 +205,7 @@ class HttpClient
|
||||
throw new \Exception($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
|
||||
$obj = Serializer::deserialize(
|
||||
(string) $responseData->getBody(),
|
||||
'RetailCrm\Mg\Bot\Model\Response\UploadFileResponse'
|
||||
);
|
||||
|
||||
return $obj instanceof UploadFileResponse ? $obj : null;
|
||||
return isset($responseData) ? $responseData : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
namespace RetailCrm\Mg\Bot\Model\Response;
|
||||
|
||||
use JMS\Serializer\Annotation\Exclude;
|
||||
use JMS\Serializer\Annotation\Accessor;
|
||||
use JMS\Serializer\Annotation\SkipWhenEmpty;
|
||||
use JMS\Serializer\Annotation\Type;
|
||||
@ -38,6 +39,12 @@ trait CommonFields
|
||||
*/
|
||||
private $errors;
|
||||
|
||||
/**
|
||||
* @var int $statusCode
|
||||
* @Exclude()
|
||||
*/
|
||||
private $statusCode;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@ -54,6 +61,22 @@ trait CommonFields
|
||||
$this->errors = $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function setStatusCode(int $statusCode): void
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
|
@ -26,7 +26,8 @@ use RetailCrm\Common\Serializer;
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
class ListResponse implements \Iterator, \ArrayAccess, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@ -42,18 +43,26 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
*/
|
||||
private $position = 0;
|
||||
|
||||
/**
|
||||
* @var int $statusCode
|
||||
*/
|
||||
private $statusCode;
|
||||
|
||||
/**
|
||||
* ListResponse constructor.
|
||||
*
|
||||
* @param string $responseType
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function __construct($responseType, $data)
|
||||
public function __construct($responseType, $data, $statusCode)
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
|
||||
if (isset($data['errors'])) {
|
||||
$this->errors = $data['errors'];
|
||||
} else {
|
||||
foreach($data as $item) {
|
||||
foreach ($data as $item) {
|
||||
$this->items[] =
|
||||
Serializer::deserialize($item, $responseType, Serializer::S_ARRAY);
|
||||
}
|
||||
@ -160,7 +169,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
* Implements rewind() for Iterable
|
||||
* @internal
|
||||
*/
|
||||
public function rewind() {
|
||||
public function rewind()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
@ -170,7 +180,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
* @internal
|
||||
* @return mixed
|
||||
*/
|
||||
public function current() {
|
||||
public function current()
|
||||
{
|
||||
return $this->items[$this->position];
|
||||
}
|
||||
|
||||
@ -180,7 +191,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
* @internal
|
||||
* @return int|mixed
|
||||
*/
|
||||
public function key() {
|
||||
public function key()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
@ -188,7 +200,8 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
* Implements next() for Iterable
|
||||
* @internal
|
||||
*/
|
||||
public function next() {
|
||||
public function next()
|
||||
{
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
@ -198,7 +211,24 @@ class ListResponse implements \Iterator, \ArrayAccess, \Countable {
|
||||
* @internal
|
||||
* @return bool
|
||||
*/
|
||||
public function valid() {
|
||||
public function valid()
|
||||
{
|
||||
return isset($this->items[$this->position]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode(): int
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $statusCode
|
||||
*/
|
||||
public function setStatusCode(int $statusCode): void
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user