Skip to content

Commit

Permalink
Обеспечен проброс дополнительных параметров для задания настроек Guzz…
Browse files Browse the repository at this point in the history
…le-клиента.
  • Loading branch information
S.E.M committed Mar 26, 2018
1 parent f290233 commit 6454cbb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
13 changes: 10 additions & 3 deletions Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ class Api
* @var ClientInterface
*/
private $client;


/**
* @var array
*/
private $config = [];

/**
* @param AuthInterface $auth
* @param array $config additional config setting for client service
*/
public function __construct(AuthInterface $auth)
public function __construct(AuthInterface $auth, $config = [])
{
$this->auth = $auth;
$this->auth->setContext($this);
$this->config = $config;
}

/**
Expand Down Expand Up @@ -280,7 +287,7 @@ public function request($method, $params = [])
public function getClient()
{
if ($this->client === null) {
$this->client = new Client();
$this->client = new Client($this->config);
}

return $this->client;
Expand Down
19 changes: 18 additions & 1 deletion Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@

class Client implements ClientInterface
{
/**
* Additional configuration for Guzzle Client.
* For example @link http://docs.guzzlephp.org/en/stable/request-options.html#verify
* @var array
*/
protected $config = [];

/**
* @var string
*/
private $baseUrl = 'https://sms.ru/{method}';

/**
* @param array $config
*/
public function __construct($config = [])
{
$this->config = $config;
}

/**
* @param string $method
* @param array $params
Expand All @@ -24,7 +38,10 @@ public function request($method, $params = [])
{
$client = new \GuzzleHttp\Client();

$response = $client->post($this->getUrl($method), ['query' => $params]);
// Merge with specific config
$params = array_merge($this->config, ['query' => $params]);

$response = $client->post($this->getUrl($method), $params);

if ($response->getStatusCode() === 200) {
return (string)$response->getBody();
Expand Down
6 changes: 6 additions & 0 deletions Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
interface ClientInterface
{

/**
* Provides an additional configs for client service
* @param array $config
*/
public function __construct($config = []);

/**
* @param string $method
* @param array $params
Expand Down

0 comments on commit 6454cbb

Please sign in to comment.