Modern Apps git

← xtool.git

FetchDeviceOperation: account for pagination (#91)

Commit
8d8ba6fb94ee9c66642f9d5c34939b21ac505cdf
Author
Kabir Oberai <[email protected]>
When
2025-05-24 22:26 UTC (1 year ago)
Parents
72db71c
- Always try to register the device and just return gracefully if it
fails (prevents races, pagination issues)
- Allow `SigningContext` (and therefore `AutoSigner`) to be created
without a target device: just skip device registration.

Diff

Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift
diff --git a/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift b/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swiftindex 0654725..6d1d85e 100644--- a/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift+++ b/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift@@ -39,7 +39,7 @@ public struct DeveloperServicesProvisioningOperation: DeveloperServicesOperation      public func perform() async throws -> Response {         progress(0/3)-        _ = try await DeveloperServicesFetchDeviceOperation(context: context).perform()+        try await DeveloperServicesAddDeviceOperation(context: context).perform()          progress(1/3)         let signingInfo = try await DeveloperServicesFetchCertificateOperation(
Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift
diff --git a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swiftnew file mode 100644index 0000000..1e2de2a--- /dev/null+++ b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift@@ -0,0 +1,36 @@+import Foundation+import DeveloperAPI++public struct DeveloperServicesAddDeviceOperation: DeveloperServicesOperation {+    public let context: SigningContext+    public init(context: SigningContext) {+        self.context = context+    }++    public func perform() async throws {+        guard let targetDevice = context.targetDevice else { return }++        // try to register the device+        let response = try await context.developerAPIClient.devicesCreateInstance(+            body: .json(.init(data: .init(+                _type: .devices,+                attributes: .init(+                    name: targetDevice.name,+                    platform: .init(.ios),+                    udid: targetDevice.udid+                )+            )))+        )++        // we get a 409 CONFLICT if the device was already registered.+        // handle this by returning gracefully.+        if (try? response.conflict) != nil {+            return+        }++        // otherwise, we should get a 201 CREATED to indicate that the device+        // was added. any other case is unexpected, and this will throw.+        _ = try response.created+    }++}
Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift
diff --git a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swiftdeleted file mode 100644index 9f5eaf7..0000000--- a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift+++ /dev/null@@ -1,39 +0,0 @@-//-//  DeveloperServicesFetchDeviceOperation.swift-//  XKit-//-//  Created by Kabir Oberai on 13/10/19.-//  Copyright © 2019 Kabir Oberai. All rights reserved.-//--import Foundation-import DeveloperAPI--public struct DeveloperServicesFetchDeviceOperation: DeveloperServicesOperation {-    public let context: SigningContext-    public init(context: SigningContext) {-        self.context = context-    }--    public func perform() async throws -> Components.Schemas.Device {-        let devices = try await context.developerAPIClient.devicesGetCollection().ok.body.json.data--        if let device = devices.first(where: { $0.attributes?.udid == self.context.udid }) {-            return device-        }--        let response = try await context.developerAPIClient.devicesCreateInstance(-            body: .json(.init(data: .init(-                _type: .devices,-                attributes: .init(-                    name: self.context.deviceName,-                    platform: .init(.ios),-                    udid: self.context.udid-                )-            )))-        )--        return try response.created.body.json.data-    }--}
Sources/XKit/Integration/IntegratedInstaller.swift
diff --git a/Sources/XKit/Integration/IntegratedInstaller.swift b/Sources/XKit/Integration/IntegratedInstaller.swiftindex 7549d56..27546a0 100644--- a/Sources/XKit/Integration/IntegratedInstaller.swift+++ b/Sources/XKit/Integration/IntegratedInstaller.swift@@ -270,9 +270,8 @@ public actor IntegratedInstaller {         try await updateProgress(to: 1)          let context = try SigningContext(-            udid: udid,-            deviceName: deviceName,-            auth: auth+            auth: auth,+            targetDevice: .init(udid: udid, name: deviceName)         )          let signer = AutoSigner(context: context) { certs in
Sources/XKit/Signer/SigningContext.swift
diff --git a/Sources/XKit/Signer/SigningContext.swift b/Sources/XKit/Signer/SigningContext.swiftindex 46cf575..644f176 100644--- a/Sources/XKit/Signer/SigningContext.swift+++ b/Sources/XKit/Signer/SigningContext.swift@@ -11,24 +11,26 @@ import DeveloperAPI  public struct SigningContext: Sendable { -    public let udid: String-    public let deviceName: String-    public let auth: DeveloperAPIAuthData-    public let signer: Signer+    public struct TargetDevice: Sendable, Hashable {+        public var udid: String+        public var name: String+    }++    public var auth: DeveloperAPIAuthData+    public var targetDevice: TargetDevice?+    public var signer: Signer      public var developerAPIClient: DeveloperAPIClient {         DeveloperAPIClient(auth: auth)     }      public init(-        udid: String,-        deviceName: String,         auth: DeveloperAPIAuthData,+        targetDevice: TargetDevice? = nil,         signer: Signer? = nil     ) throws {-        self.udid = udid-        self.deviceName = deviceName         self.auth = auth+        self.targetDevice = targetDevice         self.signer = try signer ?? .first()     }