ScromViewModel.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // ScromViewModel.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 18/01/23.
  6. //
  7. import Foundation
  8. import Alamofire
  9. class ScromViewModel {
  10. var delegate: ScromProtocol?
  11. func getScromData(fileId:String) {
  12. if let delegate = delegate {
  13. delegate.startLoader()
  14. let accessToken = UserDefaults.standard.value(forKey: "accessToken") as! String
  15. let url = ApiURL.DownloadBase64 + "/\(fileId)"
  16. print(url)
  17. let headers: HTTPHeaders = [
  18. "Authorization": "Bearer \(accessToken)",
  19. "Accept": "application/json",
  20. "Content-Type": "application/json"
  21. ]
  22. print(headers)
  23. AF.request(url,
  24. method: .get,
  25. encoding: URLEncoding.httpBody,
  26. headers: headers)
  27. .responseString { response in
  28. if response.response?.statusCode == 200 {
  29. switch response.result {
  30. case .success(_):
  31. delegate.stopLoader()
  32. let stringData = response.description.components(separatedBy: ",")
  33. delegate.scromBase64(base64: stringData[1])
  34. break
  35. case .failure(let error):
  36. delegate.stopLoader()
  37. print(error.localizedDescription)
  38. delegate.showError(error: error.localizedDescription)
  39. break
  40. }
  41. } else {
  42. delegate.stopLoader()
  43. print("Server error")
  44. }
  45. }
  46. }
  47. }
  48. }
  49. protocol ScromProtocol {
  50. func startLoader()
  51. func stopLoader()
  52. func showError(error: String)
  53. func scromBase64(base64: String)
  54. }