public enum AHVideoAPi { case fetchVideoCategory case fetchVideoCategoryDetail(_ id:Int) }
extension AHVideoAPi : TargetType{ public var baseURL: URL { return URL(string: "https://api.apiopen.top")! } public var path: String { switch self { case .fetchVideoCategory: return "/videoCategory" case .fetchVideoCategoryDetail: return "/videoCategoryDetails" } } public var method: Moya.Method { switch self { case .fetchVideoCategory,.fetchVideoCategoryDetail: return .post } } public var sampleData: Data { return "{}".data(using: String.Encoding.utf8)! } public var task: Task { switch self { case .fetchVideoCategory: return .requestPlain case .fetchVideoCategoryDetail(let id): let params: [String: Any] = ["id":id] return .requestParameters(parameters: params,encoding: URLEncoding.default) } } public var headers: [String : String]? { return nil }
Api类就是Moya的常规使用,这里就不多做解释了
请求案例
1 2 3 4 5
AHNetwork.request(MultiTarget(AHVideoAPi.fetchVideoCategory), success: { (_) in /// do something success }) { (_) in /// do something error }