Skip to content

Commit

Permalink
Version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
a.pushkarev committed May 29, 2022
1 parent 1004b75 commit 761505d
Show file tree
Hide file tree
Showing 21 changed files with 436 additions and 598 deletions.
22 changes: 18 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
.idea/
vendor/
examples/config.php
# Logs
logs
*.log

.env
composer.phar
composer.lock
composer.lock
vendor

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Alexander Pushkarev
Copyright (c) 2022 Alexander Pushkarev

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
Expand All @@ -17,4 +17,4 @@ 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.
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
241 changes: 134 additions & 107 deletions README.md

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "axp-dev/ya-metrika",
"description": "Yandex Metrika API Library",
"homepage": "https://github.com/axp-dev/ya-metrika",
"keywords": ["yandex","metrika","api"],
"version": "1.1.0",
"keywords": ["yandex", "metrika", "api"],
"version": "2.0.0",
"license": "MIT",
"authors": [
{
Expand All @@ -13,14 +13,16 @@
}
],
"require": {
"php": ">=5.6.4",
"guzzlehttp/guzzle": "^6.3|^7.0",
"nesbot/carbon": "^1.22|^2.0"
"php": ">=8.0",
"guzzlehttp/guzzle": "^7.4.3"
},
"require-dev": {
"monolog/monolog": "^2.6.0"
},
"autoload": {
"psr-4": {
"AXP\\YaMetrika\\": "src"
}
},
"minimum-stability": "dev"
}
}
28 changes: 28 additions & 0 deletions example/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use AXP\YaMetrika\Client;
use AXP\YaMetrika\YaMetrika;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;

$stack = HandlerStack::create();
$stack->push(
Middleware::log(
new Logger('Logger'),
new MessageFormatter('{req_body} - {res_body}')
)
);

$token = getenv('TOKEN');
$counterId = getenv('COUNTER_ID');

$client = new Client($token, $counterId, [
'handler' => $stack
]);
$metrika = new YaMetrika($client);

$visitors = $metrika->getVisitors();

print_r($visitors->formatData());
17 changes: 17 additions & 0 deletions example/proxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use AXP\YaMetrika\Client;
use AXP\YaMetrika\YaMetrika;

$token = getenv('TOKEN');
$counterId = getenv('COUNTER_ID');
$proxy = getenv('PROXY');

$client = new Client($token, $counterId, [
'proxy' => $proxy
]);
$metrika = new YaMetrika($client);

$visitors = $metrika->getVisitors();

print_r($visitors->formatData());
14 changes: 14 additions & 0 deletions example/visitors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use AXP\YaMetrika\Client;
use AXP\YaMetrika\YaMetrika;

$token = getenv('TOKEN');
$counterId = getenv('COUNTER_ID');

$client = new Client($token, $counterId);
$metrika = new YaMetrika($client);

$visitors = $metrika->getVisitors();

print_r($visitors->formatData());
20 changes: 0 additions & 20 deletions examples/customQuery.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getAgeGender.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getBrowsers.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getGeo.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getMostViewedPages.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getPreset.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getUsersSearchEngine.php

This file was deleted.

15 changes: 0 additions & 15 deletions examples/getVisitors.php

This file was deleted.

60 changes: 60 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace AXP\YaMetrika;

use AXP\YaMetrika\Exception\ClientException;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;

/**
* Class Client
*
* @author Alexander Pushkarev <axp-dev@yandex.com>
* @link https://github.com/axp-dev/ya-metrika
* @package AXP\YaMetrika
*/
class Client
{
private string $apiEndpoint = 'https://api-metrika.yandex.ru/stat/v1/data';

private HttpClient $httpClient;

public function __construct(
private string $token,
private string $counterId,
private array $httpClientOptions = []
)
{
$this->httpClient = new HttpClient(
$this->buildConfig($this->httpClientOptions)
);
}

private function buildConfig(array $options): array
{
return array_merge($options, [
RequestOptions::HEADERS => [
'Authorization' => "OAuth {$this->token}"
]
]);
}

private function buildUri(array $params = []): string
{
$params['ids'] = $this->counterId;

return $this->apiEndpoint . '?' . http_build_query(data: $params, arg_separator: '&');
}

public function request($params): array
{
try {
$response = $this->httpClient->request('GET', $this->buildUri($params));

return json_decode($response->getBody(), true);
} catch (GuzzleException $exception) {
throw new ClientException($exception->getMessage());
}
}
}
14 changes: 14 additions & 0 deletions src/Exception/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace AXP\YaMetrika\Exception;

/**
* Class ClientException
*
* @author Alexander Pushkarev <axp-dev@yandex.com>
* @link https://github.com/axp-dev/ya-metrika
* @package AXP\YaMetrika\Exception
*/
class ClientException extends \Exception
{
}
14 changes: 14 additions & 0 deletions src/Exception/FormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace AXP\YaMetrika\Exception;

/**
* Class ClientException
*
* @author Alexander Pushkarev <axp-dev@yandex.com>
* @link https://github.com/axp-dev/ya-metrika
* @package AXP\YaMetrika\Exception
*/
class FormatException extends \Exception
{
}
17 changes: 0 additions & 17 deletions src/Exceptions/YaMetrikaException.php

This file was deleted.

Loading

0 comments on commit 761505d

Please sign in to comment.