Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

DeveloperServices: support paginated requests (#89)

Kabir Oberai72db71c8711f
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 0000000..1964d15
--- /dev/null
+++ b/Sources/XKit/DeveloperServices/OpenAPI/DeveloperAPIPages.swift
@@ -0,0 +1,78 @@
+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? {
+ guard !Task.isCancelled else { return nil }
+
+ 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), next != cursor {
+ 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 77%
rename from Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift
rename to Sources/XKit/DeveloperServices/OpenAPI/DeveloperServices+OpenAPI.swift
index 69da496..ccd08ca 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,39 @@ 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 newOffset = components.queryItems?.first(where: { $0.name == "cursor" }),
+ let newLimit = components.queryItems?.first(where: { $0.name == "limit" })
+ else { throw Errors.badNextLink(link) }
+ // the next value will contain a cursor (offset) *and* a limit even if we didn't
+ // provide a limit in our initial request. we need to include the limit in subsequent
+ // requests, otherwise the cursor isn't respected.
+ cursor = [newOffset, newLimit]
+ } else {
+ cursor = []
+ }
+
+ return try await $cursor.withValue(cursor) {
+ try await action()
+ }
+ }
+
+ public enum Errors: Error {
+ case badNextLink(String)
+ }
}
public enum DeveloperAPIAuthData: Sendable {
@@ -126,9 +161,13 @@ public struct DeveloperAPIXcodeAuthMiddleware: ClientMiddleware {
let path = request.path ?? "/"
var components = URLComponents(string: path) ?? .init()
- components.queryItems = (components.queryItems ?? []) + [
+
+ var items = components.queryItems ?? []
+ items.upsertQueryItems(DeveloperAPIClient.cursor + [
URLQueryItem(name: "teamId", value: authData.teamID.rawValue)
- ]
+ ])
+ components.queryItems = items
+
let query = components.percentEncodedQuery ?? ""
components.query = nil
@@ -214,69 +253,28 @@ public struct DeveloperAPIASCAuthMiddleware: ClientMiddleware {
_ baseURL: URL
) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
- let jwt = try await generator.generate()
var request = request
- request.headerFields[.authorization] = "Bearer \(jwt)"
- 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 jwt = try await generator.generate()
+ request.headerFields[.authorization] = "Bearer \(jwt)"
- let text = String(decoding: data, as: UTF8.self)
- print("\(operationID) response body -> \(text)")
+ let cursor = DeveloperAPIClient.cursor
+ if !cursor.isEmpty {
+ var components = URLComponents(string: request.path ?? "") ?? .init()
+ var items = components.queryItems ?? []
+ items.upsertQueryItems(cursor)
+ components.queryItems = items
+ request.path = components.string
}
- return (response, body)
+ return try await next(request, body, baseURL)
}
}
-// 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 [URLQueryItem] {
+ fileprivate mutating func upsertQueryItems(_ items: [URLQueryItem]) {
+ let newNames = Set(items.map(\.name))
+ removeAll { newNames.contains($0.name) }
+ append(contentsOf: items)
}
}
-
-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 0000000..70b7187
--- /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 0000000..7a6f586
--- /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<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 {}
diff --git a/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift b/Sources/XToolSupport/DSCommands/DSCertificatesCommand.swift
index 80e5dda..569bd34 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 991b954..87b7cc5 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 7d904cd..38150ff 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 90f66a0..b9f4556 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 {