RAC网络封装以及多个网络请求嵌套


封装RAC的网络请求方式

+ (RACSignal *)rac_request:(NSString *)apiName parameters:(NSDictionary *)parameters showError:(BOOL)showError {
    NSAssert(apiName != nil, @"identifier 不能为空");
    
    Class requestClass = NSClassFromString(apiName);
    NSAssert(requestClass != nil, @"未找到 %@ 类", apiName);
    
    RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) {
        YTKRequestWithAuth *api = [[requestClass alloc] init];
        
        NSAssert([api isKindOfClass:[YTKRequestWithAuth class]], @"class %@ must be a subclass of YTKRequestWithAuth", apiName);
        
        [api setRequestArgumentWithAuth:parameters];
        
        [api setSuccessCompletionBlock:^(__kindof YTKRequestWithAuth *request){
            if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
                [subscriber sendNext:request.responseString];
            } else {
                [subscriber sendNext:request.responseJSONObject];
            }
            [subscriber sendCompleted];
        }];
        
        [api setFailureCompletionBlock:^(__kindof YTKRequestWithAuth *request) {
            if (request.responseStatusCode == 200) {
                if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
                    [subscriber sendNext:request.responseString];
                } else {
                    [subscriber sendNext:request.responseJSONObject];
                }
                [subscriber sendCompleted];
            } else if (request.authStrategy == AuthStrategyTokenWithSign && request.responseStatusCode == 403) {
                NSLog(@"====    重新申请token   ========");
                // 如果请求失败是由于认证未通过则获取新的token后retry请求
                [AuthTokenManager refreshAuthTokenWithCompletion:^(NSString *token, NSError *tokenError) {
                    if (!tokenError) {
                        YTKRequestWithAuth *requestTwice = [[requestClass alloc] init];
                        requestTwice.requestArgumentWithAuth = request.requestArgumentWithAuth;
                        
                        [requestTwice startWithCompletionBlockWithSuccess:request.successCompletionBlock failure:^(__kindof YTKRequestWithAuth *request2) {
                            if (request.responseStatusCode == 200) {
                                if (request.responseSerializerType == YTKResponseSerializerTypeHTTP) {
                                    [subscriber sendNext:request.responseString];
                                } else {
                                    [subscriber sendNext:request.responseJSONObject];
                                }
                                [subscriber sendCompleted];
                            } else {
                                NSError *error = [self proccessResponseForRequest:request];
                                [subscriber sendError:error];
                            }
                        }];
                    } else {
//                        [subscriber sendError:tokenError];
                    }
                }];
            } else {
                NSError *error = [self proccessResponseForRequest:request];
                [subscriber sendError:error];
            }
        }];
        
        [api startWithAgent:nil];
        
        return [RACDisposable disposableWithBlock:^{
            [api stop];
        }];
    }] replayLazily];
    
    if (showError) {
        [signal subscribeError:^(NSError *error) {
        }];
    }
    return signal;
}
- (void)refreshData{
    [[FYAPIClient rac_request:@"WebGetDialogDetailInfo" parameters:@{@"dialog_tid":self.model.TID} showError:YES]subscribeNext:^(id x) {
       
    } completed:^{
        [self refreshDataSource];
    } ] ;
}
- (void)refreshDataSource{
    NSDictionary *params;
    if (pageIndex>0&&self.dataSource.count>0) {
        LastTestMsgModel *model = self.dataSource.firstObject;
        params = @{@"pagesize":@PAGESIZE,@"dialog_tid":self.model.TID,@"tid":model.TID};
    }else{
        params = @{@"pagesize":@PAGESIZE,@"dialog_tid":self.model.TID};
    }
    [[FYAPIClient rac_request:@"WebGetChatList" parameters:params showError:YES] subscribeNext:^(id x) {
        
    }];
}


你可能感兴趣的:(RAC网络封装以及多个网络请求嵌套)