Search code examples
apidata-structuresswiftuicombinepublisher

SwiftUI Combine Publisher targetstruct for decoding data


I have some difficulty using Combine in SwiftUI with making an API request and then decoding the data and returning it. When calling the API Service, it states in the 'AnyPublisher<UserLoginResponse, APIError>' that the result will be of such type. However, I would want to reuse the API Service and decode the response to different model structures. How can I call the API Service while defining which data structure it has to decode the returned data to? For example, in another ViewModel I would want to decode the API data to a 'NewsUpdatesResponse' instead of 'UserLoginResponse'. The code I have now is as follows:

Most code comes from: tundsdev

API Service

struct APIService {

func request(from endpoint: APIRequest, body: String) -> AnyPublisher<UserLoginResponse, APIError> {
    
    var request = endpoint.urlRequest
    request.httpMethod = endpoint.method
    
    if endpoint.authenticated == true {
        request.setValue("testToken", forHTTPHeaderField: "token")
    }
    if body != "" {
        let finalBody = body.data(using: .utf8)
        request.httpBody = finalBody
    }
    
    return URLSession
        .shared
        .dataTaskPublisher(for: request)
        .receive(on: DispatchQueue.main)
        .mapError { _ in APIError.unknown}
        .flatMap { data, response -> AnyPublisher<UserLoginResponse, APIError> in
            
            guard let response = response as? HTTPURLResponse else {
                return Fail(error: APIError.unknown).eraseToAnyPublisher()
            }
            
            print(response.statusCode)
            
            if response.statusCode == 200 {
                let jsonDecoder = JSONDecoder()
                
                return Just(data)
                    .decode(type: UserLoginResponse.self, decoder: jsonDecoder)
                    .mapError { _ in APIError.decodingError }
                    .eraseToAnyPublisher()
            }
            else {
                return Fail(error: APIError.errorCode(response.statusCode)).eraseToAnyPublisher()
            }
        }
        .eraseToAnyPublisher()
    }
}

Login ViewModel

class LoginViewModel: ObservableObject {

@Published var loginState: ResultState = .loading

private var cancellables = Set<AnyCancellable>()
private let service: APIService

init(service: APIService) {
    self.service = service
}

func login(username: String, password: String) {
    
    self.loginState = .loading
    
    let cancellable = service
        .request(from: .login, body: "username=admin&password=admin")
        .sink { res in
            print(res)
            switch res {
            case .finished:
                self.loginState = .success
            case .failure(let error):
                self.loginState = .failed(error: error)
            }
        } receiveValue: { response in
            print(response)
        }
    
    self.cancellables.insert(cancellable)
    }
}

Solution

  • the following is untested, but you could try something along this line, using generic Decodable:

    struct APIService {
        
        func request<T: Decodable>(from endpoint: APIRequest, body: String) -> AnyPublisher<T, APIError> { 
            
            var request = endpoint.urlRequest
            request.httpMethod = endpoint.method
            
            if endpoint.authenticated == true {
                request.setValue("testToken", forHTTPHeaderField: "token")
            }
            if body != "" {
                let finalBody = body.data(using: .utf8)
                request.httpBody = finalBody
            }
            
            return URLSession
                .shared
                .dataTaskPublisher(for: request)
                .receive(on: DispatchQueue.main)
                .mapError { _ in APIError.unknown}
                .flatMap { data, response -> AnyPublisher<T, APIError> in  // <-- here
                    
                    guard let response = response as? HTTPURLResponse else {
                        return Fail(error: APIError.unknown).eraseToAnyPublisher()
                    }
                    
                    print(response.statusCode)
                    
                    if response.statusCode == 200 {
                        let jsonDecoder = JSONDecoder()
                        return Just(data)
                            .decode(type: T.self, decoder: jsonDecoder)  // <-- here
                            .mapError { _ in APIError.decodingError }
                            .eraseToAnyPublisher()
                    }
                    else {
                        return Fail(error: APIError.errorCode(response.statusCode)).eraseToAnyPublisher()
                    }
                }
                .eraseToAnyPublisher()
        }
    }
    

    you may also want to return an array of such Decodable:

    func requestThem<T: Decodable>(from endpoint: APIRequest, body: String) -> AnyPublisher<[T], APIError> {
      ....
      .flatMap { data, response -> AnyPublisher<[T], APIError> in
      ...
      .decode(type: [T].self, decoder: jsonDecoder)
      ...