Moya基本用法

Moya简介

一句话来讲,Moya是对Alamofire的封装,让开发人员更加优雅的使用Alamofire。

基本用法

  1. 定义接口枚举

    public enum HFAPI {
        case login(String, String, String) //登录接口
        case smscode(String)    //登录,发送验证码
    }
    
  1. 拓展枚举并实现TargetType,代码详解如下:

    extension HFAPI: TargetType {
        
        public var baseURL: URL {
            return URL.init(string: "接口域名")!;
        }
        
        public var path: String {
            switch self {
            case .login:
                return "login接口路径"
            case .smscode:
                return "smscode接口路径"
            }
        }
        
        //请求接口时使用的方法
        public var method: Moya.Method {
            switch self {
            case .login:
                return .post
            case .smscode:
                return .get
            }
        }
        
         //请求接口时对应的请求参数
        public var task: Task {
            var params:[String : Any] = [:]
            
            params["v"] = ProjectInfo.appVersionWithOutPoint()
            params["dev"] = ProjectInfo.platform()
            switch self {
                case.login(let smsCodeToken, let smsCode, let phone):
                    params["smsCodeToken"] = smsCodeToken
                    params["smsCode"] = smsCode
                    params["phone"] = phone
                    break;
                case .smscode(let phone):
                    params["phone"] = phone
                    break;
            }
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        }
        
        //header信息
        public var headers: [String : String]? {
            var header: [String:String] = [:]
            let token = HFDefault.getToken()
            if token.count > 0 {
                header["X-Token"] = token
            }
            return header
        }
        
        //用于单元测试
        public var sampleData: Data {
            switch self {
            case .login:
                return "{\"id\": \"1\", \"first_name\": \"Harry\", \"last_name\": \"Potter\"}".data(using: String.Encoding.utf8)!
            default:
                return Data()
            }
        }
    }
    

    sampleData是用于单元测试的,我们可以使用Xcode自带的单元测试工具来自测,测试用例如下:

    func testSmscodeAPI() {
            let loginProvitder = MoyaProvider(stubClosure: MoyaProvider.immediatelyStub)
            loginProvitder.request(.smscode("17755558888")) { result in
                switch result {
                case let .success(moyaResponse):
                    let statusCode = moyaResponse.statusCode // Int - 200, 401, 500, etc
                    let data = String.init(data: moyaResponse.data, encoding: String.Encoding.utf8)
                    print("\(statusCode)")
                    print(data ?? "no data")
                case .failure(_):
                    break
                }
            }
        }
    
  2. 自定义超时时间

     public static func hFApiProvider(timeInterval:TimeInterval  = 15) -> MoyaProvider {
            return MoyaProvider(
                requestClosure: { (endPoint, closure) in
                    do {
                        var urlRequest = try endPoint.urlRequest()
                        urlRequest.timeoutInterval = timeInterval;
                        closure(.success(urlRequest))
                    } catch MoyaError.requestMapping(let url) {
                        closure(.failure(MoyaError.requestMapping(url)))
                    } catch MoyaError.parameterEncoding(let error) {
                        closure(.failure(MoyaError.parameterEncoding(error)))
                    } catch {
                        closure(.failure(MoyaError.underlying(error, nil)))
                    }
            })
        }
    
  3. 错误处理

    struct HFNetWork {
        
        public static func request(provider: MoyaProvider,
                            target: HFAPI,
                            success successCallback: @escaping (JSON) -> Void,
                            error errorCallback: @escaping (Int) -> Void,
                            failure failureCallback: @escaping (MoyaError) -> Void) {
            provider.request(target) { result in
                switch result {
                case let .success(response):
                    do {
                        let json = try JSON(response.filterSuccessfulStatusCodes().mapJSON())
                        successCallback(json)
                    }
                    catch let error {
                        errorCallback((error as! MoyaError).response!.statusCode)
                    }
                case let .failure(error):
                    failureCallback(error)
                }
            }
        }
    }
    
  4. 调用

    HFNetWork.request(provider: HFAPI.hFApiProvider(), target: .smscode(phone), success: { (json) in
                let jsonString = json.rawString() ?? ""
                if jsonString.count <= 0 {
                    return
                }
                let responseObject = ResponseModel(JSONString: jsonString)
                guard let fResponseObject = responseObject else {
                    return
                }
                
                HFLog.info(fResponseObject.data)
            }, error: { (errorcode) in
            }) { (error) in
            }
    }
    

你可能感兴趣的:(Moya基本用法)