Добрый день, мне необходимо Отправить post запрос с key= value строковый параметр в. moya
//
import UIKit
import Foundation
import Moya
import Alamofire
let tmProvider = MoyaProvider<APIEndpoint>(plugins: [NetworkLoggerPlugin(verbose: true, responseDataFormatter: JSONResponseDataFormatter)])
public enum APIEndpoint {
// Auth
case signUp(params: [String: Any])
case signIn(params: [String: Any])
case forgotPassword(params: [String: Any])
}
extension APIEndpoint: TargetType {
public var baseURL: URL { return URL(string: "")! }
public var path: String {
switch self {
case .signIn:
return "/drivers/login"
}
public var method: Moya.Method {
switch self {
case .signIn, .signUp, .forgotPassword, .addComment, .uploadReportImage,
.addReport, .addFavorite, .getFavorites, .removeFavorite, .uploadAvatar,
.getProfile, .updateProfile, .getUserComments:
return .post
default:
return .get
}
}
public var parameters: [String: Any]? {
var params:[String: Any] = [:] // for post params
var query:[String: Any] = [:] // for get params
public var parameterEncoding: ParameterEncoding {
switch self {
case .addComment, .uploadReportImage,
.addReport, .addFavorite, .getFavorites, .removeFavorite, .uploadAvatar,
.getProfile, .updateProfile, .getUserComments:
return CompositeJsonEncoding()
default:
return CompositeEncoding()
}
}
public var sampleData: Data {
return "Default sample data".data(using: String.Encoding.utf8)!
}
public var task: Task {
switch self {
case .uploadAvatar(let data):
let imgData = MultipartFormData(provider: MultipartFormData.FormDataProvider.data(data), name: "file", fileName: "userAva.jpg", mimeType: "image/jpeg")
let multipartData = [imgData]
return .uploadMultipart(multipartData)
case .uploadReportImage(let data):
let imgData = MultipartFormData(provider: .data(data), name: "file", fileName: "userAva.jpg", mimeType: "image/jpeg")
let multipartData = [imgData]
return .uploadMultipart(multipartData)
default:
return .requestPlain
}
}
public var validate: Bool {
return true
}
public var headers: [String : String]? {
var params:[String: String] = [:] // for post params
params["Authorization"] = "Basic ZGV2QGFtb3RvcnMua3o6MTIzMTIz"
return params
}
}
public func url(_ route: TargetType) -> String {
return route.baseURL.appendingPathComponent(route.path).absoluteString
}
struct CompositeEncoding: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
guard let parameters = parameters else {
return try urlRequest.asURLRequest()
}
let queryParameters = (parameters["query"] as! Parameters)
var queryRequest = try URLEncoding(destination: .queryString).encode(urlRequest, with: queryParameters)
if let body = parameters["body"] {
let bodyParameters = (body as! Parameters)
let req = try URLEncoding(destination: .queryString).encode(urlRequest, with: bodyParameters)
if let url = req.url, let query = url.query {
queryRequest.httpBody = query.data(using: .utf8)
}
return queryRequest
} else {
return queryRequest
}
}
}
нигде не нашел подобного примера.А дебажить этот файл проблемно.