Last active
February 3, 2019 08:08
-
-
Save NeilsUltimateLab/ccd4873bfbadbe309969acafd7cd2079 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static func postResource<A>(_ resource: WebResource<A>, completion: @escaping (Result<A>)->Void, progressCompletion: ((Double)->Void)?) { | |
| guard let url = resource.urlPath.url else { | |
| return | |
| } | |
| let parameter = resource.method.parameter | |
| let header = resource.header | |
| guard isReachable else { | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
| completion(.error(.notReachable)) | |
| return | |
| } | |
| print("Calling Webservice call for resource: \(resource.urlPath)....") | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = true | |
| Alamofire.upload(multipartFormData: { (multipartFormData) in | |
| if let parameters = parameter { | |
| for (key, value) in parameters { | |
| let (url, mimeType) = MediaType.generateMimeType(key: key, value: value) | |
| if let url = url { | |
| multipartFormData.append(url, withName: key, fileName: url.fileName.appending(".").appending(url.pathExtension), mimeType: mimeType) | |
| }else { | |
| if let stringValue = value as? String, let data = stringValue.data(using: String.Encoding.utf8) { | |
| multipartFormData.append(data, withName: key, mimeType: "text/plain") | |
| } | |
| } | |
| } | |
| } | |
| }, to: url, headers: header) { (encodingResult) in | |
| switch encodingResult { | |
| case .success(let upload, _, _): | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
| upload.uploadProgress(closure: {(progress: Progress) in | |
| progressCompletion?(progress.fractionCompleted) | |
| }) | |
| upload.responseData(completionHandler: { (response) in | |
| UIApplication.shared.isNetworkActivityIndicatorVisible = false | |
| // print("\(String.consoleSeperator)Webservice responded for : \(resource) \nwith: \(response)") | |
| print("\(String.consoleSeperator)Webservice responded for Web Resource: \(resource) \nwith Response: \(response.debugDescription)") | |
| if let code = response.response?.statusCode { | |
| if code == 401 { | |
| completion(.error(.sessionExpired)) | |
| return | |
| } | |
| } | |
| if let data = response.result.value { | |
| completion(resource.decode(data)) | |
| } else { | |
| guard response.response?.url == url else { | |
| return | |
| } | |
| if let error = response.result.error { | |
| completion(.error(.serverError(error.localizedDescription))) | |
| } | |
| } | |
| }) | |
| case .failure(let error): | |
| print("Error : \(error)") | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| enum MediaType: String { | |
| case doc, dot, docx, | |
| dotx, dotm, docm | |
| case txt, rtf | |
| case ppt, pot, pps, ppa, | |
| pptx, potx, ppsx, ppam, pptm, | |
| potm, ppsm | |
| case xls, xlt, xla, xlsx, xltx, | |
| xlsm, xltm, xlam, xlsb | |
| case pdf | |
| case jpg, png, jpeg | |
| } | |
| extension MediaType { | |
| var mimeType: String { | |
| switch self { | |
| case .doc, .dot, .docx, | |
| .dotx, .dotm, .docm: | |
| return "application/msword" | |
| case .txt, .rtf: | |
| return "text/plain" | |
| case .ppt, .pot, .pps, .ppa, | |
| .pptx, .potx, .ppsx, .ppam, .pptm, | |
| .potm, .ppsm: | |
| return "application/vnd.openxmlformats-officedocument.presentationml.presentation" | |
| case .pdf: | |
| return "application/pdf" | |
| case .xls, .xlt, .xla, .xlsx, .xltx, | |
| .xlsm, .xltm, .xlam, .xlsb: | |
| return "application/vnd.ms-excel" | |
| case .png: | |
| return "image/png" | |
| case .jpg, .jpeg: | |
| return "image/jpeg" | |
| } | |
| } | |
| } | |
| extension MediaType { | |
| static func generateMimeType(key: String, value: Any) -> (url: URL?, mimeType: String) { | |
| if let url = value as? URL { | |
| guard let mediaType = MediaType(rawValue: url.pathExtension.lowercased()) else { | |
| return (nil, "") | |
| } | |
| return (value as? URL, mediaType.mimeType) | |
| } | |
| return (nil, "") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment