From 9fd25b684fae8f92f29a071eac5c2801e368060e Mon Sep 17 00:00:00 2001 From: Nesmeyanov Kirill Date: Mon, 6 Feb 2017 19:15:27 +0300 Subject: [PATCH 1/4] A little improvements --- src/Client.php | 12 ++++++++-- src/Contracts/ClientInterface.php | 38 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/Contracts/ClientInterface.php diff --git a/src/Client.php b/src/Client.php index ff120f2..07f721d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -10,6 +10,7 @@ namespace ATehnix\VkClient; +use ATehnix\VkClient\Contracts\ClientInterface as ClientContract; use ATehnix\VkClient\Contracts\RequestInterface; use ATehnix\VkClient\Exceptions\VkException; use GuzzleHttp\Client as HttpClient; @@ -22,7 +23,7 @@ * * @package ATehnix\VkClient */ -class Client +class Client implements ClientContract { const API_URI = 'https://api.vk.com/method/'; const API_VERSION = '5.53'; @@ -98,6 +99,7 @@ public function setPassError($bool = true) /** * @param RequestInterface $request * @return array + * @throws \ATehnix\VkClient\Exceptions\VkException */ public function send(RequestInterface $request) { @@ -113,8 +115,9 @@ public function send(RequestInterface $request) * @param array $parameters * @param string|null $token * @return array + * @throws \ATehnix\VkClient\Exceptions\VkException */ - public function request($method, $parameters, $token = null) + public function request($method, $parameters = [], $token = null) { $options = $this->buildOptions($parameters, $token); $response = $this->http->request('POST', $method, $options); @@ -148,6 +151,10 @@ protected function getResponseData(ResponseInterface $response) { $data = json_decode((string)$response->getBody(), true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new VkException('Invalid VK response format: ' . json_last_error_msg()); + } + if (!$this->passError) { $this->checkErrors($data); } @@ -191,6 +198,7 @@ public static function toException($error) 15 => Exceptions\AccessDeniedVkException::class, ]; + /** @var \Exception|\Throwable $exception */ $exception = isset($map[$code]) ? $map[$code] : $map[0]; return new $exception($message, $code); diff --git a/src/Contracts/ClientInterface.php b/src/Contracts/ClientInterface.php new file mode 100644 index 0000000..506b8ad --- /dev/null +++ b/src/Contracts/ClientInterface.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ATehnix\VkClient\Contracts; + +use ATehnix\VkClient\Contracts\RequestInterface; +use ATehnix\VkClient\Exceptions\VkException; + +/** + * Interface ClientInterface + * + * @package ATehnix\VkClient\Contracts + */ +interface ClientInterface +{ + /** + * @param RequestInterface $request + * @return array + * @throws VkException + */ + public function send(RequestInterface $request); + + /** + * @param string $method + * @param array $parameters + * @param string|null $token + * @return array + * @throws VkException + */ + public function request($method, $parameters = [], $token = null); +} From 8955b7515042d517bb107f9aeceb9413bbd37d12 Mon Sep 17 00:00:00 2001 From: Nesmeyanov Kirill Date: Mon, 6 Feb 2017 19:19:11 +0300 Subject: [PATCH 2/4] Improve constructor --- src/Client.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 07f721d..f04a5cd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -63,7 +63,16 @@ class Client implements ClientContract public function __construct($version = null, ClientInterface $http = null) { $this->version = $version ?: static::API_VERSION; - $this->http = $http ?: new HttpClient([ + $this->http = $this->resolveHttpClient($http); + } + + /** + * @param ClientInterface|null $http + * @return ClientInterface + */ + private function resolveHttpClient(ClientInterface $http = null) + { + return $http ?: new HttpClient([ 'base_uri' => static::API_URI, 'timeout' => static::API_TIMEOUT, 'http_errors' => false, From 03a9af212bcae44508d1b2a6796efa94e723095b Mon Sep 17 00:00:00 2001 From: Nesmeyanov Kirill Date: Mon, 6 Feb 2017 19:20:12 +0300 Subject: [PATCH 3/4] Increment API version --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index f04a5cd..9152665 100644 --- a/src/Client.php +++ b/src/Client.php @@ -26,7 +26,7 @@ class Client implements ClientContract { const API_URI = 'https://api.vk.com/method/'; - const API_VERSION = '5.53'; + const API_VERSION = '5.62'; const API_TIMEOUT = 30.0; /** From b7cec3d7f255cac9591a8cb002775290b6e4c150 Mon Sep 17 00:00:00 2001 From: Nesmeyanov Kirill Date: Mon, 6 Feb 2017 19:26:30 +0300 Subject: [PATCH 4/4] Fix json exceptions --- src/Client.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9152665..3d080ef 100644 --- a/src/Client.php +++ b/src/Client.php @@ -160,10 +160,6 @@ protected function getResponseData(ResponseInterface $response) { $data = json_decode((string)$response->getBody(), true); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new VkException('Invalid VK response format: ' . json_last_error_msg()); - } - if (!$this->passError) { $this->checkErrors($data); } @@ -172,11 +168,19 @@ protected function getResponseData(ResponseInterface $response) } /** - * @param $data + * @param array|false $data * @throws VkException */ protected function checkErrors($data) { + if (json_last_error() !== JSON_ERROR_NONE) { + throw new VkException('Invalid VK response format: ' . json_last_error_msg()); + } + + if (!is_array($data)) { + throw new VkException('Invalid response format'); + } + if (isset($data['error'])) { throw self::toException($data['error']); }