Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from SerafimArts/master
Browse files Browse the repository at this point in the history
A little improvements
  • Loading branch information
Андрей Соснов authored Feb 6, 2017
2 parents 692ec91 + b7cec3d commit 2648ee6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,10 +23,10 @@
*
* @package ATehnix\VkClient
*/
class Client
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;

/**
Expand Down Expand Up @@ -62,7 +63,16 @@ class Client
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,
Expand Down Expand Up @@ -98,6 +108,7 @@ public function setPassError($bool = true)
/**
* @param RequestInterface $request
* @return array
* @throws \ATehnix\VkClient\Exceptions\VkException
*/
public function send(RequestInterface $request)
{
Expand All @@ -113,8 +124,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);
Expand Down Expand Up @@ -156,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']);
}
Expand Down Expand Up @@ -191,6 +211,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);
Expand Down
38 changes: 38 additions & 0 deletions src/Contracts/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of VkClient package.
*
* @author ATehnix <atehnix@gmail.com>
*
* 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);
}

0 comments on commit 2648ee6

Please sign in to comment.