Skip to content

Commit

Permalink
Update guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Gladyshev committed Dec 14, 2024
1 parent 7e88d31 commit f21c475
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 169 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
vendor/
build/
runtime/
cache/
composer.lock
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.2",
"php": "^7.4|^8.0",
"ext-mbstring": "*",
"ext-json":"*",
"psr/http-client": "*",
"psr/http-message": "*",
"guzzlehttp/psr7": "~1.7.0"
"psr/http-client": "1.0.3",
"guzzlehttp/psr7": "2.7.0"
},
"require-dev": {
"phpunit/phpunit": "~8.5.13",
"squizlabs/php_codesniffer": "~3.5.8",
"guzzlehttp/guzzle": "~7.2.0",
"psr/log": "~1.0"
"phpunit/phpunit": "8.5.41",
"squizlabs/php_codesniffer": "3.5.8",
"vimeo/psalm": "3.18.2",
"guzzlehttp/guzzle": "7.9.0",
"psr/log": "1.1"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions examples/regions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require '../vendor/autoload.php';

$credentials = \Gladyshev\Yandex\Direct\Credentials::clientSandbox(
getenv('_TOKEN_')
);

$client = new \Gladyshev\Yandex\Direct\Client(
$credentials,
new GuzzleHttp\Client()
);

$resp = $client->dictionaries->get(['GeoRegions']);

print_r($resp);
59 changes: 19 additions & 40 deletions src/AbstractService.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
<?php

declare(strict_types=1);

namespace Gladyshev\Yandex\Direct;

use Gladyshev\Yandex\Direct\Exception\ErrorResponseException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use StdClass;

abstract class AbstractService implements ServiceInterface
{
/**
* @var string
*/
private $serviceName;

/**
* @var \Gladyshev\Yandex\Direct\CredentialsInterface
*/
private $credentials;

/**
* @var \Psr\Http\Client\ClientInterface
*/
private $httpClient;
private string $serviceName;
private CredentialsInterface $credentials;
private ClientInterface $httpClient;

public function __construct(
string $serviceName,
\Gladyshev\Yandex\Direct\CredentialsInterface $credentials,
\Psr\Http\Client\ClientInterface $httpClient
CredentialsInterface $credentials,
ClientInterface $httpClient
) {
$this->serviceName = $serviceName;
$this->credentials = $credentials;
Expand All @@ -33,7 +27,7 @@ public function __construct(

public function call(array $params = []): array
{
$request = new \GuzzleHttp\Psr7\Request(
$request = new Request(
'POST',
$this->getUri(),
$this->getHeaders(),
Expand All @@ -50,22 +44,16 @@ protected function getServiceName(): string
return $this->serviceName;
}

protected function getCredentials(): \Gladyshev\Yandex\Direct\CredentialsInterface
protected function getCredentials(): CredentialsInterface
{
return $this->credentials;
}

/**
* @return string
*/
protected function getUri(): string
{
return $this->getCredentials()->getBaseUrl() . '/json/v5/' . mb_strtolower($this->getServiceName());
}

/**
* @return array
*/
protected function getHeaders(): array
{
$headers = [
Expand All @@ -85,35 +73,26 @@ protected function getHeaders(): array
return $headers;
}

/**
* @param array $params
* @return string
*/
protected function getBody(array $params): string
{
if (empty($params['params'])) {
$params = new \StdClass();
$params = new StdClass();
} else {
$params['params'] = array_filter($params['params']);
}

return json_encode($params);
}

/**
* @param \Psr\Http\Message\RequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @return array
*/
protected function handleResponse(
\Psr\Http\Message\RequestInterface $request,
\Psr\Http\Message\ResponseInterface $response
RequestInterface $request,
ResponseInterface $response
): array {
$contents = $response->getBody()->getContents();
$parsedBody = json_decode($contents, true);

if (!is_array($parsedBody)) {
throw new \Gladyshev\Yandex\Direct\Exception\ErrorResponseException(
throw new ErrorResponseException(
'Unexpected API response.',
$contents,
0,
Expand All @@ -123,7 +102,7 @@ protected function handleResponse(
}

if (!empty($parsedBody['error'])) {
throw new \Gladyshev\Yandex\Direct\Exception\ErrorResponseException(
throw new ErrorResponseException(
$parsedBody['error']['error_string'],
$parsedBody['error']['error_detail'],
(int) $parsedBody['error']['error_code'],
Expand Down
97 changes: 55 additions & 42 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,90 @@
<?php

declare(strict_types=1);

namespace Gladyshev\Yandex\Direct;

use Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException;
use Gladyshev\Yandex\Direct\Service\AdExtensions;
use Gladyshev\Yandex\Direct\Service\AdGroups;
use Gladyshev\Yandex\Direct\Service\AdImages;
use Gladyshev\Yandex\Direct\Service\Ads;
use Gladyshev\Yandex\Direct\Service\AgencyClients;
use Gladyshev\Yandex\Direct\Service\AudienceTargets;
use Gladyshev\Yandex\Direct\Service\BidModifiers;
use Gladyshev\Yandex\Direct\Service\Bids;
use Gladyshev\Yandex\Direct\Service\Campaigns;
use Gladyshev\Yandex\Direct\Service\Changes;
use Gladyshev\Yandex\Direct\Service\Clients;
use Gladyshev\Yandex\Direct\Service\Dictionaries;
use Gladyshev\Yandex\Direct\Service\DynamicTextAdTargets;
use Gladyshev\Yandex\Direct\Service\KeywordBids;
use Gladyshev\Yandex\Direct\Service\Keywords;
use Gladyshev\Yandex\Direct\Service\KeywordsResearch;
use Gladyshev\Yandex\Direct\Service\Reports;
use Gladyshev\Yandex\Direct\Service\RetargetingLists;
use Gladyshev\Yandex\Direct\Service\Sitelinks;
use Gladyshev\Yandex\Direct\Service\TurboPages;
use Gladyshev\Yandex\Direct\Service\VCards;
use Psr\Http\Client\ClientInterface;

/**
* Yandex.Direct v5 API client implementation
*
* @property \Gladyshev\Yandex\Direct\Service\AdExtensions $adExtensions
* @property \Gladyshev\Yandex\Direct\Service\AdGroups $adGroups
* @property \Gladyshev\Yandex\Direct\Service\AdImages $adImages
* @property \Gladyshev\Yandex\Direct\Service\Ads $ads
* @property \Gladyshev\Yandex\Direct\Service\AgencyClients $agencyClients
* @property \Gladyshev\Yandex\Direct\Service\AudienceTargets $audienceTargets
* @property \Gladyshev\Yandex\Direct\Service\BidModifiers $bidModifiers
* @property \Gladyshev\Yandex\Direct\Service\Bids $bids
* @property \Gladyshev\Yandex\Direct\Service\Campaigns $campaigns
* @property \Gladyshev\Yandex\Direct\Service\Changes $changes
* @property \Gladyshev\Yandex\Direct\Service\Clients $clients
* @property \Gladyshev\Yandex\Direct\Service\Dictionaries $dictionaries
* @property \Gladyshev\Yandex\Direct\Service\DynamicTextAdTargets $dynamicTextAdTargets
* @property \Gladyshev\Yandex\Direct\Service\KeywordBids $keywordBids
* @property \Gladyshev\Yandex\Direct\Service\Keywords $keywords
* @property \Gladyshev\Yandex\Direct\Service\KeywordsResearch $keywordsResearch
* @property \Gladyshev\Yandex\Direct\Service\Reports $reports
* @property \Gladyshev\Yandex\Direct\Service\RetargetingLists $retargetingLists
* @property \Gladyshev\Yandex\Direct\Service\Sitelinks $sitelinks
* @property \Gladyshev\Yandex\Direct\Service\TurboPages $turboPages
* @property \Gladyshev\Yandex\Direct\Service\VCards $vCards
* @property AdExtensions $adExtensions
* @property AdGroups $adGroups
* @property AdImages $adImages
* @property Ads $ads
* @property AgencyClients $agencyClients
* @property AudienceTargets $audienceTargets
* @property BidModifiers $bidModifiers
* @property Bids $bids
* @property Campaigns $campaigns
* @property Changes $changes
* @property Clients $clients
* @property Dictionaries $dictionaries
* @property DynamicTextAdTargets $dynamicTextAdTargets
* @property KeywordBids $keywordBids
* @property Keywords $keywords
* @property KeywordsResearch $keywordsResearch
* @property Reports $reports
* @property RetargetingLists $retargetingLists
* @property Sitelinks $sitelinks
* @property TurboPages $turboPages
* @property VCards $vCards
*/
class Client implements ServiceFactoryInterface
{
private const SERVICE_NAMESPACE = __NAMESPACE__ . '\\Service\\';

/**
* @var \Gladyshev\Yandex\Direct\ServiceInterface[]
* @var ServiceInterface[]
*/
private $services = [];
private array $services = [];

/**
* @var \Gladyshev\Yandex\Direct\CredentialsInterface
*/
private $credentials;

/**
* @var \Psr\Http\Client\ClientInterface
*/
private $httpClient;
private CredentialsInterface $credentials;
private ClientInterface $httpClient;

public function __construct(
CredentialsInterface $credentials,
\Psr\Http\Client\ClientInterface $httpClient
ClientInterface $httpClient
) {
$this->credentials = $credentials;
$this->httpClient = $httpClient;
}

public function createService(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface
public function createService(string $serviceName): ServiceInterface
{
if (!isset($this->services[$serviceName])) {
$className = self::SERVICE_NAMESPACE . ucfirst($serviceName);

if (!class_exists($className)) {
throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException(
throw new ServiceNotFoundException(
$serviceName,
"Class '{$className}' is not found."
);
}

$classInstance = new $className($serviceName, $this->credentials, $this->httpClient);

if (!$classInstance instanceof \Gladyshev\Yandex\Direct\ServiceInterface) {
throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException(
if (!$classInstance instanceof ServiceInterface) {
throw new ServiceNotFoundException(
$serviceName,
"Class '{$className}' must be an instance of '\Gladyshev\Yandex\Direct\ServiceInterface'."
);
Expand All @@ -83,7 +96,7 @@ public function createService(string $serviceName): \Gladyshev\Yandex\Direct\Ser
return $this->services[$serviceName];
}

public function __get(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface
public function __get(string $serviceName): ServiceInterface
{
return $this->createService($serviceName);
}
Expand Down
Loading

0 comments on commit f21c475

Please sign in to comment.