diff --git a/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift b/Sources/XKit/DeveloperServices/DeveloperServicesProvisioningOperation.swift index 06547252..6d1d85e2 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( diff --git a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift new file mode 100644 index 00000000..1e2de2a3 --- /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 + } + +} diff --git a/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift b/Sources/XKit/DeveloperServices/Devices/DeveloperServicesFetchDeviceOperation.swift deleted file mode 100644 index 9f5eaf7d..00000000 --- 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 - } - -} diff --git a/Sources/XKit/Integration/IntegratedInstaller.swift b/Sources/XKit/Integration/IntegratedInstaller.swift index 7549d569..27546a0e 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 diff --git a/Sources/XKit/Signer/SigningContext.swift b/Sources/XKit/Signer/SigningContext.swift index 46cf5759..644f176f 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() }