No Description

DSBRequestProxy.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // DSBDataProxy.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 2016/12/6.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "DSBRequestProxy.h"
  9. #import "DSBBaseRequest.h"
  10. #import "DSBResponseHeader.h"
  11. #import "DSBNetwork.h"
  12. @implementation DSBRequestProxy
  13. + (NSURLSessionDataTask *)request:(DSBBaseRequest *)request
  14. success:(void (^)(NSURLSessionDataTask *, NSDictionary *, DSBBaseRequest *))success
  15. failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
  16. NSAssert(request.urlString != nil, @"request url could not be nil");
  17. NSURLSessionDataTask *tk = [DSBNetwork POST:request.urlString
  18. parameters:request.para
  19. progress:^(NSProgress *uploadProgress) {
  20. } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
  21. DSBResponseHeader *header = [self responseHeader:responseJson];
  22. if (header.code == DSBResponseCodeNormal) {
  23. if ([responseJson isKindOfClass:[NSDictionary class]]) {
  24. success(task, responseJson, request);
  25. }
  26. } else {
  27. if (failure) {
  28. failure(task, header, request);
  29. }
  30. }
  31. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  32. if (failure) {
  33. DSBResponseHeader *header = [DSBResponseHeader new];
  34. header.code = (DSBResponseCode)[error code];
  35. header.msg = error.description;
  36. failure(task, header, request);
  37. }
  38. }];
  39. return tk;
  40. }
  41. + (NSArray<NSURLSessionDataTask *> *)requestSerial:(NSArray<DSBBaseRequest *> *)requests
  42. success:(void (^)(NSURLSessionDataTask *, NSDictionary *, DSBBaseRequest *))success
  43. failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
  44. if (requests.count == 0) {
  45. return nil;
  46. }
  47. NSMutableArray *taskArray = [NSMutableArray arrayWithCapacity:requests.count];
  48. NSMutableArray *responseArray = [NSMutableArray arrayWithCapacity:requests.count];
  49. for (NSInteger idx = 0; idx < requests.count; idx++) {
  50. [responseArray addObject:[NSNull null]];
  51. }
  52. __block NSInteger callbackStartIdx = 0;
  53. for (DSBBaseRequest *req in requests) {
  54. NSURLSessionDataTask *tk = [DSBNetwork POST:req.urlString
  55. parameters:req.para
  56. progress:^(NSProgress *uploadProgress) {
  57. } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
  58. DSBResponseHeader *header = [self responseHeader:responseJson];
  59. if (header.code == DSBResponseCodeNormal && [responseJson isKindOfClass:[NSDictionary class]]) {
  60. [responseArray replaceObjectAtIndex:[taskArray indexOfObject:task] withObject:responseJson];
  61. for (NSInteger idx = callbackStartIdx; idx < requests.count; idx++) {
  62. id obj = responseArray[idx];
  63. if ([obj isKindOfClass:[NSDictionary class]]) {
  64. callbackStartIdx++;
  65. success(task, responseJson, req);
  66. } else {
  67. break;
  68. }
  69. }
  70. } else {
  71. if (failure) {
  72. [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
  73. [task cancel];
  74. }];
  75. failure(task, header, req);
  76. }
  77. }
  78. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  79. if (failure) {
  80. [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
  81. [task cancel];
  82. }];
  83. DSBResponseHeader *header = [DSBResponseHeader new];
  84. header.code = (DSBResponseCode)[error code];
  85. header.msg = error.description;
  86. failure(task, header, req);
  87. }
  88. }];
  89. [taskArray addObject:tk];
  90. }
  91. return taskArray;
  92. }
  93. + (NSArray<NSURLSessionDataTask *> *)requestBatch:(NSArray<DSBBaseRequest *> *)requestArray
  94. success:(void (^)(NSArray<NSURLSessionDataTask *> *, NSArray<NSDictionary *> *, NSArray<DSBBaseRequest *> *))success
  95. failure:(void (^)(NSURLSessionDataTask *, DSBResponseHeader *, DSBBaseRequest *))failure {
  96. if (requestArray.count == 0) {
  97. return nil;
  98. }
  99. NSMutableArray *taskArray = [NSMutableArray arrayWithCapacity:requestArray.count];
  100. NSMutableArray *responseArray = [NSMutableArray arrayWithCapacity:requestArray.count];
  101. for (NSInteger idx = 0; idx < requestArray.count; idx++) {
  102. [responseArray addObject:[NSNull null]];
  103. }
  104. __block NSInteger responseCount = 0;
  105. for (DSBBaseRequest *req in requestArray) {
  106. NSURLSessionDataTask *tk = [DSBNetwork POST:req.urlString
  107. parameters:req.para
  108. progress:^(NSProgress *uploadProgress) {
  109. } success:^(NSURLSessionDataTask *task, NSDictionary *responseJson) {
  110. DSBResponseHeader *header = [self responseHeader:responseJson];
  111. if (header.code == DSBResponseCodeNormal && [responseJson isKindOfClass:[NSDictionary class]]) {
  112. [responseArray replaceObjectAtIndex:[taskArray indexOfObject:task] withObject:responseJson];
  113. responseCount++;
  114. if (responseCount == requestArray.count) {
  115. success(taskArray, responseArray, requestArray);
  116. }
  117. } else {
  118. if (failure) {
  119. [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
  120. [task cancel];
  121. }];
  122. failure(task, header, req);
  123. }
  124. }
  125. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  126. if (failure) {
  127. [taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *obj, NSUInteger idx, BOOL *stop) {
  128. [task cancel];
  129. }];
  130. DSBResponseHeader *header = [DSBResponseHeader new];
  131. header.code = (DSBResponseCode)[error code];
  132. header.msg = error.description;
  133. failure(task, header, req);
  134. }
  135. }];
  136. [taskArray addObject:tk];
  137. }
  138. return taskArray;
  139. }
  140. #pragma mark - Method
  141. + (DSBResponseHeader *)responseHeader:(NSDictionary *)json {
  142. DSBResponseHeader *header = [DSBResponseHeader new];
  143. if ([json isKindOfClass:[NSDictionary class]]) {
  144. NSString *code = json[@"code"];
  145. if ([code isKindOfClass:[NSString class]]) {
  146. header.code = (DSBResponseCode)[code integerValue];
  147. }
  148. NSString *msg = json[@"message"];
  149. if ([msg isKindOfClass:[NSString class]]) {
  150. header.msg = msg;
  151. }
  152. }
  153. return header;
  154. }
  155. @end