mirror of
https://github.com/hanzoai/swift-sdk.git
synced 2026-10-30 18:20:19 +00:00
The forge tree held the package scaffolding and none of the generated sources — 2,435 files of APIs, Models and Infrastructure, including BaseAPI, were simply absent, so anything depending on this copy had a package that compiled to nothing usable. Generated from the current API document rather than reconciled with the other copy: a client is a function of the spec, so regenerating answers "which history wins" without anyone having to choose. .spec-lock records which document, so the next reader can tell.
137 lines
4.9 KiB
Swift
137 lines
4.9 KiB
Swift
// APIs.swift
|
|
//
|
|
// Generated by openapi-generator
|
|
// https://openapi-generator.tech
|
|
//
|
|
|
|
import Foundation
|
|
#if canImport(FoundationNetworking)
|
|
import FoundationNetworking
|
|
#endif
|
|
|
|
open class HanzoAPIConfiguration: @unchecked Sendable {
|
|
public var basePath: String
|
|
public var customHeaders: [String: String]
|
|
public var credential: URLCredential?
|
|
public var requestBuilderFactory: RequestBuilderFactory
|
|
public var apiResponseQueue: DispatchQueue
|
|
public var codableHelper: CodableHelper
|
|
|
|
/// Configures the range of HTTP status codes that will result in a successful response
|
|
///
|
|
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
|
|
public var successfulStatusCodeRange: Range<Int>
|
|
|
|
public var interceptor: OpenAPIInterceptor
|
|
|
|
public init(
|
|
basePath: String = "https://api.hanzo.ai",
|
|
customHeaders: [String: String] = [:],
|
|
credential: URLCredential? = nil,
|
|
requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory(),
|
|
apiResponseQueue: DispatchQueue = .main,
|
|
codableHelper: CodableHelper = CodableHelper(),
|
|
successfulStatusCodeRange: Range<Int> = 200..<300,
|
|
interceptor: OpenAPIInterceptor = DefaultOpenAPIInterceptor()
|
|
) {
|
|
self.basePath = basePath
|
|
self.customHeaders = customHeaders
|
|
self.credential = credential
|
|
self.requestBuilderFactory = requestBuilderFactory
|
|
self.apiResponseQueue = apiResponseQueue
|
|
self.codableHelper = codableHelper
|
|
self.successfulStatusCodeRange = successfulStatusCodeRange
|
|
self.interceptor = interceptor
|
|
}
|
|
|
|
public static let shared = HanzoAPIConfiguration()
|
|
}
|
|
|
|
open class RequestBuilder<T>: @unchecked Sendable, Identifiable {
|
|
public var credential: URLCredential?
|
|
public var headers: [String: String]
|
|
public let parameters: [String: any Sendable]?
|
|
public let method: String
|
|
public let URLString: String
|
|
public let requestTask: RequestTask = RequestTask()
|
|
public let requiresAuthentication: Bool
|
|
public let apiConfiguration: HanzoAPIConfiguration
|
|
|
|
/// Optional block to obtain a reference to the request's progress instance when available.
|
|
public var onProgressReady: ((Progress) -> Void)?
|
|
|
|
required public init(method: String, URLString: String, parameters: [String: any Sendable]?, headers: [String: String] = [:], requiresAuthentication: Bool, apiConfiguration: HanzoAPIConfiguration = HanzoAPIConfiguration.shared) {
|
|
self.method = method
|
|
self.URLString = URLString
|
|
self.parameters = parameters
|
|
self.headers = headers
|
|
self.requiresAuthentication = requiresAuthentication
|
|
self.apiConfiguration = apiConfiguration
|
|
|
|
addHeaders(apiConfiguration.customHeaders)
|
|
addCredential()
|
|
}
|
|
|
|
open func addHeaders(_ aHeaders: [String: String]) {
|
|
for (header, value) in aHeaders {
|
|
headers[header] = value
|
|
}
|
|
}
|
|
|
|
@discardableResult
|
|
open func execute(completion: @Sendable @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
|
|
return requestTask
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
|
|
@discardableResult
|
|
open func execute() async throws(ErrorResponse) -> Response<T> {
|
|
do {
|
|
let requestTask = self.requestTask
|
|
return try await withTaskCancellationHandler {
|
|
try Task.checkCancellation()
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
guard !Task.isCancelled else {
|
|
continuation.resume(throwing: CancellationError())
|
|
return
|
|
}
|
|
|
|
self.execute { result in
|
|
switch result {
|
|
case let .success(response):
|
|
nonisolated(unsafe) let response = response
|
|
continuation.resume(returning: response)
|
|
case let .failure(error):
|
|
continuation.resume(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
} onCancel: {
|
|
requestTask.cancel()
|
|
}
|
|
} catch {
|
|
if let errorResponse = error as? ErrorResponse {
|
|
throw errorResponse
|
|
} else {
|
|
throw ErrorResponse.error(-3, nil, nil, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
public func addHeader(name: String, value: String) -> Self {
|
|
if !value.isEmpty {
|
|
headers[name] = value
|
|
}
|
|
return self
|
|
}
|
|
|
|
open func addCredential() {
|
|
credential = apiConfiguration.credential
|
|
}
|
|
}
|
|
|
|
public protocol RequestBuilderFactory {
|
|
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
|
|
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
|
|
}
|