App Store Connect Swift SDK

repository·master·Indexed 23 days ago

https://github.com/avdlee/appstoreconnect-swift-sdk

A Swift SDK for interacting with the Apple App Store Connect API. It features type-safe models, JWT signing logic, and support for all Apple platforms. The library provides an APIProvider for executing requests via an APIEndpoint hierarchy, supports both AsyncSequence and manual pagination for paged responses, and includes structured error handling for APIProvider.Error.requestFailure.

Tokens
1.2K
Snippets
4
Records
5
Agent score
32%

What's inside appstoreconnect-swift-sdk

  1. Handle paged API responses

    master

    When an API response is paginated, you have two ways to handle it:

    1. AsyncSequence: Use provider.paged(request) to iterate through all pages automatically using a for try await loop.
    2. Manual Pagination: Use provider.request(_:isPagedResponse:) to check if a response has more pages, and provider.request(request, pageAfter: firstPageResult) to fetch the next page.
    // Option 1: Using AsyncSequence to get all pages
    var allApps: [App] = []
    for try await pagedResult in provider.paged(request) {
        allApps.append(contentsOf: pagedResult.data)
    }
    
    // Option 2: Manual pagination
    let firstPageResult = try await provider.request(request)
    if provider.request(request, isPagedResponse: firstPageResult) {
        if let nextPage = try await provider.request(request, pageAfter: firstPageResult) {
            let secondPageApps = nextPage.data
        }
    }
  2. Install the App Store Connect Swift SDK via Swift Package Manager

    master

    To add the SDK to your Swift project, add it as a dependency in your Package.swift file using the following URL and version requirement:

    dependencies: [
        .package(url: "https://github.com/AvdLee/appstoreconnect-swift-sdk.git", .upToNextMajor(from: "4.0.0"))
    ]
  3. Handle API errors

    master

    Errors from the App Store Connect API can be caught by specifically catching APIProvider.Error.requestFailure. This error provides the HTTP status code and the structured error response from Apple, which includes error codes, titles, and details.

    do {
        print(try await self.provider.request(requestWithError).data)
    } catch APIProvider.Error.requestFailure(let statusCode, let errorResponse, _) {
        print("Request failed with statuscode: \(statusCode) and the following errors:")
        errorResponse?.errors?.forEach({ error in
            print("Error code: \(error.code)")
            print("Error title: \(error.title)")
            print("Error detail: \(error.detail)")
        })
    } catch {
        print("Something went wrong: \(error.localizedDescription)")
    }
  4. Configure APIConfiguration

    master

    To use the SDK, you must create an APIConfiguration using your App Store Connect API credentials (Issuer ID, Private Key ID, and the Private Key itself or a URL to the .p8 file).

    Private Key Format: When providing the privateKey as a string, you must remove the header/footer lines (-----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----) and any whitespace/newlines from the .p8 file content.

    Both configuration methods support an optional expirationDuration (in seconds) to account for clock skews. The default is 20 minutes.

  5. Perform an API request with APIProvider

    master

    After initializing an APIProvider with your APIConfiguration, you can perform requests using the APIEndpoint hierarchy. Requests are typically made using await and return a result containing the .data property.

    import AppStoreConnect_Swift_SDK
    
    // 1. Create configuration and provider (assumed setup)
    // let provider = APIProvider(configuration: configuration)
    
    // 2. Define and execute request
    let request = APIEndpoint
        .v1
        .apps
        .get(parameters: .init(
            sort: [.bundleID],
            fieldsApps: [.appInfos, .name, .bundleID],
            limit: 5
        ))
    
    let apps = try await self.provider.request(request).data
    print("Did fetch \(apps.count) apps")