修改 KJNetworkingRequest 和 获取 KJNetworkingResponse 插件git
本文主要介绍如何使用 KJNetworkThiefPlugin 插件,来达到修改请求体和获取结果数据的隐藏用法,偷梁换柱小偷行为?因此干脆就给它命名为小偷插件,哈哈哈!请忽略个人搞笑github
设置请求ip,路径,参数,请求方式,插件等等等缓存
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = @"/j/app/radio/channels";
复制代码
设置需求插件pluginsmarkdown
KJNetworkThiefPlugin * plugin = [[KJNetworkThiefPlugin alloc] init];
request.plugins = @[plugin];
复制代码
就是这么简单简洁的使用,网络
[KJNetworkPluginManager HTTPPluginRequest:request success:^(KJNetworkingRequest * _Nonnull request, id _Nonnull responseObject) {
NSLog(@"----%@",responseObject);
} failure:^(KJNetworkingRequest * _Nonnull request, NSError * _Nonnull error) {
NSLog(@"----%@",error);
}];
复制代码
到此,小偷插件使用就算初步完成,是否是特别轻松简单app
下面继续接着奏乐接着舞,oop
KJNetworkThiefPlugin * plugin = [[KJNetworkThiefPlugin alloc] init];
plugin.kChangeRequest = ^(KJNetworkingRequest * _Nonnull request) {
// 这里便可修改请求体,这里须要提醒一下
// 此处修改会影响后续网络请求,也就是说会改变你上面设置的 `request`
};
复制代码
其实通常状况下,咱们不会去更改这个请求体,毕竟顺着一条线下来咱们就已经设置是本身须要的请求体,固然这个也能够拿来作点骚操做,后面的慢慢讲,咱先一步一步的来测试
plugin.kGetResponse = ^(KJNetworkingResponse * _Nonnull response) {
// 这里能够拿到网络请求返回的原始数据
NSLog(@"🎷🎷🎷原汁原味的数据 = %@", response.responseObject);
// 准备插件处理以后的数据
if (response.opportunity == KJNetworkingRequestOpportunityPrepare) {
NSLog(@"🎷🎷🎷准备插件处理以后的数据 = %@", response.prepareResponse);
}
// 成功插件处理以后的数据
if (response.opportunity == KJNetworkingRequestOpportunitySuccess) {
NSLog(@"🎷🎷🎷成功插件处理以后的数据 = %@", response.successResponse);
}
// 失败插件处理以后的数据
if (response.opportunity == KJNetworkingRequestOpportunityFailure) {
NSLog(@"🎷🎷🎷失败插件处理以后的数据 = %@", response.failureResponse);
NSLog(@"🎷🎷🎷失败 = %@", response.error);
}
};
复制代码
备注说明:若是你使用的插件没有处理过成功数据或者失败数据,甚至任何一个插件数据,只要未经插件处理过,那么对应的该插件数据即为空,没必要大惊小怪spa
不知道你们在使用的过程当中,有没有碰见这样的状况:插件
KJNetworkThiefPlugin * plugin = [[KJNetworkThiefPlugin alloc] init];
// 该字段必须开启,才会再次调用网络请求
plugin.againRequest = YES;
plugin.kChangeRequest = ^(KJNetworkingRequest * _Nonnull request) {
// 场景一,失败以后更换ip
if (request.opportunity == KJNetworkingRequestOpportunityFailure) {
request.ip = @"https://www.baidu.com";
}
// 场景二,失败以后更换另一套网络接口
if (request.opportunity == KJNetworkingRequestOpportunityFailure) {
request.path = @"/other/path";
request.params = @{
@"token": @"XHFI-213-XDJHso-A0345",
@"userid": @"likeyou",
};
}
};
复制代码
我以为还有更多骚操做,能够玩一下,待发现ing..
插件版网络就是有这样一个弊端,最终成功或者失败回调出来的数据都是最后一个插件处理以后的数据,那么若是我想拿到中间某个插件处理以后的数据,常规的方法就不行,因而就有了下面这种骚操做来知足这些无理取闹的需求
KJNetworkCachePlugin * cache = [[KJNetworkCachePlugin alloc] init];
...
缓存插件相关操做
KJNetworkThiefPlugin * afterCache = [[KJNetworkThiefPlugin alloc] init];
afterCache.kGetResponse = ^(KJNetworkingResponse * _Nonnull response) {
// 获取缓存插件处理后的数据
};
KJNetworkAnslysisPlugin * anslysis = [[KJNetworkAnslysisPlugin alloc] init];
...
解析插件相关操做
KJNetworkThiefPlugin * afterAnslysis = [[KJNetworkThiefPlugin alloc] init];
afterAnslysis.kGetResponse = ^(KJNetworkingResponse * _Nonnull response) {
// 获取解析插件处理后的数据
};
// 这里设置好对应插件顺序便可
request.plugins = @[cache, afterCache, anslysis, afterAnslysis];
复制代码
我依然以为还有更多用法待发现,去吧!皮卡丘
提供完整的测试用例,本身去玩吧
- (void)testThiefPlugin{
XCTestExpectation * expectation = [self expectationWithDescription:@"test thief plugin."];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = @"/j/app/radio/channels";
request.responseSerializer = KJResponseSerializerJSON;
KJNetworkThiefPlugin * plugin = [[KJNetworkThiefPlugin alloc] init];
plugin.kGetResponse = ^(KJNetworkingResponse * _Nonnull response) {
// 这里能够拿到网络请求返回的原始数据
NSLog(@"🎷🎷🎷原汁原味的数据 = %@", response.responseObject);
};
request.plugins = @[plugin];
[KJNetworkPluginManager HTTPPluginRequest:request success:^(KJNetworkingRequest * _Nonnull request, id _Nonnull responseObject) {
NSLog(@"----%@",responseObject);
[expectation fulfill];
} failure:^(KJNetworkingRequest * _Nonnull request, NSError * _Nonnull error) {
XCTFail(@"%@", error.localizedDescription);
}];
[self waitForExpectationsWithTimeout:30 handler:nil];
}
复制代码
关于这个插件版网络库,即将开源使用,须要的朋友自行
pod 'KJNetworkPlugin' # 插件版网络
pod 'KJNetworkPlugin/Thief' # 偷梁换柱插件
复制代码
写东西着实累,老铁们以为有用还望点个星支持一下,传送门KJNetworkPlugin
后面有相关插件我也会慢慢补充...