cleanup
This commit is contained in:
parent
997cc48acf
commit
38fa980038
20
README.md
20
README.md
@ -4,9 +4,9 @@
|
||||
[![PHP from Packagist](https://img.shields.io/packagist/php-v/retailcrm/mg-bot-api-client-php.svg?style=flat-square)](https://packagist.org/packages/retailcrm/mg-bot-api-client-php)
|
||||
|
||||
|
||||
# retailCRM API PHP client
|
||||
# Message Gateway Bot API PHP client
|
||||
|
||||
This is php retailCRM MG Bot API client.
|
||||
This is php library for retailCRM MG Bot API.
|
||||
|
||||
## Requirements
|
||||
|
||||
@ -32,6 +32,9 @@ require 'path/to/vendor/autoload.php';
|
||||
|
||||
### Assign dialog
|
||||
```php
|
||||
<?php
|
||||
|
||||
use RetailCrm\Common\Exception;
|
||||
use RetailCrm\Mg\Bot\Client;
|
||||
use RetailCrm\Mg\Bot\Model\Request\DialogAssignRequest;
|
||||
|
||||
@ -42,20 +45,13 @@ try {
|
||||
$request->setDialogId(60);
|
||||
$request->setUserId(4);
|
||||
|
||||
/* @var \RetailCrm\Mg\Bot\Model\Response\AssignResponse $response */
|
||||
$response = $client->dialogAssign($request);
|
||||
} catch (\RetailCrm\Common\Exception\CurlException $exception) {
|
||||
echo $exception->getMessage();
|
||||
} catch (\RetailCrm\Common\Exception\LimitException $exception) {
|
||||
echo $exception->getMessage();
|
||||
} catch (\InvalidArgumentException $exception) {
|
||||
echo $exception->getMessage();
|
||||
} catch (\Exception $exception) {
|
||||
} catch (Exception\LimitException | Exception\InvalidJsonException | Exception\UnauthorizedException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
$response->getPreviousResponsible();
|
||||
}
|
||||
echo $response->getPreviousResponsible();
|
||||
```
|
||||
|
||||
### Documentation
|
||||
|
@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* Response adapter
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot\Adapter
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Mg\Bot\Adapter;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RetailCrm\Mg\Bot\Model\Response;
|
||||
|
||||
/**
|
||||
* Class ResponseAdapter
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class ResponseAdapter
|
||||
{
|
||||
/**
|
||||
* @var \RetailCrm\Mg\Bot\Model\Response $model
|
||||
*/
|
||||
private $model;
|
||||
/**
|
||||
* @var \Psr\Http\Message\ResponseInterface $interface
|
||||
*/
|
||||
private $interface;
|
||||
|
||||
/**
|
||||
* ResponseAdapter constructor.
|
||||
*
|
||||
* @param \Psr\Http\Message\ResponseInterface $interface
|
||||
*/
|
||||
public function __construct(ResponseInterface $interface)
|
||||
{
|
||||
$this->interface = $interface;
|
||||
$this->model = new Response();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build response
|
||||
*
|
||||
* @return \RetailCrm\Mg\Bot\Model\Response
|
||||
*/
|
||||
public function build(): Response
|
||||
{
|
||||
$this->model->setStatus($this->interface->getStatusCode());
|
||||
$this->model->setBody((string) $this->interface->getBody());
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ namespace RetailCrm\Mg\Bot;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RetailCrm\Common\Url;
|
||||
use RetailCrm\Common\Serializer;
|
||||
use RetailCrm\Mg\Bot\Adapter\ModelAdapter;
|
||||
use RetailCrm\Mg\Bot\Model\ModelAdapter;
|
||||
use RetailCrm\Mg\Bot\Model\Entity\Bot;
|
||||
use RetailCrm\Mg\Bot\Model\Entity\Channel\Channel;
|
||||
use RetailCrm\Mg\Bot\Model\Entity\Chat\Chat;
|
||||
@ -58,7 +58,7 @@ class Client
|
||||
*
|
||||
* @param string $url MG API URL
|
||||
* @param string $token MG API Key
|
||||
* @param bool $debug Enable or disable debug mode - will log all requests to STDOUT (default: false)
|
||||
* @param bool $debug Enable or disable debug mode (default: false)
|
||||
* @param \GuzzleHttp\HandlerStack $handler GuzzleHttp::HandlerStack instance (default: null)
|
||||
*/
|
||||
public function __construct($url, $token, $debug = false, $handler = null)
|
||||
|
@ -23,7 +23,6 @@ use RetailCrm\Common\Exception\NotFoundException;
|
||||
use RetailCrm\Common\Exception\UnauthorizedException;
|
||||
use RetailCrm\Common\Serializer;
|
||||
use RetailCrm\Common\Url;
|
||||
use RetailCrm\Mg\Bot\Adapter\ResponseAdapter;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use GuzzleHttp\Client;
|
||||
@ -112,7 +111,7 @@ class HttpClient
|
||||
* @param string $method Request method (default: 'GET')
|
||||
* @param mixed $request Request model (default: null)
|
||||
*
|
||||
* @return \RetailCrm\Mg\Bot\Model\Response
|
||||
* @return \Psr\Http\Message\ResponseInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function makeRequest($path, $method, $request = null)
|
||||
@ -139,7 +138,11 @@ class HttpClient
|
||||
]
|
||||
);
|
||||
|
||||
if (in_array($method, [self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE]) && is_string($requestBody)) {
|
||||
if (in_array(
|
||||
$method,
|
||||
[self::METHOD_POST, self::METHOD_PUT, self::METHOD_PATCH, self::METHOD_DELETE]
|
||||
) && is_string($requestBody)
|
||||
) {
|
||||
$request = $request->withBody(stream_for($requestBody));
|
||||
}
|
||||
|
||||
@ -161,9 +164,7 @@ class HttpClient
|
||||
|
||||
$this->validateResponse($responseObject);
|
||||
|
||||
$adapter = new ResponseAdapter($responseObject);
|
||||
|
||||
return $adapter->build();
|
||||
return $responseObject;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -248,6 +249,7 @@ class HttpClient
|
||||
* @param \Psr\Http\Message\ResponseInterface $responseObject
|
||||
*
|
||||
* @throws \ErrorException
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function validateResponse(ResponseInterface $responseObject)
|
||||
{
|
||||
@ -257,10 +259,6 @@ class HttpClient
|
||||
$errorMessage = !empty($response['errorMsg']) ? $response['errorMsg'] : '';
|
||||
$errorMessage = !empty($response['errors']) ? $this->getErrors($response['errors']) : $errorMessage;
|
||||
|
||||
/**
|
||||
* responses with 400 & 460 http codes contains extended error data
|
||||
* therefore they are not handled as exceptions
|
||||
*/
|
||||
if ($statusCode == 400) {
|
||||
throw new RuntimeException($errorMessage);
|
||||
}
|
||||
@ -284,6 +282,10 @@ class HttpClient
|
||||
if ($statusCode == 503) {
|
||||
throw new LimitException($errorMessage);
|
||||
}
|
||||
|
||||
if ($statusCode >= 400) {
|
||||
throw new Exception($errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,11 +11,11 @@
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Mg\Bot\Adapter;
|
||||
namespace RetailCrm\Mg\Bot\Model;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RetailCrm\Common\Exception\InvalidJsonException;
|
||||
use RetailCrm\Common\Serializer;
|
||||
use RetailCrm\Mg\Bot\Model\Response;
|
||||
|
||||
/**
|
||||
* Class ModelAdapter
|
||||
@ -46,25 +46,25 @@ class ModelAdapter
|
||||
/**
|
||||
* Get Response Model
|
||||
*
|
||||
* @param \RetailCrm\Mg\Bot\Model\Response $response
|
||||
* @param \Psr\Http\Message\ResponseInterface $response
|
||||
*
|
||||
* @return \RetailCrm\Mg\Bot\Model\ModelInterface
|
||||
*/
|
||||
public function getResponseModel(Response $response)
|
||||
public function getResponseModel(ResponseInterface $response)
|
||||
{
|
||||
return Serializer::deserialize($response->getBody(), $this->classname);
|
||||
return Serializer::deserialize((string)$response->getBody(), $this->classname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Response List
|
||||
*
|
||||
* @param \RetailCrm\Mg\Bot\Model\Response $response
|
||||
* @param \Psr\Http\Message\ResponseInterface $response
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseList(Response $response)
|
||||
public function getResponseList(ResponseInterface $response)
|
||||
{
|
||||
$array = json_decode($response->getBody(), true);
|
||||
$array = json_decode((string)$response->getBody(), true);
|
||||
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
throw new InvalidJsonException('Received invalid JSON', 1);
|
@ -1,57 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* ModelInterface
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot\Model
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Mg\Bot\Model;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
*
|
||||
* @package RetailCrm\Mg\Bot\Model
|
||||
*/
|
||||
class Response
|
||||
{
|
||||
private $status;
|
||||
|
||||
private $body;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStatus()
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $status
|
||||
*/
|
||||
public function setStatus($status)
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $body
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->body = $body;
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 7.1
|
||||
*
|
||||
* CurlException
|
||||
*
|
||||
* @package RetailCrm\Common\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
|
||||
namespace RetailCrm\Common\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class CurlException
|
||||
*
|
||||
* @package RetailCrm\Common\Exception
|
||||
* @author retailCRM <integration@retailcrm.ru>
|
||||
* @license https://opensource.org/licenses/MIT MIT License
|
||||
* @link http://help.retailcrm.pro/docs/Developers
|
||||
*/
|
||||
class CurlException extends RuntimeException implements Throwable
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue
Block a user