mirror of
https://github.com/LukeHagar/plexswift.git
synced 2025-12-06 04:20:52 +00:00
61 lines
2.0 KiB
Swift
61 lines
2.0 KiB
Swift
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
|
|
import Foundation
|
|
|
|
struct Server {
|
|
public let urlString: String
|
|
}
|
|
|
|
protocol Servers {
|
|
static var urlStrings: [String] { get }
|
|
|
|
static func `default`() throws -> Server
|
|
static func server(at index: Int) throws -> Server
|
|
static func server(at index: Int, substituting parameters: [String: String]?) throws -> Server
|
|
}
|
|
|
|
protocol ServerConvertible {
|
|
func server() throws -> Server
|
|
}
|
|
|
|
enum ServerConversionError: Swift.Error {
|
|
case `internal`
|
|
case missingDefaultServer(serverType: String)
|
|
case invalidServerIndex(serverType: String, index: Int)
|
|
case missingServerParameterSubstitutionKey(_ key: String, serverString: String)
|
|
}
|
|
|
|
extension Servers {
|
|
static func `default`() throws -> Server {
|
|
guard urlStrings.count > 0 else {
|
|
throw ServerConversionError.missingDefaultServer(serverType: String(describing: self))
|
|
}
|
|
|
|
return Server(urlString: urlStrings[0])
|
|
}
|
|
|
|
static func server(at index: Int) throws -> Server {
|
|
return try server(at: index, substituting: nil)
|
|
}
|
|
|
|
static func server(at index: Int, substituting parameters: [String: String]?) throws -> Server {
|
|
guard index >= 0 && index < urlStrings.count else {
|
|
throw ServerConversionError.invalidServerIndex(serverType: String(describing: self), index: index)
|
|
}
|
|
if let parameters = parameters {
|
|
do {
|
|
return Server(urlString: try urlStrings[index].substituteComponents { key in parameters[key] })
|
|
} catch let error as StringSubstitutionError {
|
|
switch error {
|
|
case .internal:
|
|
throw ServerConversionError.internal
|
|
case .missingParameter(named: let name, in: let string):
|
|
throw ServerConversionError.missingServerParameterSubstitutionKey(name, serverString: string)
|
|
}
|
|
}
|
|
} else {
|
|
return Server(urlString: urlStrings[index])
|
|
}
|
|
}
|
|
}
|