有没有一种方法可以使用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()
}
答案 0 :(得分:2)
没有您的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()
}