Alamofire automatic validation(Alamofire自動化驗證)
有時候你希望對一些請求使用Alamofire自動化驗證。當(dāng)一個請求被配置為Alamofire驗證時,Moya將會在內(nèi)部自動在某個具體的DataRequest上調(diào)用Alamofire的validate()方法
/ MARK: - TargetType Protocol Implementation
extension MyService: TargetType {
var baseURL: URL { return URL(string: "https://api.myservice.com")! }
var path: String {
switch self {
case .zen:
return "/zen"
case .showUser(let id):
return "/users/\(id)"
case .createUser:
return "/users"
case .showAccounts:
return "/accounts"
}
}
// Other needed configurations
// ...
// Validate setup is not required; defaults to `false`
// for all requests unless specified otherwise.
var validate: Bool {
switch self {
case .zen, .showUser, .showAccounts:
return true
case .createUser(let firstName, let lastName):
return false
}
}
}
Alamofire自動驗證可能很有用,例如,如果您想要使用的Alamofire RequestRetrier RequestAdapter,(for an oAuth 2 ready Moya Client)
總結(jié) 這小節(jié)的核心:
- 在Moya中使用Alamofire的自動化驗證