Search code examples
swiftalamofireobjectmapper

Generic network call with Alamofire and ObjectMapper


I am using Alamofire, and Alamofire Object Mapper to make network request and serialize it. We always get our response same shape with following example that includes result, isSuccessfull and message.

{
    "Result": {
        "WaitingCount": 0,
        "ApprovedCount": 0,
        "RejectedCount": 0
    },
    "IsSuccessfull": true,
    "Message": null
}

I created a base class to prevent code duplication with following code.

    struct BaseObjectResponseParser<T:Mappable>: Mappable {
    public var item: T?
    public var isSuccessful:Bool?
    public var message: String?
    init?(map: Map){
    }
    mutating func mapping(map: Map) {
        item <- map["Result"]
        isSuccessful <- map["IsSuccessfull"]
        message <- map["Message"]
    }
}

Then, for every different models, I created a model class with following.

class UnitWorkOrdersCount: Mappable {
    var waitingCount: Int = 0
    var approvedCount: Int = 0
    var rejectedCount: Int = 0

    required init?(map: Map) {
    }

    func mapping(map: Map) {
        waitingCount <- map["WaitingCount"]
        approvedCount <- map["ApprovedCount"]
        rejectedCount <- map["RejectedCount"]
    }    
}

I am using following code snippet to make a network call from server and serialize the response.

typealias HandlerGetUnitWorkOrdersCount = (UnitWorkOrdersCount?, _ message: String?) -> ()
func getUnitWorkOrdersCount(by identifier: Int, handler: @escaping HandlerGetUnitWorkOrdersCount) {
    let parameters: Parameters = ["identifier": identifier]
    Alamofire.request(URL_GET_UNIT_WORK_ORDER_COUNT, method: .get, parameters: parameters).responseObject { (response: DataResponse<BaseObjectResponseParser<UnitWorkOrdersCount>>) in
        switch response.result {
        case .success:
            if let result = response.result.value {
                if result.isSuccessful ?? false {
                    handler(result.item, nil)
                } else {
                    handler(nil, result.message)
                }
            }
        case .failure(let error):
            print(error)
            handler(nil, error.localizedDescription)
        }
    }
}

However, I have a lot of network request and my code is duplicated with above code. I am trying to make it generic but I could not be successful. How can I make networkRequest function and my completion handler generic and prevent it from duplication?


Solution

  • You can try making it generic as below,

    class MyService {
    
        public static func request<T: Mappable>(_ urlString: String,
                                                method: HTTPMethod,
                                                params: Parameters?,
                                                type: T.Type,
                                                completion: @escaping (T?, String?) -> Void) {
    
            Alamofire.request(urlString, method: method, parameters: params).responseObject { (response: DataResponse<BaseObjectResponseParser<T>>) in
                switch response.result {
                case .success:
                    if let result = response.result.value {
                        if result.isSuccessful ?? false {
                            completion(result.item, nil)
                        } else {
                            completion(nil, result.message)
                        }
                    }
                case .failure(let error):
                    completion(nil, error.localizedDescription)
                }
            }
        }
    }
    

    Usage

    MyService.request("urlPathString", method: .get, params: nil,
                      type: UnitWorkOrdersCount.self) { (unitWork, message) in
                        if let message = message {
                            print(message)
                            return
                        }
                        print(unitWork!.waitingCount)
    }