lazy-json-pages
lazy-json-pages copied to clipboard
is it possible to get the nextlink from the header?
I am wondering if that is possible because searching for "header" in this repo didn't show me anything.
thanks
Hi @michabbb, it is not possible at the moment but it shouldn't be too hard to add it to the package.
If you can describe how the API you are dealing with works, I'll try to implement the missing logic to make it work.
My assumption is that every page of your API has a header with a URL pointing to the next page, is that correct? Does the last page include the header with a NULL value or the header is not present at all?
Feel free to add as much details as possible, the more the better :)
hey @cerbero90 thanks for your quick response. please don't feel pushed to do anything. I am just investigating some packages and found this one and asked myself, how it could work with the header.
but of course I can provide Infos to help making your package more flexible 😊
I asked the question because I have the shopify api in mind, where the nextlink is provided in the header result:
https://shopify.dev/api/usage/pagination-rest#link-headers
so the last page still provides the header, but only with the "previous" link.
in laravel I am currently doing this way:
private function parseNextLink(Response $response): string
{
if (!array_key_exists('Link', $response->headers())) {
return '';
}
$links = $this->parseLinks($response->headers()['Link'][0]);
if (array_key_exists('next', $links)) {
return $links['next'];
}
return '';
}
private function parseLinks(string $linkstr): array
{
$links = explode(',', $linkstr);
$returnParsedLinks = [];
foreach ($links as $link) {
$link = preg_replace("/\s+/", "", $link);
preg_match('/^<(https:\/\/[\S]+)>;rel="(previous|next)"/', $link, $matches);
if (count($matches) === 3) {
$returnParsedLinks[$matches[2]] = $matches[1];
}
}
return $returnParsedLinks;
}
so maybe my example helps to add another pager to your project 😏
Thanks for all the details @michabbb, I didn't know that the Link header is a standard :)
Given that many APIs may adopt this standard, I'm thinking of exposing a new configuration option to make it easy to fetch the next pages programmatically. Something like:
LazyCollection::fromJsonPages($source, $path, fn (Config $config) => $config->nextPageInLinkHeader());
That would be all we need to let this package work with any API adopting the Link header.
I'm planning the release of v2 and this feature will definitely be included 👍 However if your investigation doesn't find alternatives and this feature is urgent for you, I can try to find some time to implement it in v1.
thanks for your feedback. don't worry, there is nothing urgent right now. I guess this would definitely be a good feature for this project 😏
@michabbb the Link header is now supported in Lazy JSON Pages v2 🎉