Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Generics / Codable w/ API response 204 NO CONTENT

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()
    }
}
like image 752
Tim J Avatar asked Oct 17 '22 08:10

Tim J


1 Answers

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.

like image 152
nodediggity Avatar answered Nov 15 '22 12:11

nodediggity