saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.06k stars 105 forks source link

Pagination without next_page_url #169

Closed advil0 closed 1 year ago

advil0 commented 1 year ago

Hello!

Was wondering if it's possible to use the built-in pagination without supplying a next_page_url? The API I'm using only supplies the below for pagination.

"pagination": { "pageNumber": "1", "pageSize": "100", "totalAvailable": "640" }

Sammyjo20 commented 1 year ago

Hmm you could extend the Paginator class you’re currently using and then overwrite the “isFinished” logic to check for your properties instead? Let me know how you get on!

Sammyjo20 commented 1 year ago

You could also use the total / page count calculation, you'd have to change the isFinished slightly...

Your class

class CustomPaginator extends PagedPaginator
{
    protected function isFinished(): bool
    {
            return $this->getCurrentPage() > $this->totalPages();
    }
}

Your configuration

public function paginate(Request $request, mixed ...$additionalArguments): PagedPaginator;
{
    $paginator = new PagedPaginator($this, $request, perPage: 50, ...$additionalArguments);

    $paginator->setTotalKeyName('pagination.totalAvailable');
    $paginator->setPageKeyName('pagination.pageNumber');

    return $paginator;
}
benjaminbowles commented 1 year ago

I have the same issue, I've tried your custompagintor and it returns the correct number of paginations, but all the n+1 paginations repeat page 1, instead of moving onto page 2/3/4 etc.

My connector code looks like:

public function paginate(Request $request, mixed ...$additionalArguments): CRMSPaginator
    {
        $paginator = new CRMSPaginator($this, $request, 25, ...$additionalArguments);

        $paginator->setLimitKeyName('meta.per_page');
        $paginator->setTotalKeyName('meta.total_row_count');
        $paginator->setPageKeyName('meta.page');

        return $paginator;
    } 

My pagination code looks like:

        $paginator = $current->paginate(new GetOpportunityRequest);

        foreach($paginator->json() as $opportunities) {
            $pageData[] = $opportunities;
        }
benjaminbowles commented 1 year ago

@advil0 have you had any joy with this at all?

Sammyjo20 commented 1 year ago

Hey both, just wanted to let you know this is still on my radar. I'll do some testing on my end and see if I can replicate the issue.

I would also recommend adding the AlwaysThrowOnErrors trait to your connector so it throws an exception if a page errors, that might help with debugging. You can also use Guzzle's debug option in your connector/requests config.

$request->config()->add('debug', true); or $request->config()->add('debug', fopen('somefile', 'w+'));

Sammyjo20 commented 1 year ago

@advil0 Did you manage to fix this?

Sammyjo20 commented 1 year ago

I'm going to close this issue for now.