JPushAPI.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // JPushAPI.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/11/8.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import Moya
  9. // x_jpush_assemble_control 极光推送模块
  10. enum JPushAPI {
  11. case pushConfig
  12. case bindDevice(JPushDevice)
  13. case unBindDevice(String)
  14. case unBindDeviceNew(String, String)
  15. }
  16. // 上下文根
  17. extension JPushAPI: OOAPIContextCapable {
  18. var apiContextKey: String {
  19. return "x_jpush_assemble_control"
  20. }
  21. }
  22. // 是否需要xtoken
  23. extension JPushAPI: OOAccessTokenAuthorizable {
  24. var shouldAuthorize: Bool {
  25. return true
  26. }
  27. }
  28. extension JPushAPI: TargetType {
  29. var baseURL: URL {
  30. let model = O2AuthSDK.shared.o2APIServer(context: .x_jpush_assemble_control)
  31. let baseURLString = "\(model?.httpProtocol ?? "http")://\(model?.host ?? ""):\(model?.port ?? 80)\(model?.context ?? "")"
  32. if let trueUrl = O2AuthSDK.shared.bindUnitTransferUrl2Mapping(url: baseURLString) {
  33. return URL(string: trueUrl)!
  34. }
  35. return URL(string: baseURLString)!
  36. }
  37. var path: String {
  38. switch self {
  39. case .pushConfig:
  40. return "/jaxrs/device/config/push/type"
  41. case .bindDevice(_):
  42. return "/jaxrs/device/bind"
  43. case .unBindDevice(let deviceName):
  44. return "/jaxrs/device/unbind/\(deviceName)/ios"
  45. case .unBindDeviceNew(let deviceToken, let pushType):
  46. return "/jaxrs/device/unbind/new/\(deviceToken)/ios/\(pushType)"
  47. }
  48. }
  49. var method: Moya.Method {
  50. switch self {
  51. case .pushConfig:
  52. return .get
  53. case .bindDevice(_):
  54. return .post
  55. case .unBindDevice(_):
  56. return .delete
  57. case .unBindDeviceNew(_, _):
  58. return .get
  59. }
  60. }
  61. var sampleData: Data {
  62. return "".data(using: String.Encoding.utf8)!
  63. }
  64. var task: Task {
  65. switch self {
  66. case .pushConfig:
  67. return .requestPlain
  68. case .bindDevice(let device):
  69. return .requestParameters(parameters: device.toJSON()!, encoding: JSONEncoding.default)
  70. case .unBindDevice:
  71. return .requestPlain
  72. case .unBindDeviceNew(_, _):
  73. return .requestPlain
  74. }
  75. }
  76. var headers: [String : String]? {
  77. return nil
  78. }
  79. }