appstoreconnect-swift-sdk icon indicating copy to clipboard operation
appstoreconnect-swift-sdk copied to clipboard

Add documentation about pagination

Open AvdLee opened this issue 1 year ago • 4 comments

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

AvdLee avatar Jul 29 '22 15:07 AvdLee

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 avatar Aug 01 '22 16:08 andyj-at-aspin

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()

andyj-at-aspin avatar Aug 02 '22 10:08 andyj-at-aspin

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!

AvdLee avatar Aug 03 '22 15:08 AvdLee

OK. I have.

andyj-at-aspin avatar Aug 04 '22 17:08 andyj-at-aspin

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

jeffersonsetiawan avatar Oct 21 '22 09:10 jeffersonsetiawan

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

AvdLee avatar Oct 31 '22 08:10 AvdLee

The pull request #195 that closed this issue was merged and released as part of Release 2.3.0 :rocket: 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

SwiftLeeBot avatar Oct 31 '22 08:10 SwiftLeeBot