Alamofire automatic validation(Alamofire自动化验证)--Moya文档

Alamofire automatic validation(Alamofire自动化验证)

有时候你希望对一些请求使用Alamofire自动化验证。当一个请求被配置为Alamofire验证时,Moya将会在内部自动在某个具体的DataRequest上调用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)

总结 这小节的核心:

  1. 在Moya中使用Alamofire的自动化验证

你可能感兴趣的:(Alamofire automatic validation(Alamofire自动化验证)--Moya文档)