Skip to content

Commit

Permalink
upgrade to saloon v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Jul 17, 2023
1 parent 5a36368 commit 1350185
Show file tree
Hide file tree
Showing 142 changed files with 307 additions and 140 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"ext-json": "*",
"illuminate/support": "^9.0 || ^10.0",
"nesbot/carbon": "^2.53.1",
"sammyjo20/saloon": "^1.5.3",
"sammyjo20/saloon-laravel": "^1.5.0",
"sammyjo20/saloon": "^2.0",
"sammyjo20/saloon-laravel": "^2.0",
"spatie/laravel-data": "^3.1",
"symfony/css-selector": "^5.4 || ^6.0",
"symfony/dom-crawler": "^6.1",
Expand Down
7 changes: 0 additions & 7 deletions src/Exceptions/BadGatewayException.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/Exceptions/BadResponseException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/ClientException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/ServerException.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Extractors/Banlist/DefaultExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function handle(Crawler $crawler): ?LengthAwarePaginator
->values(),
total: $pagination['total'],
perPage: $pagination['end'] - $pagination['start'],
currentPage: $this->currentPageFromSelect($crawler->filter('#banlist table + #banlist-nav select')),
);
}
}
1 change: 1 addition & 0 deletions src/Extractors/Banlist/FluentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function handle(Crawler $crawler): ?LengthAwarePaginator
->values(),
total: $pagination['total'],
perPage: $pagination['end'] - $pagination['start'],
currentPage: $this->currentPageFromSelect($crawler->filter('#banlist table + #banlist-nav select')),
);
}
}
9 changes: 9 additions & 0 deletions src/Extractors/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ protected function paginationFromString(string $value): array
'total' => (int) $matches[3],
];
}

protected function currentPageFromSelect(Crawler $select): int
{
return rescue(
callback: fn () => $select->filter('option[selected]')->attr('value'),
report: false,
rescue: 1,
);
}
}
80 changes: 80 additions & 0 deletions src/Paginator/FirstResponsePagedPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Astrotomic\SourceBansSdk\Paginator;

use Closure;
use Saloon\Contracts\Connector;
use Saloon\Contracts\Request;
use Saloon\Exceptions\PaginatorException;
use Saloon\Http\Paginators\PagedPaginator;

class FirstResponsePagedPaginator extends PagedPaginator
{
protected ?int $total = null;

public function __construct(
Connector $connector,
Request $originalRequest,
protected Closure $limitCallback,
protected Closure $totalCallback,
int $page = 1
) {
parent::__construct(
connector: $connector,
originalRequest: $originalRequest,
perPage: PHP_INT_MAX,
page: $page
);

$this->limit = null;
}

protected function applyPagination(Request $request): void
{
$request->query()->add($this->getPageKeyName(), $this->getCurrentPage());
}

public function limit(): int
{
if (is_null($this->currentResponse)) {
$this->current();
}

if (is_null($this->limit)) {
$this->limit = call_user_func($this->limitCallback, $this->currentResponse);

if (is_null($this->limit)) {
throw new PaginatorException('Unable to calculate the limit from the response. Make sure the limit callback is correct.');
}
}

return $this->limit;
}

public function totalResults(): int
{
if (is_null($this->currentResponse)) {
$this->current();
}

if (is_null($this->total)) {
$this->total = call_user_func($this->totalCallback, $this->currentResponse);

if (is_null($this->total)) {
throw new PaginatorException('Unable to calculate the total results from the response. Make sure the callback key is correct.');
}
}

return $this->total;
}

public function totalPages(): int
{
return (int) ceil($this->totalResults() / $this->limit());
}

protected function isFinished(): bool
{
return $this->getCurrentPage() > $this->totalPages();
}
}
43 changes: 17 additions & 26 deletions src/Requests/QueryBansRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Str;
use OutOfBoundsException;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Saloon\Contracts\Response;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Request\CastDtoFromResponse;
use SteamID;

