Skip to content

Commit

Permalink
cleaning up api
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jan 13, 2024
1 parent 1a2e82c commit 1a7ec37
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 37 deletions.
156 changes: 127 additions & 29 deletions Sources/IPSWDownloads/IPSWDownloads.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,152 @@
//
// File.swift
//
//
//
// Created by Leo Dion on 1/11/24.
//
import OpenAPIRuntime
import Foundation
import OpenAPIRuntime

enum FirmwareType : String {
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()

/// The underlying generated client to make HTTP requests to GreetingService.
private let underlyingClient: any APIProtocol
/// The underlying generated client to make HTTP requests to GreetingService.
private let underlyingClient: any APIProtocol
//
/// 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 }
/// 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) {
self.init(
underlyingClient: Client(
serverURL: serverURL,
transport: transport
)
)
}
func firmwaresForDevice(withIdentifier identifier: String, type: FirmwareType) async throws {
self.init(
underlyingClient: Client(
serverURL: serverURL,
transport: transport
)
)
}

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 firmwares = try await self.underlyingClient.firmwaresForDevice(input)
//self.underlyingClient.firmwaresForDevice(Operations.firmwaresForDevice.Input)
let device = try await underlyingClient.firmwaresForDevice(input).ok.body.json
return try Device(component: device)
}
//
// /// Fetches the customized greeting for the provided name.
// /// - Parameter name: The name for which to provide a greeting, or nil to get a default.
// /// - Returns: A customized greeting message.
// /// - Throws: An error if the underlying HTTP client fails.
// public func getGreeting(name: String?) async throws -> String {
// let response = try await underlyingClient.getGreeting(query: .init(name: name))
// return try response.ok.body.json.message
// }

}
16 changes: 8 additions & 8 deletions Sources/IPSWDownloads/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5562,7 +5562,7 @@ components:
type: object
properties:
status:
type: number
type: integer
message:
type: string
ReleaseDate:
Expand All @@ -5588,7 +5588,7 @@ components:
type: string
format: date-time
count:
type: number
type: integer
type:
type: string
required:
Expand All @@ -5608,7 +5608,7 @@ components:
url:
type: string
filesize:
type: number
type: integer
prerequisitebuildid:
type: string
prerequisiteversion:
Expand Down Expand Up @@ -5728,9 +5728,9 @@ components:
platform:
type: string
cpid:
type: number
type: integer
bdid:
type: number
type: integer
required:
- boardconfig
- platform
Expand Down Expand Up @@ -5761,7 +5761,7 @@ components:
md5sum:
type: string
filesize:
type: number
type: integer
url:
type: string
format: uri
Expand Down Expand Up @@ -5798,9 +5798,9 @@ components:
platform:
type: string
cpid:
type: number
type: integer
bdid:
type: number
type: integer
tags:
- name: Api

0 comments on commit 1a7ec37

Please sign in to comment.