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

Added paged documents links handling to APIProvider

Open andyj-at-aspin opened this issue 1 year ago • 1 comments

Here's a new API for handling paged sets of results from AppStore Connect.

The additions do not rely on chopping up bits of URL to find specific URL Query parameters, rather they access the entire URL passed to the client as the next page link by AppStore Connect, adding in the necessary JWT authentication headers and decoding the subsequent response in the same way a direct call to APIProvider.request(_) would decode the response from the endpoint. As such, these functions do not need to care what the endpoint is to use its generic specialisation to apply the correct decode() and return the correct structure.

    let request = APIEndpoint.v1.apps
        .get(parameters: .init(
            sort: [.bundleID],
            fieldsApps: [.appInfos, .name, .bundleID],
            limit: 2
        ))
    
    // Demonstration of AsyncSequence result of APIProvider.paged(_)
    var allApps: [App] = []
    for try await pagedResult in provider.paged(request) {
        allApps.append(contentsOf: pagedResult.data)
    }

    // Demonstration of APIProvider.hasPagedDocumentLinks(_) and APIProvider.request(_: pageAfter:)
    let firstPageResult = try await provider.request(request)
    let firstPageApps = firstPageResult.data
    print(firstPageApps)
    if provider.hasPagedDocumentLinks(firstPageResult) {        
        if let nextPage = try await provider.request(request, pageAfter: firstPageResult) {
            let secondPageApps = nextPage.data
            print(secondPageApps)
        }
    }

APIProvider has new public functions:

// Does this object have a property that is a PagedDocumentLinks structure?
public func hasPagedDocumentLinks<T>(_ object: T) -> Bool 

// If the argument pageAfter has a PagedDocumentLinks.next property, get the next page of results, 
// decoding the response as if it had been a direct call to the requested endpoint (async version)
public func request<T>(_ endpoint: Request<T>, pageAfter: T) async throws -> T? 

// If the argument pageAfter has a PagedDocumentLinks.next property, get the next page of results, 
// decoding the response as if it had been a direct call to the requested endpoint (completion 
// handler version)
public func request<T: Decodable>(_ request: Request<T>, pageAfter currentPage: T, completion: @escaping RequestCompletionHandler<T?>) 

// Return all pages of responses to the endpoint as an AsyncSequence
public func paged<T>(_ endpoint: Request<T>) -> PagedRequest<T>  

Fixes #187

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

Also, @andyj-at-aspin, it would be great to include your PR description example in a section to the readme!

AvdLee avatar Aug 05 '22 12:08 AvdLee

Ok. Done all that Antoine.

I have changed the function call associated with evaluating whether an object had PagedDocumentLinks properties to one that required the request to be passed in as well - so enforcing the test was only run on expected endpoint response types.

I have added the examples to the readme and have proved to myself that the example still works with the change to the function call.

I've added some documentation to the new public function declarations, but if they're no good I apologise.

I've never made project changes following a pull request before and I don't know of this is all I need to do or if I have to do some further action for you to see the new commit. I work in a team of 2 - both with 25+ years experience in the same company - and we are responsible for merging our own work into the relevant development/release branches.

andyj-at-aspin avatar Aug 05 '22 16:08 andyj-at-aspin

Warnings
:warning: 'AppStoreVersionSubmission' is deprecated: Deprecated
:warning: 'AppStoreVersionSubmission' is deprecated: Deprecated
:warning: 'AppStoreVersionSubmission' is deprecated: Deprecated
:warning: 'AppStoreVersionSubmission' is deprecated: Deprecated
:warning: 'AgeRatingDeclaration' is deprecated: Deprecated
:warning: 'AgeRatingDeclaration' is deprecated: Deprecated
:warning: 'AgeRatingDeclaration' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'AppPricePoint' is deprecated: Deprecated
:warning: 'PricePoints' is deprecated: Deprecated
:warning: 'PricePoints' is deprecated: Deprecated
:warning: 'PricePoints' is deprecated: Deprecated
:warning: 'InAppPurchases' is deprecated: Deprecated
:warning: 'InAppPurchases' is deprecated: Deprecated
:warning: 'InAppPurchases' is deprecated: Deprecated
Messages
:book:

View more details on Bitrise

:book: AppStoreConnect-Swift-SDK-Tests: Executed 9 tests (0 failed, 0 retried, 0 skipped) in 0.137 seconds

SwiftLint found issues

Severity File Reason
Warning PagedRequest.swift:12 Line should be 140 characters or less: currently 146 characters (line_length)
Warning PagedRequest.swift:15 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:18 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:23 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:25 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:30 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:36 Lines should not have trailing whitespace. (trailing_whitespace)
Warning PagedRequest.swift:39 Lines should not have trailing whitespace. (trailing_whitespace)
Error APIProvider.swift:309 Line should be 160 characters or less: currently 247 characters (line_length)
Warning APIProvider.swift:280 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:290 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:306 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:317 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:328 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:338 Lines should not have trailing whitespace. (trailing_whitespace)
Warning APIProvider.swift:346 Lines should not have trailing whitespace. (trailing_whitespace)

Code Coverage Report

Name Coverage

Generated by :no_entry_sign: Danger Swift against f1e7b72becab61d0a2c4a85d4246a625dccb8f54

SwiftLeeBot avatar Aug 08 '22 18:08 SwiftLeeBot

Congratulations! :tada: This was released as part of Release 2.3.0 :rocket:

Generated by GitBuddy

SwiftLeeBot avatar Oct 31 '22 08:10 SwiftLeeBot