Moya
is working on Alamofire
and hidden it realization. So the is no any reason to use Moya in your case. You can just use Alamofire by it-self if you need it.
The is no correct way to pass URLRequest to Moya
. Only way is to download source code and add needed functionality by yourself
After looking into the following article
Writing Network Layer with Moya for Swift
I can say following code should work for you.
import Foundation
import Moya
enum MyServerAPI {
case cameras
case settingsFor(cameraId: String)
// MARK: - User
case createUser(email: String, password: String)
}
// 2:
extension MyServerAPI: TargetType {
// 3:
var baseURL: URL { return URL(string: "https://testing.myserver.com/api/v1")! }
// 4:
var path: String {
switch self {
case .cameras:
return "/cameras"
case .settingsFor(let cameraId):
return "/cameras/\(cameraId)/settings"
case .createUser:
return "/user"
}
}
// 5:
var method: Moya.Method {
switch self {
case .createUsr:
return .post
default:
return .get
}
}
// 6:
var parameters: [String: Any]? {
switch self {
case .createUser(let email, let password):
var parameters = [String: Any]()
parameters["email"] = email
parameters["password"] = password
return parameters
default:
return nil
}
}
// 7:
var parameterEncoding: ParameterEncoding {
return JSONEncoding.default
}
// 8:
var sampleData: Data {
return Data()
}
// 9:
var task: Task {
return .request
}
}
let provider = MoyaProvider<MyServerAPI>()
provider.request(.cameras) { (result) in
switch result {
case .success(let response):
// do something with resoinse
case .failure(let error):
// show error
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.