我正在使用这个包装类作为网络的实用程序这是正确的或任何其他方式正确使用它与带有闭包的web webservices,因为我没有使用任何singelton或委托设计模式是否需要或如何和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)
}
})
}
}