I am using generics and codable with URLSession
.
When I receive a response from an API, I check the status is in the 200 - 299 range and decode the data like so
guard let data = data, let value = try? JSONDecoder().decode(T.self, from: data) else {
return completion(.error("Could not decode JSON response"))
}
completion(.success(value))
This is then passed off to the completion handler and everything is OK.
I have a new endpoint I must POST too however, this endpoint returns a 204 with no content body.
As such, I cannot decode the response, simply as I cannot pass in a type?
My completion handler expects
enum Either<T> {
case success(T)
case error(String?)
}
and switching on my response statusCode like so
case 204:
let value = String(stringLiteral: "no content")
return completion(.success(value))
produces an error of
Member 'success' in 'Either<>' produces result of type 'Either', but context expects 'Either<>'
My APIClient is
protocol APIClientProtocol: class {
var task: URLSessionDataTask { get set }
var session: SessionProtocol { get }
func call<T: Codable>(with request: URLRequest, completion: @escaping (Either<T>) -> Void) -> Void
func requestRefreshToken<T: Codable>(forRequest failedRequest: URLRequest, completion: @escaping (Either<T>) -> Void) -> Void
}
extension APIClientProtocol {
func call<T: Codable>(with request: URLRequest, completion: @escaping (Either<T>) -> Void) -> Void {
task = session.dataTask(with: request, completionHandler: { [weak self] data, response, error in
guard error == nil else {
return completion(.error("An unknown error occured with the remote service"))
}
guard let response = response as? HTTPURLResponse else {
return completion(.error("Invalid HTTPURLResponse recieved"))
}
switch response.statusCode {
case 100...299:
guard let data = data else { return completion(.error("No data in response")) }
guard let value = try? JSONDecoder().decode(T.self, from: data) else { return completion(.error("Could not decode the JSON response")) }
completion(.success(value))
case 300...399: return completion(.error(HTTPResponseStatus.redirection.rawValue))
case 400: return completion(.error(HTTPResponseStatus.clientError.rawValue))
case 401: self?.requestRefreshToken(forRequest: request, completion: completion)
case 402...499: return completion(.error(HTTPResponseStatus.clientError.rawValue))
case 500...599: return completion(.error(HTTPResponseStatus.serverError.rawValue))
default:
return completion(.error("Request failed with a status code of \(response.statusCode)"))
}
})
task.resume()
}
}
Make your Either
enum success type optional
enum Either<T> {
case success(T?)
case error(String)
}
Create a case for a 204
response status, passing nil
case 204:
completion(.success(nil))
You can then use a typealias
and create something generic like
typealias NoContentResponse = Either<Bool>
You should then be able to switch on NoContentResponse
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With