Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation about pagination #187

Closed
AvdLee opened this issue Jul 29, 2022 · 7 comments · Fixed by #195
Closed

Add documentation about pagination #187

AvdLee opened this issue Jul 29, 2022 · 7 comments · Fixed by #195
Labels
enhancement New feature or request

Comments

@AvdLee
Copy link
Owner

AvdLee commented Jul 29, 2022

It might not be clear from the get-go how to use pagination support. Adding this to the readme would help implementors!

@AvdLee AvdLee added the enhancement New feature or request label Jul 29, 2022
@andyj-at-aspin
Copy link
Contributor

Please please please Antoine. I had paging sorted with the pre 2.0.0 releases but I'm completely stuck without any clue with this new API. I can see the old version specified cursor and limit parameters in the endpoint requests but can't see anywhere in the 2.x API for these to be passed to the server.

@andyj-at-aspin
Copy link
Contributor

andyj-at-aspin commented Aug 2, 2022

I have constructed this code snippet that builds on from the example on the main README.md but processes a response's PagedDocumentLinks to obtain more results using a method similar to the pre-2.0.0 releases.

Whilst this method works, is this the limit of what's achievable or can it be done in a tidier, more elegant way?

func getApps(nextPage: String? = nil) async throws -> [AppStoreConnect_Swift_SDK.App] {
    
    var request = APIEndpoint
        .v1
        .apps
        .get(parameters: .init(
            sort: [.bundleID],
            fieldsApps: [.appInfos, .name, .bundleID],
            limit: 5
        ))
    
    // Append to the query the cursor pointing to the next page of results required, if populated
    if let nextPageCursor = nextPage {
        request.query!.append(("cursor", nextPageCursor))
    }
    
    let requestResult = try await provider.request(request)
    
    let apps = requestResult.data
    
    if let linkNextURL = requestResult.links.next.flatMap({ URL(string: $0) }),
        let queryComponents = URLComponents(url: linkNextURL, resolvingAgainstBaseURL: false)?.queryItems,
        let nextPageCursor = queryComponents.first(where: { $0.name == "cursor" })?.value {
        
        return try await apps + getApps(nextPage: nextPageCursor)
    } else {
        return apps
    }
}


let allApps = try await getApps()

@AvdLee
Copy link
Owner Author

AvdLee commented Aug 3, 2022

This is pretty much how it should work, but it would obviously be much nicer if the SDK supports this out of the box. Feel free to open a PR for that if you're able to make a generic solution. It would be inside APIProvider I think!

@andyj-at-aspin
Copy link
Contributor

OK. I have.

@jeffersonsetiawan
Copy link

I think we need to create new release that support this pagination 🙏

@AvdLee
Copy link
Owner Author

AvdLee commented Oct 31, 2022

@jeffersonsetiawan yes, good point! Just triggered a new release 🙏

@SwiftLeeBot
Copy link
Collaborator

The pull request #195 that closed this issue was merged and released as part of Release 2.3.0 🚀
Please let us know if the functionality works as expected as a reply here. If it does not, please open a new issue. Thanks!

Generated by GitBuddy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants