Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static func uploadVerificationPhoto(name: String, image: UIImage?, _ handler: @escaping (UploadVerificationPhotoResponse) -> Void) {
- guard let token = KeychainManager.shared.token else {
- os_log(.error, log: .api, "No token avaliable for request")
- let response = UploadVerificationPhotoResponse(failure: .api(.credential))
- handler(response)
- return
- }
- // Add Headers
- let headers = [
- "x-access-token": token,
- // "Content-Type":"multipart/form-data; charset=utf-8"
- // "Content-Type":"multipart/form-data; charset=utf-8; boundary=__weatly__"
- // "Content-Type":"multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__",
- ]
- let resizedImage = image // image?.scaledDown(into: CGSize(width: 800, height: 600))
- guard let data = resizedImage?.jpegData(compressionQuality: 0.25) ?? resizedImage?.pngData() else {
- os_log(.error, log: .api, "Encoding failed")
- let response = UploadVerificationPhotoResponse(failure: .imageEncoding(nil))
- handler(response)
- return
- }
- // Fetch Request
- Alamofire.upload(multipartFormData: { multipartFormData in
- multipartFormData.append(data, withName: "img", mimeType: "image/jpg")
- // multipartFormData.append(data, withName :"img")
- multipartFormData.append(name.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName :"name")
- }, to: API.baseURL + "/certification/photo",
- method: .post,
- headers: headers,
- encodingCompletion: { encodingResult in
- os_log(.info, log: .api, "Encoding successful")
- switch encodingResult {
- case .success(let upload, _, _):
- upload.uploadProgress { (progress) in
- os_log(.debug, log: .api, "Uploading %{PUBLIC}@", "\(progress.fractionCompleted)")
- }
- upload.responseString { (response) in
- switch response.result {
- case .success(let string):
- print(string)
- default: break
- }
- }
- upload.responseData(completionHandler: { (response) in
- switch response.result {
- case .success(let data):
- os_log(.info, log: .api, "Upload successful")
- let decoder = JSONDecoder()
- do {
- let result = try decoder.decode(UploadVerificationPhotoResponse.self, from: data)
- KeychainManager.shared.uid = result.application?.uid
- handler(result)
- } catch let DecodingError.dataCorrupted(context) {
- os_log(.error, log: .api, "Parse failed - data corrupted")
- os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
- os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
- let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
- handler(response)
- } catch let DecodingError.keyNotFound(key, context) {
- os_log(.error, log: .api, "Parse failed - %{PRIVATE}@ key not found", key.stringValue)
- os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
- os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
- let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
- handler(response)
- } catch let DecodingError.valueNotFound(value, context) {
- os_log(.error, log: .api, "Parse failed - value not found")
- os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
- os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
- debugPrint(value)
- let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
- handler(response)
- } catch let DecodingError.typeMismatch(type, context) {
- os_log(.error, log: .api, "Parsing failed - type mismatch")
- os_log(.debug, log: .api, "%{PRIVATE}@", context.codingPath)
- os_log(.debug, log: .api, "%{PRIVATE}@", context.debugDescription)
- debugPrint(type)
- let response = UploadVerificationPhotoResponse(application: nil, failure: .api(.parse))
- handler(response)
- } catch {
- os_log(.error, log: .api, "Request failed - %{PUBLIC}@", error.localizedDescription)
- let response = UploadVerificationPhotoResponse(failure: .api(.general(error)))
- handler(response)
- }
- case .failure(let error):
- os_log(.error, log: .api, "Request failed - %{PUBLIC}@", error.localizedDescription)
- let response = UploadVerificationPhotoResponse(failure: .api(.general(error)))
- handler(response)
- }
- })
- case .failure(let encodingError):
- os_log(.error, log: .api, "Encoding failed - %{PUBLIC}@", encodingError.localizedDescription)
- let response = UploadVerificationPhotoResponse(failure: .imageEncoding(encodingError))
- handler(response)
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement