1. 概述
Moya是对请求库Alamofire的抽象封装,相当于YTKNetwork和AFNetworking的关系
2. 基本使用
假设有如下api用于订单相关逻辑,使用Moya来实现
- 请求地址:http://127.0.0.1:8080
- 公共请求头:devtype:iOS,devid
- 公共请求参数:token:"Gz1qYLXeBW8MZuUfDlr9wsAYuVS1cZFMJY9BbaF842L2gRps747o4w=="
API | 参数 | 说明 |
---|---|---|
order/list | pageNO:订单列表开始页码,默认从1, pageSize:每页记录数 | 订单列表 |
order/findById | sn:订单id | 根据id查询订单 |
使用Moya接入API,它的设计风格就是利用枚举来实现,特别很好的利用枚举绑定值这个特性
// 生成请求封装类
let orderProvider = MoyaProvider<OrderApi>()
/// 订单相关api
enum OrderApi {
case list(pageNO: Int = 1, pageSize: Int = 10)
case findOne(sn: String)
}
/// 实现TargetType协议
extension OrderApi: TargetType {
/// url
var baseURL: URL {
return URL(string: "http://127.0.0.1:8080/order")!
}
/// 请求路径
var path: String {
switch self {
case .list:
return "list"
case .findOne(_):
return "findById"
}
}
/// 请求方式
var method: Moya.Method {
return .post
}
/// 解析格式
var sampleData: Data {
return "{}".data(using: String.Encoding.utf8)!
}
var task: Task {
// 公共参数
var params: [String: Any] = ["token": "Gz1qYLXeBW8MZuUfDlr9wsAYuVS1cZFMJY9BbaF842L2gRps747o4w=="]
// 收集参数
switch self {
case let .list(pageNO, pageSize):
params["pageNO"] = pageNO
params["pageSize"] = pageSize
case .findOne(let sn):
params["sn"] = sn
}
// 发起请求
return .requestParameters(parameters: params, encoding: URLEncoding.default)
}
/// 公共请求头
var headers: [String : String]? {
return ["devtype": "iOS", "devid": UIDevice().identifierForVendor?.uuidString ?? "unknow"]
}
}
调用发送请求
orderProvider.request(OrderApi.findOne(sn: "DJKRE3248DFHJEW23")) { (result) in
let json = try! JSON(data: result.value!.data)
}
3. 高级使用
实际使用中可能有很多需求,并不像上述这么简单,比如显示网络指示器、统一加密解密后回调等等...Moya中的解决方案是在MoyaProvider创建的时候,可以传入回调闭包和插件
import Foundation
import Moya
import SwiftyJSON
import KRProgressHUD
//import Alamofire
import enum Result.Result
let endpointClosure = { (target: LawApi) -> Endpoint in
var endpoint: Endpoint = MoyaProvider.defaultEndpointMapping(for: target)
endpoint = endpoint.adding(newHTTPHeaderFields: ["appName": appName()])
let request = try! endpoint.urlRequest()
DLog("\n请求地址:\(request.url!.absoluteString)\n" + "请求头:\(request.allHTTPHeaderFields!)\n" + "请求参数:\(String(describing: String(data: request.httpBody!, encoding: .utf8)?.components(separatedBy: "&")))")
return endpoint
}
let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) -> Void in
// done(.success(<#T##URLRequest#>))
}
// 插件写法
class LoadingPlugin: PluginType {
func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
return request
}
func willSend(_ request: RequestType, target: TargetType) {
KRProgressHUD.show()
}
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
KRProgressHUD.dismiss()
}
func process(_ result: Result<Response, MoyaError>, target: TargetType) -> Result<Response, MoyaError> {
// 在这里对请求进行统一处理(比如有加密,可以统一进行解密)
if let value = result.value, let _ = try? JSONSerialization.jsonObject(with: value.data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any] {
let ob = ["code": 666, "description": "牛逼大发了"] as [String : Any]
let data = try! JSONSerialization.data(withJSONObject: ob, options: JSONSerialization.WritingOptions.fragmentsAllowed)
let response = Response(statusCode: value.statusCode, data: data, request: value.request, response: value.response)
let res = Result<Response, MoyaError>.init(value: response)
return res
}
return result
}
}
// 生成请求封装类
let lawProvider = MoyaProvider<LawApi>(endpointClosure: endpointClosure, plugins: [LoadingPlugin()])
enum LawApi {
case getUserInfo
case getNewsList(pageNo: Int = kPAGENO, pageSize: Int = kPAGESIZE)
case getNewsDetail(id: String)
}
extension LawApi: TargetType {
var baseURL: URL {
return URL(string: "http://xxoo/parse/rest.q4w")!
}
var path: String {
return ""
}
var method: Moya.Method {
return .post
}
var sampleData: Data {
return "{}".data(using: String.Encoding.utf8)!
}
var task: Task {
// 公共参数
var params: [String: Any] = ["appsid": "Gz1qYLXeBW8MZuUfDlr9wsAYuVS1cZFMJY9BbaF842L2gRps747o4w=="]
// 收集参数
switch self {
case .getUserInfo:
params["cfg"] = "com.lawyee.lam.web.parse.dto.LamUserDto@getUserInfo"
case let .getNewsList(pageNo, pageSize):
params["cfg"] = "com.lawyee.lam.web.parse.dto.LamNewsDto@getNewsList"
params["pageNo"] = pageNo
params["pageSize"] = pageSize
case .getNewsDetail(let id):
params["cfg"] = "com.lawyee.lam.web.parse.dto.LamNewsDto@getNewsDetail"
params["id"] = id
}
// 发起请求
return .requestParameters(parameters: params, encoding: URLEncoding.default)
}
var headers: [String : String]? {
return ["devtype": "wechat", "devid": "af533cbd0168f046e60817b04fd5db7f2057"]
}
}