extension APIRequestHandler {
private func uploadToServerWith<T: CodableInit>(_ decoder: T.Type, data: UploadData, request: URLRequestConvertible, parameters: Parameters?, progress: ((Progress) -> Void)?, completion: CallResponse<T>) {
upload(multipartFormData: { (mul) in
mul.append(data.data, withName: data.name, fileName: data.fileName, mimeType: data.mimeType)
guard let parameters = parameters else { return }
// adding keys and values to the multipart file,
// although I wanted to pass them with the URL Request but it didn't work somehow,
// so until I figured it out I'm putting it here.
for (key, value) in parameters {
mul.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
}, with: request) { (response) in
switch response {
case .success(let requestUp, _, _):
requestUp.responseData(completionHandler: { (results) in
self.handleResponse(results, completion: completion)
}).responseString(completionHandler: { (string) in
print(string.result.value)
})
case .failure(let error):
completion?(ServerResponse<T>.failure(error as? LocalizedError))
}
}
}