SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static func uploadVerificationPhoto(name: String, image: UIImage?, _ handler: @escaping (UploadVerificationPhotoResponse) -> Void) {
  2.         guard let token = KeychainManager.shared.token else {
  3.             os_log(.error, log: .api, "No token avaliable for request")
  4.             let response = UploadVerificationPhotoResponse(failure: .api(.credential))
  5.             handler(response)
  6.             return
  7.         }
  8.        
  9.         // Add Headers
  10.         let headers = [
  11.             "x-access-token": token,
  12. //            "Content-Type":"multipart/form-data; charset=utf-8"
  13. //            "Content-Type":"multipart/form-data; charset=utf-8; boundary=__weatly__"
  14. //            "Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__",
  15.         ]
  16.        
  17.         let resizedImage = image // image?.scaledDown(into: CGSize(width: 800, height: 600))
  18.        
  19.         guard let data = resizedImage?.jpegData(compressionQuality: 0.25) ?? resizedImage?.pngData() else {
  20.             os_log(.error, log: .api, "Encoding failed")
  21.             let response = UploadVerificationPhotoResponse(failure: .imageEncoding(nil))
  22.             handler(response)
  23.             return
  24.         }
  25.        
  26.         // Fetch Request
  27.         Alamofire.upload(multipartFormData: { multipartFormData in
  28.             multipartFormData.append(data, withName: "img", mimeType: "image/jpg")
  29. //            multipartFormData.append(data, withName :"img")
  30.             multipartFormData.append(name.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"name")
  31.         }, to: API.baseURL + "/certification/photo",
  32.            method: .post,
  33.            headers: headers,
  34.            encodingCompletion: { encodingResult in
  35.             os_log(.info, log: .api, "Encoding successful")
  36.             switch encodingResult {
  37.             case .success(let upload, _, _):
  38.                 upload.uploadProgress { (progress) in
  39.                     os_log(.debug, log: .api, "Uploading %{PUBLIC}@", "\(progress.fractionCompleted)")
  40.                 }
  41.                 upload.responseString { (response) in
  42.                     switch response.result {
  43.                     case .success(let string):
  44.                         print(string)
  45.                     default: break
  46.                     }
  47.                 }
  48.                 upload.responseData(completionHandler: { (response) in
  49.                     switch response.result {
  50.                     case .success(let data):
  51.                         os_log(.info, log: .api, "Upload successful")
  52.                         let decoder = JSONDecoder()
  53.                         do {
  54.                             let result = try decoder.decode(UploadVerificationPhotoResponse.self, from: data)
  55.                             KeychainManager.shared.uid = result.application?.uid
  56.                             handler(result)
  57.                         } catch let DecodingError.dataCorrupted(context) {
  58.                             os_log(.error, log: .api, "Parse failed - data corrupted")
  59.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
  60.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
  61.                             let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
  62.                             handler(response)
  63.                         } catch let DecodingError.keyNotFound(key, context) {
  64.                             os_log(.error, log: .api, "Parse failed - %{PRIVATE}@ key not found", key.stringValue)
  65.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
  66.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
  67.                             let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
  68.                             handler(response)
  69.                         } catch let DecodingError.valueNotFound(value, context) {
  70.                             os_log(.error, log: .api, "Parse failed - value not found")
  71.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
  72.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
  73.                             debugPrint(value)
  74.                             let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
  75.                             handler(response)
  76.                         } catch let DecodingError.typeMismatch(type, context) {
  77.                             os_log(.error, log: .api, "Parsing failed - type mismatch")
  78.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
  79.                             os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
  80.                             debugPrint(type)
  81.                             let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
  82.                             handler(response)
  83.                         } catch {
  84.                             os_log(.error, log: .api, "Request failed - %{PUBLIC}@", error.localizedDescription)
  85.                             let response = UploadVerificationPhotoResponse(failure: .api(.general(error)))
  86.                             handler(response)
  87.                         }
  88.                     case .failure(let error):
  89.                         os_log(.error, log: .api, "Request failed - %{PUBLIC}@", error.localizedDescription)
  90.                         let response = UploadVerificationPhotoResponse(failure: .api(.general(error)))
  91.                         handler(response)
  92.                     }
  93.                 })
  94.             case .failure(let encodingError):
  95.                 os_log(.error, log: .api, "Encoding failed - %{PUBLIC}@", encodingError.localizedDescription)
  96.                 let response = UploadVerificationPhotoResponse(failure: .imageEncoding(encodingError))
  97.                 handler(response)
  98.             }
  99.         })
  100.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top