Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.79 KB | None | 0 0
  1. import UIKit
  2. import Moya
  3.  
  4. enum ChatDataProvider {
  5.    
  6.     case viewChat(id: String)
  7.     case getMessages(chatId: String, count: String, messageId: String)
  8.     case chatList()
  9.     case currentTime()
  10.    
  11. }
  12.  
  13. extension ChatDataProvider: TargetType {
  14.    
  15.     var baseURL: URL {
  16.         return URL(string: BaseURL)!
  17.     }
  18.    
  19.     var path: String {
  20.         switch self {
  21.         case .viewChat:
  22.             return "chats/view"
  23.         case .getMessages:
  24.             let url = "chats/messages"
  25.             return url
  26.         case .chatList:
  27.             return "chats"
  28.         case .currentTime:
  29.             return "misc/time"
  30.         }
  31.     }
  32.    
  33.     var method: Moya.Method {
  34.         switch self {
  35.         case .getMessages, .chatList, .viewChat, .currentTime:
  36.             return .get
  37.         }
  38.     }
  39.    
  40.     var sampleData: Data {
  41.         switch self {
  42.         default:
  43.             return "{}".data(using: String.Encoding.utf8)!
  44.         }
  45.     }
  46.    
  47.     var task: Task {
  48.         switch self {
  49.         case .chatList, .currentTime:
  50.             return .requestPlain
  51.         case .viewChat(let id):
  52.             return .requestCompositeData(
  53.                 bodyData: "".data(using: .utf8)!,
  54.                 urlParameters: ["id": id])
  55.         case .getMessages(let id, let count, let messageId):
  56.             return .requestCompositeData(
  57.                 bodyData: "".data(using: .utf8)!,
  58.                 urlParameters: ["id": id, "last": messageId, "count": count])
  59.         }
  60.     }
  61.    
  62.     var headers: [String : String]? {
  63.         var params = [String : String]()
  64.         let token = UserDefaultsManager().get(data: .token)
  65.         params["Content-Type"] = "application/json"
  66.         params["Authorization"] = "JWT \(token)"
  67.         return params
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement