Moya基本用法

Moya简介

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

基本用法

  1. 定义接口枚举

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    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自带的单元测试工具来自测,测试用例如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    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
    }
    }
    }
  2. 自定义超时时间

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    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)))
    }
    })
    }
  3. 错误处理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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)
    }
    }
    }
    }
  4. 调用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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
    }
    }