import Foundation public struct NativeResponse: Sendable { public let data: Data public let status: Int public let headers: [String: String] /// Header names are stored lowercased so that `header(_:)` is case-insensitive. public init(data: Data, status: Int, headers: [String: String] = [:]) { self.data = data self.status = status var lowered: [String: String] = [:] for (key, value) in headers { lowered[key.lowercased()] = value } self.headers = lowered } public func header(_ name: String) -> String? { headers[name.lowercased()] } /// The typed failure this status means, or nil for 2xx. public var failure: NativeClientError? { NativeClientError.forStatus(status, body: data, headers: headers) } /// Throws the typed case for any status outside 2xx, so a caller that /// compared codes by hand can hand the decision to one mapping. @discardableResult public func requireSuccess() throws -> NativeResponse { if let failure { throw failure } return self } }