Download this file
| 1 | import Foundation |
| 2 | |
| 3 | public struct NativeResponse: Sendable { |
| 4 | public let data: Data |
| 5 | public let status: Int |
| 6 | public let headers: [String: String] |
| 7 | |
| 8 | /// Header names are stored lowercased so that `header(_:)` is case-insensitive. |
| 9 | public init(data: Data, status: Int, headers: [String: String] = [:]) { |
| 10 | self.data = data |
| 11 | self.status = status |
| 12 | var lowered: [String: String] = [:] |
| 13 | for (key, value) in headers { lowered[key.lowercased()] = value } |
| 14 | self.headers = lowered |
| 15 | } |
| 16 | |
| 17 | public func header(_ name: String) -> String? { headers[name.lowercased()] } |
| 18 | |
| 19 | /// The typed failure this status means, or nil for 2xx. |
| 20 | public var failure: NativeClientError? { NativeClientError.forStatus(status, body: data, headers: headers) } |
| 21 | |
| 22 | /// Throws the typed case for any status outside 2xx, so a caller that |
| 23 | /// compared codes by hand can hand the decision to one mapping. |
| 24 | @discardableResult |
| 25 | public func requireSuccess() throws -> NativeResponse { |
| 26 | if let failure { throw failure } |
| 27 | return self |
| 28 | } |
| 29 | } |
| 30 | |