熟悉swift的朋友应该都知道一款优秀的三方库Moya,插件版网络请求是真香,于是乎借鉴思路制作一款纯oc版本的插件网络请求库
熟悉oc的朋友又应该都知道一款优秀的三方库YTKNetwork,基于对象的协议版网络请求,然后他的批量网络请求和链式网络请求也超级香
结合两者部分优点,制作一款纯OC版 批量 和 链式 插件版网络请求库。
目前整理九款插件:解析插件,缓存插件,配置自建证书插件,加载提示插件,修改请求插件,日志抓包插件,错误码插件,刷新插件,网络错误和空数据插件
- English Documents
插件版网络请求可以更方便快捷的定制专属网络请求,并且支持批量操作,链式操作
主要功能清单如下:
KJBaseNetworking:网络请求基类,基本的网络请求,上传下载文件等方法
/// 根路径地址
@property (nonatomic, strong, class) NSString *baseURL;
/// 基本参数,类似:userID,token等
@property (nonatomic, strong, class) NSDictionary *baseParameters;
KJNetworkingRequest:请求体,设置网络请求相关参数,其中包含参数,请求方式,插件等等
KJNetworkingResponse:响应请求结果,获取插件之间产生的数据等等
KJNetworkingType:汇总所有枚举和回调声明
KJNetworkBasePlugin:插件基类,插件父类
KJNetworkPluginManager:插件管理器,中枢神经
/// 插件版网络请求
/// @param request 请求体
/// @param success 成功回调
/// @param failure 失败回调
+ (void)HTTPPluginRequest:(KJNetworkingRequest *)request
success:(KJNetworkPluginSuccess)success
failure:(KJNetworkPluginFailure)failure;
KJNetworkingDelegate:插件协议,管理网络请求结果
/// 开始准备网络请求
/// @param request 请求相关数据
/// @param endRequest 是否结束下面的网络请求
/// @return 返回准备插件处理后的数据
- (KJNetworkingResponse *)prepareWithRequest:(KJNetworkingRequest *)request endRequest:(BOOL *)endRequest;
/// 网络请求开始时刻请求
/// @param request 请求相关数据
/// @param stopRequest 是否停止网络请求
/// @return 返回网络请求开始时刻插件处理后的数据
- (KJNetworkingResponse *)willSendWithRequest:(KJNetworkingRequest *)request stopRequest:(BOOL *)stopRequest;
/// 成功接收数据
/// @param request 请求相关数据
/// @param againRequest 是否需要再次请求该网络
/// @return 返回成功插件处理后的数据
- (KJNetworkingResponse *)succeedWithRequest:(KJNetworkingRequest *)request againRequest:(BOOL *)againRequest;
/// 失败处理
/// @param request 请求相关数据
/// @param againRequest 是否需要再次请求该网络
/// @return 返回失败插件处理后的数据
- (KJNetworkingResponse *)failureWithRequest:(KJNetworkingRequest *)request againRequest:(BOOL *)againRequest;
/// 准备返回给业务逻辑时刻调用
/// @param request 请求相关数据
/// @param error 错误信息
/// @return 返回最终加工之后的数据
- (KJNetworkingResponse *)processSuccessResponseWithRequest:(KJNetworkingRequest *)request error:(NSError **)error;
目前已有13款插件供使用:
// 测试链式网络请求
- (void)testChainNetworking{
XCTestExpectation * expectation = [self expectationWithDescription:@"test chain."];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.httpbin.org";
request.path = @"/ip";
request.responseSerializer = KJSerializerJSON;
[KJNetworkChainManager HTTPChainRequest:request failure:^(NSError * error) {
XCTFail(@"%@", error.localizedDescription);
}]
.chain(^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) {
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.ip = @"https://www.httpbin.org";
request.path = @"/post";
request.params = {
"ip": responseObject["origin"]
};
return request;
})
.lastChain(^(id _Nonnull responseObject) {
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:300 handler:nil];
}
更多关于链式插件网络处理.
// 测试批量网络请求
- (void)testBatchNetworking{
XCTestExpectation * expectation = [self expectationWithDescription:@"test batch."];
NSMutableArray * array = [NSMutableArray array];
{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/headers";
request.responseSerializer = KJSerializerJSON;
[array addObject:request];
}{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/ip";
[array addObject:request];
}
KJBatchConfiguration * configuration = [KJBatchConfiguration sharedBatch];
configuration.maxQueue = 3;
configuration.requestArray = array.mutableCopy;
[KJNetworkBatchManager HTTPBatchRequestConfiguration:configuration reconnect:^BOOL(NSArray * _Nonnull reconnectArray) {
return YES;
} complete:^(NSArray * _Nonnull result) {
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:300 handler:nil];
}
更多关于批量插件网络处理.