Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jan 15, 2024
1 parent 410c979 commit aa845b7
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 122 deletions.
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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: [
Expand Down
32 changes: 32 additions & 0 deletions Sources/IPSWDownloads/Board.swift
Original file line number Diff line number Diff line change
@@ -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
)
}
}
32 changes: 32 additions & 0 deletions Sources/IPSWDownloads/Device.swift
Original file line number Diff line number Diff line change
@@ -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:))
)
}
}
61 changes: 61 additions & 0 deletions Sources/IPSWDownloads/Firmware.swift
Original file line number Diff line number Diff line change
@@ -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
)
}
}
12 changes: 12 additions & 0 deletions Sources/IPSWDownloads/FirmwareType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Leo Dion on 1/11/24.
//
import Foundation

public enum FirmwareType: String {
case ipsw
case ota
}
132 changes: 13 additions & 119 deletions Sources/IPSWDownloads/IPSWDownloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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,
Expand All @@ -140,13 +32,15 @@ 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)
)
let device = try await underlyingClient.firmwaresForDevice(input).ok.body.json
return try Device(component: device)
}

}
11 changes: 11 additions & 0 deletions Sources/IPSWDownloads/RuntimeError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// File.swift
//
//
// Created by Leo Dion on 1/11/24.
//
import Foundation

enum RuntimeError: Error {
case invalidURL(String)
}
17 changes: 17 additions & 0 deletions Sources/IPSWDownloads/URL.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit aa845b7

Please sign in to comment.