Files
asconnect-service/Sources/ASConnectService/Catalogs/ASConnectService.docc/Library.md
T

102 lines
2.6 KiB
Markdown
Raw Normal View History

2025-10-05 23:16:54 +00:00
# ``ASConnectService``
A type-safe Swift client for the App Store Connect API.
2025-10-05 23:16:54 +00:00
## Overview
ASConnectService exposes every endpoint of Apple's App Store Connect API as a Swift method with typed requests and responses. It's generated from the official OpenAPI specification with [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator).
2026-03-26 01:07:23 +00:00
### Creating a Client
2026-10-25 18:30:43 +01:00
Create a ``Client`` with a server URL, a transport, and a ``BearerAuthMiddleware``. With an API key from App Store Connect, ``APIKeyTokenProvider`` signs the JSON Web Tokens (JWT) the API requires, and renews them before they expire:
2026-03-26 01:07:23 +00:00
```swift
import ASConnectService
import OpenAPIURLSession
2026-10-25 18:11:51 +01:00
let tokenProvider = try APIKeyTokenProvider(
issuerID: yourIssuerID,
keyID: yourKeyID,
privateKey: yourP8FileContents
)
2026-10-25 18:30:43 +01:00
let client = Client(
serverURL: try Servers.Server1.url(),
transport: URLSessionTransport(),
middlewares: [
BearerAuthMiddleware { try await tokenProvider.token() }
]
)
```
To use a token you signed yourself, pass it directly. It expires after 20 minutes at most:
```swift
BearerAuthMiddleware(token: yourJWTToken)
```
2026-03-26 01:07:23 +00:00
### Making API Calls
Each endpoint is a method on ``Client``, declared by ``APIProtocol``. It returns an `Output` enum with a case for each documented response:
2026-03-26 01:07:23 +00:00
```swift
let response = try await client.appsGetCollection(query: .init(limit: 10))
let apps = try response.ok.body.json.data
2026-03-26 01:07:23 +00:00
```
2025-10-05 23:16:54 +00:00
The `ok` property throws for any other response. To handle those, switch over the `Output` cases, such as `.unauthorized` or `.undocumented(statusCode:_:)`.
### Fetching More Pages
2026-10-25 18:42:16 +01:00
Lists are paged. Iterate a ``PagedSequence`` to fetch each page in turn:
```swift
2026-10-25 18:42:16 +01:00
let pages = PagedSequence(links: \.links) { cursor in
try await client.appsGetCollection(query: .init(limit: 200, cursor: cursor)).ok.body.json
}
2026-10-25 18:42:16 +01:00
var apps: [Components.Schemas.App] = []
for try await page in pages {
apps += page.data
2026-10-25 18:42:16 +01:00
}
```
2026-10-25 18:42:16 +01:00
To fetch pages yourself, pass a page's ``Components/Schemas/PagedDocumentLinks/nextCursor`` as the `cursor` of the next request.
2026-10-25 18:14:19 +01:00
### Handling Rate Limits
Add ``RateLimitMiddleware`` before the authentication middleware to retry `429 Too Many Requests` responses:
```swift
middlewares: [
RateLimitMiddleware(),
BearerAuthMiddleware { try await tokenProvider.token() }
]
```
2025-10-05 23:16:54 +00:00
## Topics
2026-03-26 01:07:23 +00:00
### API Client
2025-10-05 23:16:54 +00:00
2026-03-26 01:07:23 +00:00
- ``Client``
- ``APIProtocol``
### Authentication
- ``BearerAuthMiddleware``
2026-10-25 18:11:51 +01:00
- ``APIKeyTokenProvider``
2026-03-26 01:07:23 +00:00
2026-10-25 18:42:16 +01:00
### Pagination
- ``PagedSequence``
2026-10-25 18:14:19 +01:00
### Rate Limiting
- ``RateLimitMiddleware``
2026-03-26 01:07:23 +00:00
### Generated Types
- ``Components``
- ``Operations``
- ``Servers``