OC版本的Moya插件网络

2022-01-14  本文已影响0人  弹吉他的少年

插件版网络请求架构,支持批量操作和链式操作

功能清单

插件版网络请求可以更方便快捷的定制专属网络请求,并且支持批量操作,链式操作

主要功能清单如下:

使用教程

Network版块

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;

Plugins

目前已有13款插件供使用:

Chain

// 测试链式网络请求
- (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];
}

更多关于链式插件网络处理.👒👒

Batch

// 测试批量网络请求
- (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<KJNetworkingRequest *> * _Nonnull reconnectArray) {
        return YES;
    } complete:^(NSArray<KJBatchResponse *> * _Nonnull result) {
        [expectation fulfill];
    }];
    
    [self waitForExpectationsWithTimeout:300 handler:nil];
}

更多关于批量插件网络处理.👒👒

最后

上一篇下一篇

猜你喜欢

热点阅读