From 021951fa8e6aa474a67e7232393d2e7028f0ec91 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sat, 24 May 2025 23:45:39 +0530 Subject: [PATCH] Support pagination --- .../{ => OpenAPI}/ASCKey.swift | 0 .../OpenAPI/DeveloperAPIPages.swift | 76 ++++++++++++ .../DeveloperServices+OpenAPI.swift | 113 ++++++++---------- .../OpenAPI/LoggingMiddleware.swift | 39 ++++++ .../OpenAPI/OpenAPIExtensibleEnum.swift | 25 ++++ .../DSCommands/DSCertificatesCommand.swift | 10 +- .../DSCommands/DSDevicesCommand.swift | 10 +- .../DSCommands/DSIdentifiersCommand.swift | 10 +- .../DSCommands/DSProfilesCommand.swift | 10 +- 9 files changed, 225 insertions(+), 68 deletions(-) rename Sources/XKit/DeveloperServices/{ => OpenAPI}/ASCKey.swift (100%) create mode 100644 Sources/XKit/DeveloperServices/OpenAPI/DeveloperAPIPages.swift rename Sources/XKit/DeveloperServices/{ => OpenAPI}/DeveloperServices+OpenAPI.swift (78%) create mode 100644 Sources/XKit/DeveloperServices/OpenAPI/LoggingMiddleware.swift create mode 100644 Sources/XKit/DeveloperServices/OpenAPI/OpenAPIExtensibleEnum.swift diff --git a/Sources/XKit/DeveloperServices/ASCKey.swift b/Sources/XKit/DeveloperServices/OpenAPI/ASCKey.swift similarity index 100% rename from Sources/XKit/DeveloperServices/ASCKey.swift rename to Sources/XKit/DeveloperServices/OpenAPI/ASCKey.swift diff --git a/Sources/XKit/DeveloperServices/OpenAPI/DeveloperAPIPages.swift b/Sources/XKit/DeveloperServices/OpenAPI/DeveloperAPIPages.swift new file mode 100644 index 00000000..6a70c464 --- /dev/null +++ b/Sources/XKit/DeveloperServices/OpenAPI/DeveloperAPIPages.swift @@ -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: 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 + } + } +} diff --git a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift b/Sources/XKit/DeveloperServices/OpenAPI/DeveloperServices+OpenAPI.swift similarity index 78% rename from Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift rename to Sources/XKit/DeveloperServices/OpenAPI/DeveloperServices+OpenAPI.swift index 69da496d..9ad78cdd 100644 --- a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift +++ b/Sources/XKit/DeveloperServices/OpenAPI/DeveloperServices+OpenAPI.swift @@ -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( + _ 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, 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 {} diff --git a/Sources/XKit/DeveloperServices/OpenAPI/LoggingMiddleware.swift b/Sources/XKit/DeveloperServices/OpenAPI/LoggingMiddleware.swift new file mode 100644 index 00000000..70b71879 --- /dev/null +++ b/Sources/XKit/DeveloperServices/OpenAPI/LoggingMiddleware.swift @@ -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) + } +} diff --git a/Sources/XKit/DeveloperServices/OpenAPI/OpenAPIExtensibleEnum.swift b/Sources/XKit/DeveloperServices/OpenAPI/OpenAPIExtensibleEnum.swift new file mode 100644 index 00000000..7a6f5867 --- /dev/null +++ b/Sources/XKit/DeveloperServices/OpenAPI/OpenAPIExtensibleEnum.swift @@ -0,0 +1,25 @@ +import DeveloperAPI + +// syntactic sugar to make it nicer to work with `anyOf:` types +public protocol OpenAPIExtensibleEnum { + associatedtype Value1Payload: RawRepresentable, 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 {} diff --git a/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift b/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift index 80e5dda5..569bd34a 100644 --- a/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift +++ b/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift @@ -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 { diff --git a/Sources/XToolSupport/DSCommands/DSDevicesCommand.swift b/Sources/XToolSupport/DSCommands/DSDevicesCommand.swift index 991b9542..87b7cc5f 100644 --- a/Sources/XToolSupport/DSCommands/DSDevicesCommand.swift +++ b/Sources/XToolSupport/DSCommands/DSDevicesCommand.swift @@ -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 { diff --git a/Sources/XToolSupport/DSCommands/DSIdentifiersCommand.swift b/Sources/XToolSupport/DSCommands/DSIdentifiersCommand.swift index 7d904cda..38150ffc 100644 --- a/Sources/XToolSupport/DSCommands/DSIdentifiersCommand.swift +++ b/Sources/XToolSupport/DSCommands/DSIdentifiersCommand.swift @@ -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 { diff --git a/Sources/XToolSupport/DSCommands/DSProfilesCommand.swift b/Sources/XToolSupport/DSCommands/DSProfilesCommand.swift index 90f66a02..b9f4556a 100644 --- a/Sources/XToolSupport/DSCommands/DSProfilesCommand.swift +++ b/Sources/XToolSupport/DSCommands/DSProfilesCommand.swift @@ -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 {