Support pagination

This commit is contained in:
Kabir Oberai
2025-05-24 23:45:39 +05:30
Unverified
parent 693e60faef
commit 021951fa8e
9 changed files with 225 additions and 68 deletions
@@ -0,0 +1,76 @@
import DeveloperAPI
/// An `AsyncSequence` that unfolds a paginated DeveloperAPI request.
///
/// Some Developer API GET requests are paginated. Each response contains
/// a `links.next` field that points to the next page. This API allows you to
/// consume all pages as an `AsyncSequence`. For example, you can
/// enumerate all devices with
///
/// ```swift
/// let pages = DeveloperAPIPages {
/// try await client.devicesGetCollection().ok.body.json
/// } next: {
/// $0.links.next
/// }
/// for try await page in pages {
/// print(page.data)
/// }
/// ```
public struct DeveloperAPIPages<Page>: AsyncSequence {
// inspired by PagedRequest from
// https://github.com/AvdLee/appstoreconnect-swift-sdk
public var request: @Sendable () async throws -> Page
public var getNext: @Sendable (Page) -> String?
public init(
request: @escaping @Sendable () async throws -> Page,
next getNext: @escaping @Sendable (Page) -> String?
) {
self.request = request
self.getNext = getNext
}
public func makeAsyncIterator() -> AsyncIterator {
AsyncIterator(request: request, getNext: getNext)
}
public struct AsyncIterator: AsyncIteratorProtocol {
fileprivate enum State {
case initial
case hasNext(String)
case end
}
fileprivate let request: @Sendable () async throws -> Page
fileprivate let getNext: @Sendable (Page) -> String?
fileprivate var state: State = .initial
public mutating func next() async throws -> Page? {
let cursor: String?
switch state {
case .initial:
cursor = nil
case .hasNext(let next):
cursor = next
case .end:
return nil
}
let page = try await DeveloperAPIClient.withNextLink(cursor) {
try await request()
}
if let next = getNext(page) {
state = .hasNext(next)
} else {
state = .end
}
return page
}
}
}
@@ -6,6 +6,8 @@ import OpenAPIURLSession
import Dependencies
extension DeveloperAPIClient {
@TaskLocal fileprivate static var cursor: URLQueryItem?
public init(
auth: DeveloperAPIAuthData
) {
@@ -22,6 +24,35 @@ extension DeveloperAPIClient {
]
)
}
/// Perform a paginated request, starting at the page given by `link`.
///
/// `link` should be a URL that contains a `cursor` query parameter. The
/// cursor will be applied to any requests made inside the closure.
public static func withNextLink<T>(
_ link: String?,
isolation: isolated (any Actor)? = #isolation,
perform action: () async throws -> T
) async throws -> T {
let cursor: URLQueryItem?
if let link {
guard let components = URLComponents(string: link),
let newCursor = components.queryItems?.first(where: { $0.name == "cursor" })
else { throw Errors.badNextLink(link) }
cursor = newCursor
} else {
cursor = nil
}
return try await $cursor.withValue(cursor) {
try await action()
}
}
public enum Errors: Error {
case badNextLink(String)
}
}
public enum DeveloperAPIAuthData: Sendable {
@@ -124,11 +155,17 @@ public struct DeveloperAPIXcodeAuthMiddleware: ClientMiddleware {
request.headerFields[.init("X-HTTP-Method-Override")!] = originalMethod.rawValue
request.method = .post
let path = request.path ?? "/"
var components = URLComponents(string: path) ?? .init()
components.queryItems = (components.queryItems ?? []) + [
var newQueryItems = [
URLQueryItem(name: "teamId", value: authData.teamID.rawValue)
]
if let cursor = DeveloperAPIClient.cursor {
newQueryItems.append(cursor)
}
let path = request.path ?? "/"
var components = URLComponents(string: path) ?? .init()
components.queryItems = (components.queryItems ?? []) + newQueryItems
let query = components.percentEncodedQuery ?? ""
components.query = nil
@@ -214,69 +251,17 @@ public struct DeveloperAPIASCAuthMiddleware: ClientMiddleware {
_ baseURL: URL
) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
let jwt = try await generator.generate()
var request = request
let jwt = try await generator.generate()
request.headerFields[.authorization] = "Bearer \(jwt)"
if let cursor = DeveloperAPIClient.cursor {
var components = URLComponents(string: request.path ?? "") ?? .init()
components.queryItems = (components.queryItems ?? []) + [cursor]
request.path = components.string
}
return try await next(request, body, baseURL)
}
}
struct LoggingMiddleware: ClientMiddleware {
static let regex: NSRegularExpression? = {
guard let pat = ProcessInfo.processInfo.environment["XTL_DEV_LOG"] else { return nil }
return try? NSRegularExpression(pattern: pat)
}()
func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var (response, body) = try await next(request, body, baseURL)
guard Self.regex?.firstMatch(
in: operationID,
range: NSRange(operationID.startIndex..., in: operationID)
) != nil
else { return (response, body) }
print("\n\(operationID) response status -> \(response.status)")
if let unwrapped = body {
let data = try await Data(collecting: unwrapped, upTo: .max)
// body may only be consumable once, replace it with the collected data
body = .init(data)
let text = String(decoding: data, as: UTF8.self)
print("\(operationID) response body -> \(text)")
}
return (response, body)
}
}
// syntactic sugar to make it nicer to work with `anyOf:` types
public protocol OpenAPIExtensibleEnum {
associatedtype Value1Payload: RawRepresentable<String>, Codable, Hashable, Sendable, CaseIterable
var value1: Value1Payload? { get set }
var value2: String? { get set }
init(value1: Value1Payload?, value2: String?)
}
extension OpenAPIExtensibleEnum {
public var rawValue: String {
value2!
}
public init(_ value: Value1Payload) {
self.init(value1: value, value2: nil)
}
}
extension Components.Schemas.BundleIdPlatform: OpenAPIExtensibleEnum {}
extension Components.Schemas.CapabilityType: OpenAPIExtensibleEnum {}
extension Components.Schemas.CertificateType: OpenAPIExtensibleEnum {}
extension Components.Schemas.Device.AttributesPayload.DeviceClassPayload: OpenAPIExtensibleEnum {}
@@ -0,0 +1,39 @@
import Foundation
import OpenAPIRuntime
import HTTPTypes
struct LoggingMiddleware: ClientMiddleware {
static let regex: NSRegularExpression? = {
guard let pat = ProcessInfo.processInfo.environment["XTL_DEV_LOG"] else { return nil }
return try? NSRegularExpression(pattern: pat)
}()
func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var (response, body) = try await next(request, body, baseURL)
guard Self.regex?.firstMatch(
in: operationID,
range: NSRange(operationID.startIndex..., in: operationID)
) != nil
else { return (response, body) }
print("\n\(operationID) response status -> \(response.status)")
if let unwrapped = body {
let data = try await Data(collecting: unwrapped, upTo: .max)
// body may only be consumable once, replace it with the collected data
body = .init(data)
let text = String(decoding: data, as: UTF8.self)
print("\(operationID) response body -> \(text)")
}
return (response, body)
}
}
@@ -0,0 +1,25 @@
import DeveloperAPI
// syntactic sugar to make it nicer to work with `anyOf:` types
public protocol OpenAPIExtensibleEnum {
associatedtype Value1Payload: RawRepresentable<String>, Codable, Hashable, Sendable, CaseIterable
var value1: Value1Payload? { get set }
var value2: String? { get set }
init(value1: Value1Payload?, value2: String?)
}
extension OpenAPIExtensibleEnum {
public var rawValue: String {
value2!
}
public init(_ value: Value1Payload) {
self.init(value1: value, value2: nil)
}
}
extension Components.Schemas.BundleIdPlatform: OpenAPIExtensibleEnum {}
extension Components.Schemas.CapabilityType: OpenAPIExtensibleEnum {}
extension Components.Schemas.CertificateType: OpenAPIExtensibleEnum {}
extension Components.Schemas.Device.AttributesPayload.DeviceClassPayload: OpenAPIExtensibleEnum {}
@@ -22,7 +22,15 @@ struct DSCertificatesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let certificates = try await client.certificatesGetCollection().ok.body.json.data
let certificates = try await DeveloperAPIPages {
try await client.certificatesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for certificate in certificates {
print("- id: \(certificate.id)")
guard let attributes = certificate.attributes else {
@@ -22,7 +22,15 @@ struct DSDevicesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let devices = try await client.devicesGetCollection().ok.body.json.data
let devices = try await DeveloperAPIPages {
try await client.devicesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for device in devices {
print("- id: \(device.id)")
guard let attributes = device.attributes else {
@@ -22,7 +22,15 @@ struct DSIdentifiersListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let bundleIDs = try await client.bundleIdsGetCollection().ok.body.json.data
let bundleIDs = try await DeveloperAPIPages {
try await client.bundleIdsGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for bundleID in bundleIDs {
print("- id: \(bundleID.id)")
guard let attributes = bundleID.attributes else {
@@ -24,7 +24,15 @@ struct DSProfilesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let profiles = try await client.profilesGetCollection().ok.body.json.data
let profiles = try await DeveloperAPIPages {
try await client.profilesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for profile in profiles {
print("- id: \(profile.id)")
guard let attributes = profile.attributes else {