PersonalAPI.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // PersonalAPI.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/11/21.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import Moya
  9. enum PersonalAPI {
  10. case personInfo
  11. case updatePersonInfo(O2PersonInfo)
  12. case updatePersonIcon(UIImage)
  13. case meetingConfig
  14. }
  15. extension PersonalAPI: OOAPIContextCapable {
  16. var apiContextKey: String {
  17. return "x_organization_assemble_personal"
  18. }
  19. }
  20. extension PersonalAPI: OOAccessTokenAuthorizable {
  21. var shouldAuthorize: Bool {
  22. return true
  23. }
  24. }
  25. extension PersonalAPI: TargetType {
  26. var baseURL: URL {
  27. let model = O2AuthSDK.shared.o2APIServer(context: .x_organization_assemble_personal)
  28. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  29. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  30. return URL(string: trueUrl)!
  31. }
  32. return URL(string: baseURLString)!
  33. }
  34. var path: String {
  35. switch self {
  36. case .personInfo:
  37. return "jaxrs/person"
  38. case .updatePersonInfo(_):
  39. return "jaxrs/person"
  40. case .updatePersonIcon(_):
  41. return "jaxrs/person/icon"
  42. case .meetingConfig:
  43. return "jaxrs/definition/meetingConfig"
  44. }
  45. }
  46. var method: Moya.Method {
  47. switch self {
  48. case .personInfo, .meetingConfig:
  49. return .get
  50. case .updatePersonInfo(_), .updatePersonIcon(_):
  51. return .put
  52. }
  53. }
  54. var sampleData: Data {
  55. return "".data(using: String.Encoding.utf8)!
  56. }
  57. var task: Task {
  58. switch self {
  59. case .personInfo, .meetingConfig:
  60. return .requestPlain
  61. case .updatePersonInfo(let person):
  62. return .requestParameters(parameters: person.toJSON() ?? [:], encoding: JSONEncoding.default)
  63. case .updatePersonIcon(let image):
  64. let data = image.pngData()
  65. let formData = MultipartFormData(provider: .data(data!), name: "file",
  66. fileName: "avatar.png", mimeType: "image/png")
  67. return .uploadMultipart([formData])
  68. }
  69. }
  70. var headers: [String : String]? {
  71. return nil
  72. }
  73. }