Almofire 4.0 wrapper class with useful methods - best way ...?

I am using this wrapper class as a utility for web, is this correct or any other way to use it correctly for webservices with closure, since I am not using any singelton or delegate design pattern, this is required or like url as base am I linking base url?

class AFWrapper: NSObject {

    class func requestGETURL(_ strURL: String, params:[String : AnyObject]?, headers : [String : String]? ,success:@escaping (JSON) -> Void, failure:@escaping (NSError) -> Void) {
        guard !strURL.isEmpty else {
            return
        }

        let strURL = Constants.URLs.kBaseUrl + strURL

        Alamofire.request(strURL, method: .get, parameters: params,headers: headers).responseJSON { (responseObject) -> Void in
            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                success(resJson)
            }
            if responseObject.result.isFailure {
                let error : NSError = responseObject.result.error! as NSError
                failure(error)
            }
        }
    }

    class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (NSError) -> Void) {
        guard !strURL.isEmpty else {
            return
        }

        let strURL = Constants.URLs.kBaseUrl + strURL
        Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (responseObject:DataResponse<Any>) in
            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                success(resJson)
            }
            if responseObject.result.isFailure {
                let error : NSError = responseObject.result.error! as NSError
                failure(error)
            }
        }
    }

    class func uploadMultipartFormData(_ strURL : String, params : [String : AnyObject]?, imageDataArray: [Data],imageNamesArray: [String], headers : [String : String]?, success:@escaping (JSON) -> Void, failure:@escaping (NSError) -> Void) {
        Alamofire.upload(multipartFormData: { (MultipartFormData) in
            var secondCounter = 0
            for data in imageDataArray {
                print(imageDataArray[secondCounter])
                MultipartFormData.append(data, withName: imageNamesArray[secondCounter], fileName: imageNamesArray[secondCounter] + "myImage.png", mimeType: "image/png")

                MultipartFormData.append(data, withName: imageNamesArray[secondCounter], fileName: imageNamesArray[secondCounter] + "myImage.png", mimeType: "image/png")
                secondCounter = secondCounter + 1
            }

            let contentDict = params as? [String: String]
            for (key, value) in contentDict! {
                MultipartFormData.append(value.data(using: .utf8)!, withName: key)
            }
        }, to: strURL, method: .post, headers: nil, encodingCompletion: { (result) in
            switch result {
            case .success(let upload, _, _):
                upload.responseJSON(completionHandler: { (dataResponse) in
                    if dataResponse.result.error != nil {
                        failure(dataResponse.result.error as! NSError)
                    }
                    else {
                        print(dataResponse.result.value)
                    }
                })
            case .failure(let encodingError):
                failure(encodingError as NSError)
            }
        })
    }
}

      

+3


source to share





All Articles