123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- //
- // DSBDataProxy.m
- // FirstLink
- //
- // Created by ascii on 2016/12/6.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "DSBRequestProxy.h"
- #import "DSBBaseRequest.h"
- #import "DSBResponseHeader.h"
- #import "DSBNetwork.h"
- @implementation DSBRequestProxy
- + (NSURLSessionDataTask *)request:(DSBBaseRequest *)request
- success:(void (^)(NSURLSessionDataTask *, NSDictionary *, DSBBaseRequest *))success
- failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
- NSAssert(request.urlString != nil, @"request url could not be nil");
-
- NSURLSessionDataTask *tk = [DSBNetwork POST:request.urlString
- parameters:request.para
- progress:^(NSProgress *uploadProgress) {
-
- } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
- DSBResponseHeader *header = [self responseHeader:responseJson];
- if (header.code == DSBResponseCodeNormal) {
- if ([responseJson isKindOfClass:[NSDictionary class]]) {
- success(task, responseJson, request);
- }
- } else {
- if (failure) {
- failure(task, header, request);
- }
- }
- } failure:^(NSURLSessionDataTask *task, NSError *error) {
- if (failure) {
- DSBResponseHeader *header = [DSBResponseHeader new];
- header.code = (DSBResponseCode)[error code];
- header.msg = error.description;
- failure(task, header, request);
- }
- }];
- return tk;
- }
- + (NSArray<NSURLSessionDataTask *> *)requestSerial:(NSArray<DSBBaseRequest *> *)requests
- success:(void (^)(NSURLSessionDataTask *, NSDictionary *, DSBBaseRequest *))success
- failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
- if (requests.count == 0) {
- return nil;
- }
-
- NSMutableArray *taskArray = [NSMutableArray arrayWithCapacity:requests.count];
-
- NSMutableArray *responseArray = [NSMutableArray arrayWithCapacity:requests.count];
- for (NSInteger idx = 0; idx < requests.count; idx++) {
- [responseArray addObject:[NSNull null]];
- }
-
- __block NSInteger callbackStartIdx = 0;
- for (DSBBaseRequest *req in requests) {
- NSURLSessionDataTask *tk = [DSBNetwork POST:req.urlString
- parameters:req.para
- progress:^(NSProgress *uploadProgress) {
-
- } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
- DSBResponseHeader *header = [self responseHeader:responseJson];
- if (header.code == DSBResponseCodeNormal && [responseJson isKindOfClass:[NSDictionary class]]) {
- [responseArray replaceObjectAtIndex:[taskArray indexOfObject:task] withObject:responseJson];
-
- for (NSInteger idx = callbackStartIdx; idx < requests.count; idx++) {
- id obj = responseArray[idx];
-
- if ([obj isKindOfClass:[NSDictionary class]]) {
- callbackStartIdx++;
- success(task, responseJson, req);
- } else {
- break;
- }
- }
- } else {
- if (failure) {
- [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
- [task cancel];
- }];
- failure(task, header, req);
- }
- }
- } failure:^(NSURLSessionDataTask *task, NSError *error) {
- if (failure) {
- [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
- [task cancel];
- }];
-
- DSBResponseHeader *header = [DSBResponseHeader new];
- header.code = (DSBResponseCode)[error code];
- header.msg = error.description;
- failure(task, header, req);
- }
- }];
- [taskArray addObject:tk];
- }
-
- return taskArray;
- }
- + (NSArray<NSURLSessionDataTask *> *)requestBatch:(NSArray<DSBBaseRequest *> *)requestArray
- success:(void (^)(NSArray<NSURLSessionDataTask *> *, NSArray<NSDictionary *> *, NSArray<DSBBaseRequest *> *))success
- failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
- if (requestArray.count == 0) {
- return nil;
- }
-
- NSMutableArray *taskArray = [NSMutableArray arrayWithCapacity:requestArray.count];
-
- NSMutableArray *responseArray = [NSMutableArray arrayWithCapacity:requestArray.count];
- for (NSInteger idx = 0; idx < requestArray.count; idx++) {
- [responseArray addObject:[NSNull null]];
- }
-
- __block NSInteger responseCount = 0;
- for (DSBBaseRequest *req in requestArray) {
- NSURLSessionDataTask *tk = [DSBNetwork POST:req.urlString
- parameters:req.para
- progress:^(NSProgress *uploadProgress) {
-
- } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
- DSBResponseHeader *header = [self responseHeader:responseJson];
- if (header.code == DSBResponseCodeNormal && [responseJson isKindOfClass:[NSDictionary class]]) {
- [responseArray replaceObjectAtIndex:[taskArray indexOfObject:task] withObject:responseJson];
- responseCount++;
-
- if (responseCount == requestArray.count) {
- success(taskArray, responseArray, requestArray);
- }
- } else {
- if (failure) {
- [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
- [task cancel];
- }];
- failure(task, header, req);
- }
- }
- } failure:^(NSURLSessionDataTask *task, NSError *error) {
- if (failure) {
- [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
- [task cancel];
- }];
-
- DSBResponseHeader *header = [DSBResponseHeader new];
- header.code = (DSBResponseCode)[error code];
- header.msg = error.description;
- failure(task, header, req);
- }
- }];
- [taskArray addObject:tk];
- }
-
- return taskArray;
- }
- #pragma mark - Method
- + (DSBResponseHeader *)responseHeader:(NSDictionary *)json {
- DSBResponseHeader *header = [DSBResponseHeader new];
-
- if ([json isKindOfClass:[NSDictionary class]]) {
- NSString *code = json[@"code"];
- if ([code isKindOfClass:[NSString class]]) {
- header.code = (DSBResponseCode)[code integerValue];
- }
-
- NSString *msg = json[@"message"];
- if ([msg isKindOfClass:[NSString class]]) {
- header.msg = msg;
- }
- }
-
- return header;
- }
- @end
|