class QueryBansRequest extends SaloonRequest
class QueryBansRequest extends Request
{
use CastsToDto;
use CastDtoFromResponse;

protected ?string $method = 'GET';
protected Method $method = Method::GET;

public function __construct(
public readonly int $page = 1,
public readonly ?SteamID $steamid = null,
public readonly ?DateTimeInterface $date = null,
public readonly ?int $perPage = null,
) {
}

public function resolveEndpoint(): string
{
return '';
}

public function defaultQuery(): array
{
return array_merge([
'p' => 'banlist',
'page' => $this->page,
], $this->filter());
}

Expand All @@ -46,7 +49,7 @@ protected function filter(): array
};
}

protected function castToDto(SaloonResponse $response): ?LengthAwarePaginator
public function createDtoFromResponse(Response $response): ?LengthAwarePaginator
{
$crawler = $response->dom();

Expand All @@ -55,24 +58,12 @@ protected function castToDto(SaloonResponse $response): ?LengthAwarePaginator
->first(fn (Extractor $extractor) => $extractor->canHandle($crawler));

if ($extractor === null) {
throw new OutOfBoundsException("[{$response->getOriginalRequest()->getFullRequestUrl()}] is not supported by any extractor.");
throw new OutOfBoundsException("[{$response->getPendingRequest()->getUrl()}] is not supported by any extractor.");
}

$paginator = $extractor->handle($crawler);

if ($paginator === null) {
return null;
}
return $extractor->handle($crawler)
?->setPath($response->getPendingRequest()->getUrl())
?->appends($response->getPendingRequest()->query()->all());

return new LengthAwarePaginator(
items: $paginator->items(),
total: $paginator->total(),
perPage: max($paginator->perPage(), $this->perPage),
currentPage: $this->page,
options: [
'path' => $response->getOriginalRequest()->getFullRequestUrl(),
'query' => $response->getOriginalRequest()->getQuery(),
]
);
}
}
25 changes: 0 additions & 25 deletions src/Responses/SourceBansResponse.php

This file was deleted.

62 changes: 43 additions & 19 deletions src/SourceBansConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,67 @@

namespace Astrotomic\SourceBansSdk;

use Astrotomic\SourceBansSdk\Paginator\FirstResponsePagedPaginator;
use Astrotomic\SourceBansSdk\Requests\QueryBansRequest;
use Astrotomic\SourceBansSdk\Responses\SourceBansResponse;
use DateTimeInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Traits\Plugins\AlwaysThrowsOnErrors;
use Illuminate\Support\LazyCollection;
use Saloon\Contracts\HasPagination;
use Saloon\Contracts\Request;
use Saloon\Contracts\Response;
use Saloon\Http\Connector;
use Saloon\Http\Paginators\PagedPaginator;
use Saloon\Traits\Plugins\AlwaysThrowOnErrors;
use SteamID;

class SourceBansConnector extends SaloonConnector
class SourceBansConnector extends Connector implements HasPagination
{
use AlwaysThrowsOnErrors;

protected ?string $response = SourceBansResponse::class;
use AlwaysThrowOnErrors;

public function __construct(
public readonly string $baseUrl,
) {
}

public function defineBaseUrl(): string
public function resolveBaseUrl(): string
{
return $this->baseUrl;
}

public function queryBans(
int $page = 1,
SteamID $steamid = null,
DateTimeInterface $date = null,
int $perPage = null
): ?LengthAwarePaginator {
return $this->send(
new QueryBansRequest(
page: $page,
steamid: $steamid,
date: $date,
perPage: $perPage
)
)->dto();
int $page = null,
): LengthAwarePaginator|LazyCollection|null {
$request = new QueryBansRequest(
steamid: $steamid,
date: $date,
);

if (! is_null($page)) {
$request->query()->add('page', $page);

return $this->send($request)->dto();
}

return $this->paginate($request)
->collect()
->map(fn (Response $response) => $response->dto()->items())
->collapse();
}

public function paginate(Request $request, ...$additionalArguments): PagedPaginator
{
$paginator = new FirstResponsePagedPaginator(
connector: $this,
originalRequest: $request,
limitCallback: fn (Response $response) => $response->dtoOrFail()->perPage(),
totalCallback: fn (Response $response) => $response->dtoOrFail()->total(),
);

$paginator->setLimitKeyName('_limit');
$paginator->setPageKeyName('page');

return $paginator;
}
}
Loading

0 comments on commit 1350185

Please sign in to comment.