is there an easy way to do pagination with the ory...
# general
w
is there an easy way to do pagination with the ory sdk ? Or do you have extract the
page_token
out from the
response.headers
?
l
The SDK is auto generated from the API spec, which means it’s a little rough around the edges for this. We ended up writing a small function to parse the page token out of the headers. In golang this is
Copy code
import (
	"fmt"
	"net/http"
	"net/url"

	"<http://github.com/peterhellberg/link|github.com/peterhellberg/link>"
)

// GetNextPageToken parses the link header in the response to get the next page of the API
// See: <https://www.ory.sh/docs/ecosystem/api-design#pagination>
func GetNextPageToken(response *http.Response) (*string, error) {
	links := link.ParseResponse(response)
	if nextPage, ok := links["next"]; ok {
		uri, err := url.Parse(nextPage.URI)
		if err != nil {
			return nil, fmt.Errorf("failed to parse nextPage uri: %w", err)
		}

		pageToken := uri.Query().Get("page_token")
		if pageToken == "" {
			return nil, fmt.Errorf("next link present, but has no page_token")
		}

		return &pageToken, nil
	} else {
		// There is no next page link, so we are at the last page of the response.
		return nil, nil
	}
}
w
thanks!
p
Is there no way to get the
page_token
without parsing headers? I'm just using the Ory Kratos API. It wants a token but there is no clear way to get it. And there is no documentation about how to get it.
Seems an oversight not to include it in the
ListIndentities
response. This worked though, ty.