mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created TagsStats endpoint
This commit is contained in:
@@ -35,6 +35,7 @@ return [
|
||||
Action\Visit\GlobalVisitsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Visit\OrphanVisitsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Tag\TagsStatsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Tag\DeleteTagsAction::class => ConfigAbstractFactory::class,
|
||||
Action\Tag\UpdateTagAction::class => ConfigAbstractFactory::class,
|
||||
Action\Domain\ListDomainsAction::class => ConfigAbstractFactory::class,
|
||||
@@ -75,6 +76,7 @@ return [
|
||||
],
|
||||
Action\ShortUrl\ListShortUrlsAction::class => [Service\ShortUrlService::class, ShortUrlDataTransformer::class],
|
||||
Action\Tag\ListTagsAction::class => [TagService::class],
|
||||
Action\Tag\TagsStatsAction::class => [TagService::class],
|
||||
Action\Tag\DeleteTagsAction::class => [TagService::class],
|
||||
Action\Tag\UpdateTagAction::class => [TagService::class],
|
||||
Action\Domain\ListDomainsAction::class => [DomainService::class, Options\NotFoundRedirectOptions::class],
|
||||
|
||||
@@ -37,6 +37,7 @@ return [
|
||||
|
||||
// Tags
|
||||
Action\Tag\ListTagsAction::getRouteDef(),
|
||||
Action\Tag\TagsStatsAction::getRouteDef(),
|
||||
Action\Tag\DeleteTagsAction::getRouteDef(),
|
||||
Action\Tag\UpdateTagAction::getRouteDef(),
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ class ListTagsAction extends AbstractRestAction
|
||||
]);
|
||||
}
|
||||
|
||||
// This part is deprecated. To get tags with stats, the /tags/stats endpoint should be used instead
|
||||
$tagsInfo = $this->tagService->tagsInfo($params, $apiKey);
|
||||
$rawTags = $this->serializePaginator($tagsInfo, null, 'stats');
|
||||
$rawTags['data'] = map($tagsInfo, static fn (TagInfo $info) => $info->tag()->__toString());
|
||||
|
||||
36
module/Rest/src/Action/Tag/TagsStatsAction.php
Normal file
36
module/Rest/src/Action/Tag/TagsStatsAction.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Action\Tag;
|
||||
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Common\Paginator\Util\PagerfantaUtilsTrait;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagsParams;
|
||||
use Shlinkio\Shlink\Core\Tag\TagServiceInterface;
|
||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||
|
||||
class TagsStatsAction extends AbstractRestAction
|
||||
{
|
||||
use PagerfantaUtilsTrait;
|
||||
|
||||
protected const ROUTE_PATH = '/tags/stats';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||
|
||||
public function __construct(private TagServiceInterface $tagService)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$query = $request->getQueryParams();
|
||||
$params = TagsParams::fromRawData($query);
|
||||
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||
$tagsInfo = $this->tagService->tagsInfo($params, $apiKey);
|
||||
|
||||
return new JsonResponse(['tags' => $this->serializePaginator($tagsInfo)]);
|
||||
}
|
||||
}
|
||||
127
module/Rest/test-api/Action/TagsStatsTest.php
Normal file
127
module/Rest/test-api/Action/TagsStatsTest.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Rest\Action;
|
||||
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||
|
||||
class TagsStatsTest extends ApiTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideQueries
|
||||
*/
|
||||
public function expectedListOfTagsIsReturned(string $apiKey, array $query, array $expectedTags): void
|
||||
{
|
||||
$resp = $this->callApiWithKey(self::METHOD_GET, '/tags/stats', [RequestOptions::QUERY => $query], $apiKey);
|
||||
$payload = $this->getJsonResponsePayload($resp);
|
||||
|
||||
self::assertEquals(['tags' => $expectedTags], $payload);
|
||||
}
|
||||
|
||||
public function provideQueries(): iterable
|
||||
{
|
||||
yield 'admin API key' => ['valid_api_key', [], [
|
||||
'data' => [
|
||||
[
|
||||
'tag' => 'bar',
|
||||
'shortUrlsCount' => 1,
|
||||
'visitsCount' => 2,
|
||||
],
|
||||
[
|
||||
'tag' => 'baz',
|
||||
'shortUrlsCount' => 0,
|
||||
'visitsCount' => 0,
|
||||
],
|
||||
[
|
||||
'tag' => 'foo',
|
||||
'shortUrlsCount' => 3,
|
||||
'visitsCount' => 5,
|
||||
],
|
||||
],
|
||||
'pagination' => [
|
||||
'currentPage' => 1,
|
||||
'pagesCount' => 1,
|
||||
'itemsPerPage' => 3,
|
||||
'itemsInCurrentPage' => 3,
|
||||
'totalItems' => 3,
|
||||
],
|
||||
]];
|
||||
yield 'admin API key with pagination' => ['valid_api_key', ['page' => 1, 'itemsPerPage' => 2], [
|
||||
'data' => [
|
||||
[
|
||||
'tag' => 'bar',
|
||||
'shortUrlsCount' => 1,
|
||||
'visitsCount' => 2,
|
||||
],
|
||||
[
|
||||
'tag' => 'baz',
|
||||
'shortUrlsCount' => 0,
|
||||
'visitsCount' => 0,
|
||||
],
|
||||
],
|
||||
'pagination' => [
|
||||
'currentPage' => 1,
|
||||
'pagesCount' => 2,
|
||||
'itemsPerPage' => 2,
|
||||
'itemsInCurrentPage' => 2,
|
||||
'totalItems' => 3,
|
||||
],
|
||||
]];
|
||||
yield 'author API key' => ['author_api_key', [], [
|
||||
'data' => [
|
||||
[
|
||||
'tag' => 'bar',
|
||||
'shortUrlsCount' => 1,
|
||||
'visitsCount' => 2,
|
||||
],
|
||||
[
|
||||
'tag' => 'foo',
|
||||
'shortUrlsCount' => 2,
|
||||
'visitsCount' => 5,
|
||||
],
|
||||
],
|
||||
'pagination' => [
|
||||
'currentPage' => 1,
|
||||
'pagesCount' => 1,
|
||||
'itemsPerPage' => 2,
|
||||
'itemsInCurrentPage' => 2,
|
||||
'totalItems' => 2,
|
||||
],
|
||||
]];
|
||||
yield 'author API key with pagination' => ['author_api_key', ['page' => 2, 'itemsPerPage' => 1], [
|
||||
'data' => [
|
||||
[
|
||||
'tag' => 'foo',
|
||||
'shortUrlsCount' => 2,
|
||||
'visitsCount' => 5,
|
||||
],
|
||||
],
|
||||
'pagination' => [
|
||||
'currentPage' => 2,
|
||||
'pagesCount' => 2,
|
||||
'itemsPerPage' => 1,
|
||||
'itemsInCurrentPage' => 1,
|
||||
'totalItems' => 2,
|
||||
],
|
||||
]];
|
||||
yield 'domain API key' => ['domain_api_key', [], [
|
||||
'data' => [
|
||||
[
|
||||
'tag' => 'foo',
|
||||
'shortUrlsCount' => 1,
|
||||
'visitsCount' => 0,
|
||||
],
|
||||
],
|
||||
'pagination' => [
|
||||
'currentPage' => 1,
|
||||
'pagesCount' => 1,
|
||||
'itemsPerPage' => 1,
|
||||
'itemsInCurrentPage' => 1,
|
||||
'totalItems' => 1,
|
||||
],
|
||||
]];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user