mirakl / sdk-php-shop

Mirakl PHP SDK for sellers
29 stars 16 forks source link

Token-based pagination for /api/mms/orders is available but not implemented #71

Open gio-vlasov opened 1 month ago

gio-vlasov commented 1 month ago

The doc for this endpoint says it has token-based seek pagination available, but this library only allows offset-based pagination for $client->getOrders().

Is there a way to implement that? I tried something like this, by analogy with other endpoints:

namespace App\MiraklApi\Requests;

use Mirakl\Core\Response\Decorator\SeekableCollection;
use Mirakl\MMP\Shop\Domain\Collection\Order\ShopOrderCollection;
use Mirakl\MMP\Shop\Request\Order\Get\GetOrdersRequest;

class GetOrdersRequest_Seekable extends GetOrdersRequest
{
    public function getResponseDecorator()
    {
        return new SeekableCollection(ShopOrderCollection::class, 'data');
    }

}

but had no success.

My use case for this is fetching long order histories over extended period of time in separate processes, as such fetching can produce "429 Too many requests" so it will have to finish later, but store the point up until which it had already fetched the orders. Offset pagination doesn't seem to fit for that use case, as there seem to be no guarantees on the sorting order, so unfetched orders may change position during subsequent requests to lower offset if new orders are created. And token-based pagination is supposed to preserve ordering.

jreinke commented 1 month ago

Hello,

Indeed, the current SDK version does not support Mirakl services APIs such as SOR11 you are talking about. But your attempt is not so bad, I would have tried something like that instead:

use Mirakl\Core\Domain\Collection\MiraklCollection;
use Mirakl\Core\Request\AbstractRequest;
use Mirakl\Core\Request\SeekableTrait;
use Mirakl\Core\Response\Decorator\SeekableCollection;

class SOR11 extends AbstractRequest
{
    use SeekableTrait;

    /**
     * @var string
     */
    protected $endpoint = '/mms/orders';

    /**
     * @inheritdoc
     */
    public function getResponseDecorator()
    {
        return new SeekableCollection(MiraklCollection::class, 'data');
    }
}