内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

Moya基本用法

2018-06-08 18:49 出处:清屏网 人气: 评论(0

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

  1. 定义接口枚举

    public enum HFAPI {
        case login(String, String, String) //登录接口
        case smscode(String)    //登录,发送验证码
    }
  2. 拓展枚举并实现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<HFAPI>(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
                }
            }
        }
  3. 自定义超时时间

    public static func hFApiProvider(timeInterval:TimeInterval  = 15) -> MoyaProvider<HFAPI> {
            return MoyaProvider<HFAPI>(
                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)))
                    }
            })
        }
  4. 错误处理

    struct HFNetWork {
        
        public static func request(provider: MoyaProvider<HFAPI>,
                            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)
                }
            }
        }
    }
  5. 调用

    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

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号