Skip to content

Commit

Permalink
v4 initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Gladyshev committed Sep 14, 2020
1 parent c42f859 commit 2026a3c
Show file tree
Hide file tree
Showing 65 changed files with 945 additions and 2,133 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
vendor/
build/
composer.lock
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4.0.0-beta
- DI ready
- Improve PSRs support


3.1.2 - Businesses service added
- SmartCampaigns support added
- SmartBanners support added
Expand Down
37 changes: 20 additions & 17 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
MIT License
This is free and unencumbered software released into the public domain.

Copyright (c) 2016 Dmitry Gladyshev
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Empty file removed build/.gitkeep
Empty file.
23 changes: 11 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
"keywords": ["yandex", "direct", "yandex.direct", "client", "api"],
"description": "Useful Yandex.Direct API client.",
"type": "library",
"license": "MIT",
"license": "UNLICENSE",
"authors": [
{
"name": "Dmitry Gladyshev",
"email": "[email protected]",
"homepage": "https://github.com/gladyshev"
}
],
"minimum-stability": "stable",
"require": {
"php": "^5.6||^7.0",
"php": "^7.2",
"ext-mbstring": "*",
"ext-libxml": "*",
"ext-json":"*",
"ext-dom":"*",
"psr/log": "~1.0",
"guzzlehttp/guzzle": "~6.2",
"psr/http-client": "*",
"psr/http-message": "*",
"guzzlehttp/psr7": "~1.6",
"openlss/lib-array2xml": "~0.5"
},
"require-dev": {
"phpunit/phpunit": "~5.7|~6.0",
"squizlabs/php_codesniffer": "^2.3"
"phpunit/phpunit": "~8.0.0",
"squizlabs/php_codesniffer": "~2.9.0",
"guzzlehttp/guzzle": "~7.0.0",
"monolog/monolog": "~2.1.1"
},
"autoload": {
"psr-4": {
"Yandex\\Direct\\": "src/"
"Gladyshev\\Yandex\\Direct\\": "src/"
},
"files": [
"src/functions.php"
Expand All @@ -38,11 +42,6 @@
"Yandex\\Direct\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.5-dev"
}
},
"scripts": {
"test": "phpunit",
"cs-check": "phpcs",
Expand Down
10 changes: 8 additions & 2 deletions examples/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

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

use Yandex\Direct\Client;
$credentials = \Gladyshev\Yandex\Direct\Credentials::buildForSandbox(
getenv('_LOGIN_'),
getenv('_TOKEN_'),
getenv('_MASTER_TOKEN_')
);

$httpClient = new \GuzzleHttp\Client;

$client = new Client(getenv('_LOGIN_'), getenv('_TOKEN_'));
$client = new \Gladyshev\Yandex\Direct\Client($credentials, $httpClient);

$report = $client->reports->get(
/* Selection criteria */
Expand Down
25 changes: 11 additions & 14 deletions examples/sandbox.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?php
/**
* @author Dmitry Gladyshev <[email protected]>
* @created 10.12.16 16:10
*/

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

use Yandex\Direct\Client;
use Yandex\Direct\Credentials;
use Yandex\Direct\Logger\EchoLog;
use Yandex\Direct\Transport\Json\Transport;
use Gladyshev\Yandex\Direct\Client;
use Gladyshev\Yandex\Direct\Credentials;

$credentials = new Credentials(getenv('_LOGIN_'), getenv('_TOKEN_'));

$transport = new Transport([
'baseUrl' => 'https://api-sandbox.direct.yandex.com',
'logger' => new EchoLog
]);
$credentials = new Credentials(
getenv('_LOGIN_'),
getenv('_TOKEN_'),
'',
true,
Credentials::LANGUAGE_RU,
'https://api-sandbox.direct.yandex.com'
);

$client = new Client($credentials, $transport);
$client = new Client($credentials, new \GuzzleHttp\Client);

$resp = $client->campaigns->get(['Ids' => [123456, 654321]], ['Funds']);

Expand Down
29 changes: 21 additions & 8 deletions examples/simple.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<?php
/**
* @author Dmitry Gladyshev <[email protected]>
* @created 07.11.16 12:38
*/

use Gladyshev\Yandex\Direct\Exception\ErrorResponseException;
use function GuzzleHttp\Psr7\str;

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

use Yandex\Direct\Client;
$credentials = \Gladyshev\Yandex\Direct\Credentials::buildForSandbox(
getenv('_LOGIN_'),
getenv('_TOKEN_'),
getenv('_MASTER_TOKEN_')
);

$client = new Client(getenv('_LOGIN_'), getenv('_TOKEN_'));
$client = new \Gladyshev\Yandex\Direct\Client(
$credentials,
new GuzzleHttp\Client()
);

// just...
$resp = $client->campaigns->get(['Types' => ['TEXT_CAMPAIGN']], ['Funds']);
//$resp = $client->clients->get([
// "AccountQuality" ,"Archived", "ClientId", "ClientInfo", "CountryId", "CreatedAt", "Currency", "Grants", "Login", "Notification", "OverdraftSumAvailable", "Phone", "Representatives", "Restrictions", "Settings", "Type", "VatRate"
//]);
try {
$resp = $client->campaigns->get(['Types' => ['TEXT_CAMPAIGN']], ['Funds']);
print_r($resp);
} catch (ErrorResponseException $e) {
echo $e;
}

print_r($resp);
129 changes: 129 additions & 0 deletions src/AbstractService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);

namespace Gladyshev\Yandex\Direct;

abstract class AbstractService implements \Gladyshev\Yandex\Direct\ServiceInterface
{
/**
* @var \Gladyshev\Yandex\Direct\CredentialsInterface
*/
protected $credentials;

/**
* @var \Psr\Http\Client\ClientInterface
*/
protected $httpClient;

/**
* @var bool
*/
protected $throwExceptionOnError = true;

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

public function getName(): string
{
return (new \ReflectionClass(static::class))->getShortName();
}

/**
* @param array|null $params
* @return array
* @throws \Throwable
*/
public function call(array $params = [])
{
if (empty($params)) {
$params = new \StdClass;
}

$request = new \GuzzleHttp\Psr7\Request(
'POST',
$this->getUri(),
$this->getHeaders(),
$this->prepareBody($params)
);

$response = $this->httpClient->sendRequest($request);

return $this->handleResponse($response, $request);
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\RequestInterface $request
* @return array
* @throws \Throwable
*/
protected function handleResponse(
\Psr\Http\Message\ResponseInterface $response,
\Psr\Http\Message\RequestInterface $request
): array {
if ($response->getStatusCode() != 200) {
throw new \Gladyshev\Yandex\Direct\Exception\InvalidResponseException(
$request,
$response,
'Unexpected response.'
);
}

$result = json_decode($response->getBody()->getContents(), true);

if (isset($result['error']) && $result['error']) {
throw new \Gladyshev\Yandex\Direct\Exception\ErrorResponseException(
$result['error']['error_string'],
$result['error']['error_detail'],
$result['error']['error_code'],
$request,
$response
);
}

$result['request_id'] = current($response->getHeader('RequestId'));

if ($response->getHeader('Units')) {
[$debit, $rest, $limit] = explode('/', current($response->getHeader('Units')));
$result['units'] = [
'debit' => $debit ?? null,
'limit' => $limit ?? null,
'rest' => $rest?? null
];

$request['unitsUsedLogin'] = current($response->getHeader('Units-Used-Login'));
}

return $result;
}

protected function getHeaders(): array
{
return [
'Content-Type' => 'application/json; charset=utf-8',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->credentials->getToken(),
'Client-Login' => $this->credentials->getLogin(),
'Accept-Language' => $this->credentials->getLanguage(),
'Use-Operator-Units' => $this->credentials->getUseOperatorUnits() ? 'true' : 'false'
];
}

protected function prepareBody(array $params): string
{
return json_encode(
$params,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT
);
}

public function getUri(): string
{
return $this->credentials->getBaseUrl() . '/json/v5/' . mb_strtolower($this->getName());
}
}
Loading

0 comments on commit 2026a3c

Please sign in to comment.