func fetchData(from endpoint:ScrapeAPI )->AnyPublisher<T, APIError>{
return AF.request(endpoint.mockurl,
method: .post,
parameters: endpoint.parameters,
encoder: JSONParameterEncoder.prettyPrinted,
headers: endpoint.headers)
.validate()
.publishDecodable(type:T.self)
.value()
.mapError{_ in APIError.unknown}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
enum APIError:Error {
case decodingError
case errorCOde(Int)
case unknown
}
extension APIError: LocalizedError{
var errorDescription: String? {
switch self {
case .decodingError:
return "Failed to decode the object from the service"
case .errorCOde(let code ):
return "\(code)- Something went wrong"
case .unknown:
return "the error is unknown"
}
}
}
var subscriptions = Set<AnyCancellable>()
fetchData(from:ScrapeAPI.getScrape).sink(
receiveCompletion: { completion in
switch completion {
case .failure(let error):
print(error)
case .finished:
print("Success")
}
},
receiveValue: { value in
print(value)}
).store(in: &subscriptions)
This prints error "unknown". It reaches the API and API responds with a body. It seems it is not getting published to T. Interestingly, Alamofire .responseJSON works. Ideally, I would like to verify T has new JSON data from API. How do I go about it?