Skip to content

Commit

Permalink
Merge pull request #32 from lokalise/feature/pagination_cursor_support
Browse files Browse the repository at this point in the history
Add support for cursor based pagination
  • Loading branch information
RonnyLV authored May 23, 2024
2 parents 44dc830 + 4d192aa commit 5425280
Show file tree
Hide file tree
Showing 24 changed files with 363 additions and 116 deletions.
10 changes: 5 additions & 5 deletions Api/Endpoints/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Comments
Expand Down Expand Up @@ -46,7 +46,7 @@ public function listProject(string $projectId, array $queryParams = []): Lokalis
*/
public function fetchAllProject(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/comments",
[],
Expand Down Expand Up @@ -89,7 +89,7 @@ public function listKey(string $projectId, int $keyId, array $queryParams = []):
*/
public function fetchAllKey(string $projectId, int $keyId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/keys/$keyId/comments",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Contributors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Contributors
Expand Down Expand Up @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/contributors",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/CustomTranslationStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class CustomTranslationStatuses
Expand Down Expand Up @@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/custom-translation-statuses",
$queryParams,
Expand Down
73 changes: 71 additions & 2 deletions Api/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ protected function request(string $requestType, string $uri, array $queryParams
*
* @throws LokaliseApiException
* @throws LokaliseResponseException
* @deprecated Use requestAllUsingCursor or requestAllUsingPaging instead
*/
protected function requestAll(
string $requestType,
Expand All @@ -118,12 +119,35 @@ protected function requestAll(
array $body = [],
string $bodyResponseKey = ''
): LokaliseApiResponse {
return $this->requestAllUsingPaging($requestType, $uri, $queryParams, $body, $bodyResponseKey);
}

/**
* @param string $requestType
* @param string $uri
* @param array $queryParams
* @param array $body
* @param string $bodyResponseKey
*
* @return LokaliseApiResponse
*
* @throws LokaliseApiException
* @throws LokaliseResponseException
*/
protected function requestAllUsingPaging(
string $requestType,
string $uri,
array $queryParams = [],
array $body = [],
string $bodyResponseKey = ''
): LokaliseApiResponse
{
$page = 1;
$queryParams = array_merge($queryParams, ['limit' => static::FETCH_ALL_LIMIT, 'page' => $page]);

$bodyData = [];
$result = $this->request($requestType, $uri, $queryParams, $body);
if (is_array($result->body[$bodyResponseKey])) {
if (isset($result->body[$bodyResponseKey]) && is_array($result->body[$bodyResponseKey])) {
$bodyData = $result->body[$bodyResponseKey];
}
while ($result->getPageCount() > $page) {
Expand All @@ -134,14 +158,59 @@ protected function requestAll(

$result = $this->request($requestType, $uri, $queryParams, $body);
if (is_array($result->body[$bodyResponseKey])) {
$bodyData = array_merge($result->body[$bodyResponseKey], $bodyData);
$bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]);
$result->body[$bodyResponseKey] = $bodyData;
}
}

return $result;
}


/**
* @param string $requestType
* @param string $uri
* @param array $queryParams
* @param array $body
* @param string $bodyResponseKey
*
* @return LokaliseApiResponse
*
* @throws LokaliseApiException
* @throws LokaliseResponseException
*/
protected function requestAllUsingCursor(
string $requestType,
string $uri,
array $queryParams = [],
array $body = [],
string $bodyResponseKey = ''
): LokaliseApiResponse
{
$bodyData = [];
$cursor = '';
$queryParams['limit'] = static::FETCH_ALL_LIMIT;
$queryParams['pagination'] = 'cursor';
while (true) {
$queryParams['cursor'] = $cursor;

$result = $this->request($requestType, $uri, $queryParams, $body);

if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) {
$bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]);
$result->body[$bodyResponseKey] = $bodyData;
}

if (!$result->hasNextCursor()) {
break;
}

$cursor = $result->getNextCursor();
}

return $result;
}

/**
* @param array $queryParams
*
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Files
Expand Down Expand Up @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/files",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Keys
Expand Down Expand Up @@ -48,7 +48,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingCursor(
'GET',
"projects/$projectId/keys",
$queryParams,
Expand Down
10 changes: 5 additions & 5 deletions Api/Endpoints/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Languages
Expand Down Expand Up @@ -43,7 +43,7 @@ public function listSystem(array $queryParams = []): LokaliseApiResponse
*/
public function fetchAllSystem(): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"system/languages",
[],
Expand Down Expand Up @@ -84,7 +84,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/languages",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Orders
Expand Down Expand Up @@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse
*/
public function fetchAll(int $teamId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"teams/{$teamId}/orders",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/PaymentCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Cards
Expand Down Expand Up @@ -43,7 +43,7 @@ public function list(array $queryParams = []): LokaliseApiResponse
*/
public function fetchAll(): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"payment_cards",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Projects
Expand Down Expand Up @@ -45,7 +45,7 @@ public function list(array $queryParams = []): LokaliseApiResponse
*/
public function fetchAll(array $queryParams = []): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects",
$queryParams,
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Screenshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Screenshots
Expand Down Expand Up @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/screenshots",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Snapshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Snapshots
Expand Down Expand Up @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/snapshots",
[],
Expand Down
8 changes: 4 additions & 4 deletions Api/Endpoints/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Lokalise\Endpoints;

use \Lokalise\LokaliseApiResponse;
use \Lokalise\Exceptions\LokaliseApiException;
use \Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\Exceptions\LokaliseApiException;
use Lokalise\Exceptions\LokaliseResponseException;
use Lokalise\LokaliseApiResponse;

/**
* Class Tasks
Expand Down Expand Up @@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes
*/
public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse
{
return $this->requestAll(
return $this->requestAllUsingPaging(
'GET',
"projects/$projectId/tasks",
$queryParams,
Expand Down
Loading

0 comments on commit 5425280

Please sign in to comment.