From aa845b7b8bdb02436ce2230ae0aff4908b900384 Mon Sep 17 00:00:00 2001 From: Leo Dion Date: Sat, 13 Jan 2024 21:57:14 -0500 Subject: [PATCH] refactoring --- Package.swift | 3 - Sources/IPSWDownloads/Board.swift | 32 ++++++ Sources/IPSWDownloads/Device.swift | 32 ++++++ Sources/IPSWDownloads/Firmware.swift | 61 ++++++++++ Sources/IPSWDownloads/FirmwareType.swift | 12 ++ Sources/IPSWDownloads/IPSWDownloads.swift | 132 +++------------------- Sources/IPSWDownloads/RuntimeError.swift | 11 ++ Sources/IPSWDownloads/URL.swift | 17 +++ 8 files changed, 178 insertions(+), 122 deletions(-) create mode 100644 Sources/IPSWDownloads/Board.swift create mode 100644 Sources/IPSWDownloads/Device.swift create mode 100644 Sources/IPSWDownloads/Firmware.swift create mode 100644 Sources/IPSWDownloads/FirmwareType.swift create mode 100644 Sources/IPSWDownloads/RuntimeError.swift create mode 100644 Sources/IPSWDownloads/URL.swift diff --git a/Package.swift b/Package.swift index 28619d2..3c7f25a 100644 --- a/Package.swift +++ b/Package.swift @@ -1,5 +1,4 @@ // swift-tools-version: 5.9 -// The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -15,8 +14,6 @@ let package = Package( .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0") ], targets: [ - // Targets are the basic building blocks of a package, defining a module or a test suite. - // Targets can depend on other targets in this package and products from dependencies. .target( name: "IPSWDownloads", dependencies: [ diff --git a/Sources/IPSWDownloads/Board.swift b/Sources/IPSWDownloads/Board.swift new file mode 100644 index 0000000..96eb731 --- /dev/null +++ b/Sources/IPSWDownloads/Board.swift @@ -0,0 +1,32 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +public struct Board { + public let boardconfig: String + public let platform: String + public let cpid: Int + public let bdid: Int + + public init(boardconfig: String, platform: String, cpid: Int, bdid: Int) { + self.boardconfig = boardconfig + self.platform = platform + self.cpid = cpid + self.bdid = bdid + } +} + +extension Board { + init(component: Components.Schemas.Board) { + self.init( + boardconfig: component.boardconfig, + platform: component.platform, + cpid: component.cpid, + bdid: component.bdid + ) + } +} diff --git a/Sources/IPSWDownloads/Device.swift b/Sources/IPSWDownloads/Device.swift new file mode 100644 index 0000000..5e6cc4c --- /dev/null +++ b/Sources/IPSWDownloads/Device.swift @@ -0,0 +1,32 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +public struct Device { + public let name: String + public let identifier: String + public let firmwares: [Firmware] + public let boards: [Board] + + public init(name: String, identifier: String, firmwares: [Firmware], boards: [Board]) { + self.name = name + self.identifier = identifier + self.firmwares = firmwares + self.boards = boards + } +} + +extension Device { + init(component: Components.Schemas.Device) throws { + try self.init( + name: component.name, + identifier: component.identifier, + firmwares: component.firmwares.map(Firmware.init(component:)), + boards: component.boards.map(Board.init(component:)) + ) + } +} diff --git a/Sources/IPSWDownloads/Firmware.swift b/Sources/IPSWDownloads/Firmware.swift new file mode 100644 index 0000000..66406e4 --- /dev/null +++ b/Sources/IPSWDownloads/Firmware.swift @@ -0,0 +1,61 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +public struct Firmware { + public let identifier: String + public let version: String + public let buildid: String + public let sha1sum: String + public let md5sum: String + public let filesize: Int + public let url: URL + public let releasedate: Date + public let uploaddate: Date + public let signed: Bool + + public init( + identifier: String, + version: String, + buildid: String, + sha1sum: String, + md5sum: String, + filesize: Int, + url: URL, + releasedate: Date, + uploaddate: Date, + signed: Bool + ) { + self.identifier = identifier + self.version = version + self.buildid = buildid + self.sha1sum = sha1sum + self.md5sum = md5sum + self.filesize = filesize + self.url = url + self.releasedate = releasedate + self.uploaddate = uploaddate + self.signed = signed + } +} + +extension Firmware { + init(component: Components.Schemas.Firmware) throws { + try self.init( + identifier: component.identifier, + version: component.version, + buildid: component.buildid, + sha1sum: component.sha1sum, + md5sum: component.md5sum, + filesize: component.filesize, + url: URL(validatingURL: component.url), + releasedate: component.releasedate, + uploaddate: component.uploaddate, + signed: component.signed + ) + } +} diff --git a/Sources/IPSWDownloads/FirmwareType.swift b/Sources/IPSWDownloads/FirmwareType.swift new file mode 100644 index 0000000..21a3142 --- /dev/null +++ b/Sources/IPSWDownloads/FirmwareType.swift @@ -0,0 +1,12 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +public enum FirmwareType: String { + case ipsw + case ota +} diff --git a/Sources/IPSWDownloads/IPSWDownloads.swift b/Sources/IPSWDownloads/IPSWDownloads.swift index 5c072bd..db0dcda 100644 --- a/Sources/IPSWDownloads/IPSWDownloads.swift +++ b/Sources/IPSWDownloads/IPSWDownloads.swift @@ -7,119 +7,6 @@ import Foundation import OpenAPIRuntime -public enum FirmwareType: String { - case ipsw - case ota -} - -enum RuntimeError: Error { - case invalidURL(String) -} - -extension URL { - /// Returns a validated server URL, or throws an error. - /// - Parameter string: A URL string. - /// - Throws: If the provided string doesn't convert to URL. - public init(validatingURL string: String) throws { - guard let url = Self(string: string) else { throw RuntimeError.invalidURL(string) } - self = url - } -} - -public struct Firmware { - public init(identifier: String, version: String, buildid: String, sha1sum: String, md5sum: String, filesize: Int, url: URL, releasedate: Date, uploaddate: Date, signed: Bool) { - self.identifier = identifier - self.version = version - self.buildid = buildid - self.sha1sum = sha1sum - self.md5sum = md5sum - self.filesize = filesize - self.url = url - self.releasedate = releasedate - self.uploaddate = uploaddate - self.signed = signed - } - - public let identifier: String - public let version: String - public let buildid: String - public let sha1sum: String - public let md5sum: String - public let filesize: Int - public let url: URL - public let releasedate: Date - public let uploaddate: Date - public let signed: Bool -} - -public struct Board { - public init(boardconfig: String, platform: String, cpid: Int, bdid: Int) { - self.boardconfig = boardconfig - self.platform = platform - self.cpid = cpid - self.bdid = bdid - } - - public var boardconfig: String - public var platform: String - public var cpid: Int - public var bdid: Int -} - -public struct Device { - public init(name: String, identifier: String, firmwares: [Firmware], boards: [Board]) { - self.name = name - self.identifier = identifier - self.firmwares = firmwares - self.boards = boards - } - - public var name: String - public var identifier: String - public var firmwares: [Firmware] - public var boards: [Board] -} - -extension Firmware { - init(component: Components.Schemas.Firmware) throws { - try self.init( - identifier: component.identifier, - version: component.version, - buildid: component.buildid, - sha1sum: component.sha1sum, - md5sum: component.md5sum, - filesize: component.filesize, - url: URL(validatingURL: component.url), - releasedate: component.releasedate, - uploaddate: component.uploaddate, - signed: component.signed - ) - } -} - -extension Board { - init(component: Components.Schemas.Board) { - self.init( - boardconfig: component.boardconfig, - platform: component.platform, - cpid: component.cpid, - bdid: component.bdid - ) - } -} - -extension Device { - init(component: Components.Schemas.Device) throws { - try self.init( - name: component.name, - identifier: component.identifier, - firmwares: component.firmwares.map(Firmware.init(component:)), - boards: component.boards.map(Board.init(component:)) - ) - } -} - -/// A hand-written Swift API for the greeting service, one that doesn't leak any generated code. public struct IPSWDownloads { public static let serverURL = try! Servers.server1() @@ -128,10 +15,15 @@ public struct IPSWDownloads { // /// An internal initializer used by other initializers and by tests. /// - Parameter underlyingClient: The client to use to make HTTP requests. - private init(underlyingClient: any APIProtocol) { self.underlyingClient = underlyingClient } -// -// /// Creates a new client for GreetingService. - public init(serverURL: URL = Self.serverURL, transport: any ClientTransport) { + private init(underlyingClient: any APIProtocol) { + self.underlyingClient = underlyingClient + } + + /// Creates a new client for GreetingService. + public init( + transport: any ClientTransport, + serverURL: URL = Self.serverURL + ) { self.init( underlyingClient: Client( serverURL: serverURL, @@ -140,7 +32,10 @@ public struct IPSWDownloads { ) } - func device(withIdentifier identifier: String, type: FirmwareType) async throws -> Device { + public func device( + withIdentifier identifier: String, + type: FirmwareType + ) async throws -> Device { let input = Operations.firmwaresForDevice.Input( path: .init(identifier: identifier), query: .init(_type: type.rawValue) @@ -148,5 +43,4 @@ public struct IPSWDownloads { let device = try await underlyingClient.firmwaresForDevice(input).ok.body.json return try Device(component: device) } - } diff --git a/Sources/IPSWDownloads/RuntimeError.swift b/Sources/IPSWDownloads/RuntimeError.swift new file mode 100644 index 0000000..25d5d12 --- /dev/null +++ b/Sources/IPSWDownloads/RuntimeError.swift @@ -0,0 +1,11 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +enum RuntimeError: Error { + case invalidURL(String) +} diff --git a/Sources/IPSWDownloads/URL.swift b/Sources/IPSWDownloads/URL.swift new file mode 100644 index 0000000..855ef01 --- /dev/null +++ b/Sources/IPSWDownloads/URL.swift @@ -0,0 +1,17 @@ +// +// File.swift +// +// +// Created by Leo Dion on 1/11/24. +// +import Foundation + +extension URL { + /// Returns a validated server URL, or throws an error. + /// - Parameter string: A URL string. + /// - Throws: If the provided string doesn't convert to URL. + public init(validatingURL string: String) throws { + guard let url = Self(string: string) else { throw RuntimeError.invalidURL(string) } + self = url + } +}