Files
xtool-mirror/Sources/XKit/DeveloperServices/Devices/DeveloperServicesAddDeviceOperation.swift
Kabir Oberai 8d8ba6fb94 FetchDeviceOperation: account for pagination (#91)
- 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.
2025-05-25 03:56:19 +05:30

37 lines
1.1 KiB
Swift

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
}
}