Skip to content

Commit

Permalink
Check hard limit in client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jul 18, 2024
1 parent eb253e2 commit d53e1e7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/Client/CurseForgeAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ public function setHttpClient(?ClientInterface $httpClient): static
* @return PaginatedGameList
* @throws ApiException
*/
public function getGames(int $offset = 0, int $pageSize = 50): PaginatedGameList
public function getGames(int $offset = 0, int $pageSize = PaginatedGameList::MAX_PAGE_SIZE): PaginatedGameList
{
return new PaginatedGameList($this, $this->games->getGames($offset, $pageSize));
return new PaginatedGameList($this, $this->games->getGames(
$offset,
PaginatedGameList::getAllowedPageSize($offset, $pageSize),
));
}

/**
Expand Down Expand Up @@ -243,7 +246,7 @@ public function searchMods(ModSearchOptions $options): PaginatedModList
$options->getAuthorId(),
$options->getSlug(),
$options->getOffset(),
$options->getPageSize()
PaginatedModList::getAllowedPageSize($options->getOffset(), $options->getPageSize()),
), $options);
}

Expand Down Expand Up @@ -313,7 +316,7 @@ public function getModFiles(ModFilesOptions $options): PaginatedFilesList
$options->getModLoaderType()?->value,
$options->getGameVersionTypeId(),
$options->getOffset(),
$options->getPageSize(),
PaginatedFilesList::getAllowedPageSize($options->getOffset(), $options->getPageSize()),
), $options);
}

Expand Down
22 changes: 20 additions & 2 deletions lib/Client/List/PaginatedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,30 @@ abstract class PaginatedList implements Iterator, ArrayAccess, Countable
{
/**
* The limit for how many results are allowed to be requested.
* @type int
* @var int
*/
public const LIMIT = 10_000;

/**
* The maximum page size that can be requested.
* @var int
*/
public const MAX_PAGE_SIZE = 50;

protected int $iterator = 0;


/**
* Get the maximum page size that can be requested.
* @param int $offset
* @param int $target
* @return int
*/
public static function getAllowedPageSize(int $offset, int $target = self::MAX_PAGE_SIZE): int
{
return min($target, self::MAX_PAGE_SIZE, static::LIMIT - $offset);
}

protected function __construct(
protected Pagination $pagination,
/**
Expand Down Expand Up @@ -235,7 +253,7 @@ public function count(): int
*/
protected function getNextPageSize(int $offset): int
{
return min($this->pagination->getPageSize(), static::LIMIT - $offset);
return static::getAllowedPageSize($offset, $this->pagination->getPageSize());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Client/Options/ModFiles/ModFilesOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aternos\CurseForgeApi\Client\Options\ModFiles;

use Aternos\CurseForgeApi\Client\List\PaginatedFilesList;
use Aternos\CurseForgeApi\Client\Options\ModSearch\ModLoaderType;

class ModFilesOptions
Expand All @@ -12,7 +13,7 @@ public function __construct(
protected ?ModLoaderType $modLoaderType = null,
protected ?int $gameVersionTypeId = null,
protected int $offset = 0,
protected int $pageSize = 50,
protected int $pageSize = PaginatedFilesList::MAX_PAGE_SIZE,
)
{
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Client/Options/ModSearch/ModSearchOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Aternos\CurseForgeApi\Client\Options\ModSearch;

use Aternos\CurseForgeApi\Client\List\PaginatedModList;

class ModSearchOptions
{
/**
Expand Down Expand Up @@ -32,7 +34,7 @@ public function __construct(
protected ?int $authorId = null,
protected ?string $slug = null,
protected int $offset = 0,
protected int $pageSize = 50,
protected int $pageSize = PaginatedModList::MAX_PAGE_SIZE,
)
{
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ public function testSearchMods()
}

public function testSearchModsLastPage()
{
$options = new ModSearchOptions(static::MINECRAFT_GAME_ID);
$options->setPageSize(50);
$options->setOffset(9_960);

$mods = $this->apiClient->searchMods($options);
$this->assertNotEmpty($mods);
$this->assertFalse($mods->hasNextPage());
$this->assertEquals($mods::LIMIT, $mods->getPagination()->getIndex() + $mods->getPagination()->getPageSize());
}

public function testSearchModsLastPageNext()
{
$options = new ModSearchOptions(static::MINECRAFT_GAME_ID);
$options->setPageSize(50);
Expand Down

0 comments on commit d53e1e7

Please sign in to comment.