3
\$\begingroup\$

I am trying to wrap a responseJSON function of a Request class of Alamofire because when I send an API request I get a response in a specific pattern. I have achieved this, but I am still not satisfied with it. I can see much code repeated in both "handle" functions. One is for handling an array of Mappable type and the other is just a single Mappable type. How can I make this code better?

//
//  AlamofireRequestExtension.swift
//  DartsCool
//
//  Created by Dari on 11/22/16.
//  Copyright © 2016 Ekbana Solutions Pte. Ltd. All rights reserved.
//

import Foundation
import ObjectMapper
import Alamofire
import SwiftyJSON

extension Request {
    func handle<T: Mappable>(completion: (T, Pagination?) -> Void, failure: NSError -> Void) {
        self.validate(statusCode: 200 ..< 411).responseJSON { (response) in
            switch response.result {
            case .Success(let value):
                switch response.response?.statusCode {
                case 200?:
                    let json = JSON(value)
                    if let data: T = json["data"].map() {
                        let pagination: Pagination? = json["pagination"].map()
                        completion(data, pagination)

                    }
                default:
                    break
                }
                break

            case .Failure(let error):
                failure(error)
                break

            }
        }
    }

    func handle<T: Mappable>(completion: ([T], Pagination?) -> Void, failure: NSError -> Void) {
        self.validate(statusCode: 200 ..< 411).responseJSON { (response) in
            switch response.result {
            case .Success(let value):
                switch response.response?.statusCode {
                case 200?:
                    let json = JSON(value)
                    if let data: [T] = json["data"].map() {
                        let pagination: Pagination? = json["pagination"].map()
                        completion(data, pagination)
                    }
                default:
                    break
                }
                break

            case .Failure(let error):
                failure(error)
                break

            }
        }
    }

}

extension JSON {
    func map<T: Mappable>() -> [T]? {
        let json = self.arrayObject
        let mapped = json?.map{Mapper<T>().map($0)!}
        return mapped
    }

    func map<T: Mappable>() -> T? {
        return Mapper<T>().map(self.object)
    }
}
\$\endgroup\$
  • \$\begingroup\$ I want to implement a wrapper for ALamofire request, would you please tell me how you are doing it ? \$\endgroup\$ – Arpit B Parekh Feb 27 '18 at 10:20
  • \$\begingroup\$ Above is concrete code that I have been using in my projects for handling response JSON. I am using Object Mapper for mapping JSON to Object. \$\endgroup\$ – Dari Feb 27 '18 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.