gpt4 book ai didi

swift - 在 URLRequest 中使用 Combine 将对象编码为 httpBody

转载 作者:行者123 更新时间:2023-11-28 11:24:56 25 4
gpt4 key购买 nike

有没有一种方法可以使用 Combine 将对象编码为 urlRequest.httpBody 或从解码为具有特定错误类型的 AnyPublisher 返回错误。

我无法让它工作,而且似乎没有使用 combine 编码对象的代码示例。必须强制将编码错误转换为 AnyPublisher 似乎不正确/不安全。

谢谢

func create(object: ExampleObject, token: Token) -> AnyPublisher<ExampleObject, API.Error> {

let url = API.EndPoint.players.url
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
do {
// is there a safe combine way to encode this
urlRequest.httpBody = try JSONEncoder().encode(object)
} catch {
// Is there a better way to handle this
return error as! AnyPublisher<ExampleObject, API.Error>
}

return session
.dataTaskPublisher(for: urlRequest)
.map(\.data)
.decode(type: ExampleObject.self, decoder: JSONDecoder())
.mapError { error in
switch error {
case is URLError:
return API.Error.addressUnreachable
default:
return API.Error.invalidResponse
}
}
.eraseToAnyPublisher()
}

最佳答案

没有你的 API.Error 枚举,所以我创建了一个简单的枚举,例如

enum APIError: Error {
case encode(EncodingError)
case request(URLError)
case decode(DecodingError)
case unknown
}
func create<ExampleObject>(object: ExampleObject, token: String) -> AnyPublisher<ExampleObject, APIError> where ExampleObject: Codable {
return Just(object)
.encode(encoder: JSONEncoder())
.mapError { error -> APIError in
if let encodingError = error as? EncodingError {
return .encode(encodingError)
} else {
return .unknown
}
}
.map { data -> URLRequest in
var urlRequest = URLRequest(url: API.EndPoint.players.url)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
urlRequest.httpBody = data
return urlRequest
}.flatMap {
URLSession.shared.dataTaskPublisher(for: $0)
.mapError(APIError.request)
.map(\.data)
.decode(type: ExampleObject.self, decoder: JSONDecoder())
.mapError { error -> APIError in
if let decodingError = error as? DecodingError {
return .decode(decodingError)
} else {
return .unknown
}
}
}
.eraseToAnyPublisher()
}

关于swift - 在 URLRequest 中使用 Combine 将对象编码为 httpBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com