-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHNetworkModule.m
More file actions
398 lines (344 loc) · 18.5 KB
/
Copy pathGHNetworkModule.m
File metadata and controls
398 lines (344 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// GHNetworkModule.m
// GHNetworkModule
//
// Created by Qincc on 2020/12/12.
//
#import "GHNetworkModule.h"
#import <AFNetworking/AFNetworking.h>
#import "GHNetworkLogger.h"
#import "GHNetworkConfigure.h"
#import "GHNetworkResponse.h"
#import "GHNetworkRequest.h"
#import "GHNetworkCache.h"
@interface GHNetworkModule ()
@property (nonatomic, strong) NSMutableDictionary *reqeustDictionary;
@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
@end
@implementation GHNetworkModule
+ (nonnull instancetype)share
{
static dispatch_once_t onceToken;
static GHNetworkModule *manager = nil;
dispatch_once(&onceToken, ^{
manager = [[self alloc] init];
});
return manager;
}
- (instancetype)init
{
self = [super init];
if (self) {
_requestInterceptorObjectArray = [NSMutableArray arrayWithCapacity:3];
_responseInterceptorObjectArray = [NSMutableArray arrayWithCapacity:3];
_reqeustDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
- (AFHTTPSessionManager *)sessionManager
{
if (_sessionManager == nil){
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.HTTPMaximumConnectionsPerHost = 10; // 同一IP最大并发数
_sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
_sessionManager.responseSerializer = AFHTTPResponseSerializer.serializer; // 返回二进制,不可变更,返回值处理逻辑都是基于byte处理的
_sessionManager.securityPolicy = [self customSecurityPolicy];
}
return _sessionManager;
}
- (AFSecurityPolicy *)customSecurityPolicy {
NSData *data = [NSData dataWithContentsOfFile:GHNetworkConfigure.share.certificatePath];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = NO;
securityPolicy.validatesDomainName = NO;
securityPolicy.pinnedCertificates = [NSSet.alloc initWithObjects:data, nil];
return securityPolicy;
}
// MARK: - 🔥 Nomal Request
- (NSString *)sendRequest:(GHNetworkRequest *)request cacheComplete:(GHNetworkResponseBlock)cacheResult networkComplete:(GHNetworkResponseBlock)result
{
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
NSString *key = [NSString stringWithFormat:@"%@%@", request.baseURL, request.methodPath];
if (cacheResult && GHNetworkConfigure.share.useCache) {
id value = [GHNetworkCache syncReadCache:GHNetworkConfigure.share.userToken key:key];
GHNetworkResponse *response = [GHNetworkResponse.alloc initWithCacheResponse:value];
cacheResult(response);
}
return [self requestWithRequest:[request generateRequest] complete:^(GHNetworkResponse *response) {
if (result) {
//缓存数据
result(response);
if (cacheResult && response.status == GHNetworkResponseStatusSuccess && GHNetworkConfigure.share.useCache) {
[GHNetworkCache asycWriteCache:GHNetworkConfigure.share.userToken key:key value:response.JSONObject completeBlock:^{
}];
}
}
}];
}
- (NSString *)sendRequestWithConfigBlock:(GHRequestConfigBlock)requestBlock cacheComplete:(GHNetworkResponseBlock)cacheResult networkComplete:(GHNetworkResponseBlock) result;
{
GHNetworkRequest *request = [[GHNetworkRequest alloc] init];
requestBlock(request);
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
NSString *key = [NSString stringWithFormat:@"%@%@", request.baseURL, request.methodPath];
if (cacheResult && GHNetworkConfigure.share.useCache) {
id value = [GHNetworkCache syncReadCache:GHNetworkConfigure.share.userToken key:key];
GHNetworkResponse *response = [GHNetworkResponse.alloc initWithCacheResponse:value];
cacheResult(response);
}
return [self requestWithRequest:[request generateRequest] complete:^(GHNetworkResponse *response) {
if (result) {
//缓存数据
result(response);
if (cacheResult && response.status == GHNetworkResponseStatusSuccess && GHNetworkConfigure.share.useCache) {
[GHNetworkCache asycWriteCache:GHNetworkConfigure.share.userToken key:key value:response.JSONObject completeBlock:nil];
}
}
}];
}
- (NSString *)requestWithRequest:(NSURLRequest *)request complete:(GHNetworkResponseBlock)complete
{
if (GHNetworkConfigure.share.startReachability &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWWAN &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWiFi) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"There seems to be a glitch in the network, please try again later", nil) code:-1009 userInfo:nil];
[self requestFinishedWithBlock:complete task:nil data:nil error:error];
return nil;
}
__block NSURLSessionDataTask *task = nil;
task = [self.sessionManager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error && [response isKindOfClass:NSHTTPURLResponse.class]) {
// 重写ERROR,重新code
NSHTTPURLResponse *rsp = (NSHTTPURLResponse *)response;
error = [NSError errorWithDomain:error.localizedDescription code:rsp.statusCode userInfo:error.userInfo];
}
[self.reqeustDictionary removeObjectForKey:@([task taskIdentifier])];
[self requestFinishedWithBlock:complete task:task data:responseObject error:error];
}];
NSString *requestId = [[NSString alloc] initWithFormat:@"%@", @([task taskIdentifier])];
self.reqeustDictionary[requestId] = task;
[task resume];
return requestId;
}
// MARK: - 🔥 Upload Request
- (NSString *_Nullable)sendUploadRequest:(nonnull GHNetworkRequest *)request formData:(NSData *)bodyData progress:(void (^)(NSProgress *uploadProgress))progress complete:(GHNetworkResponseBlock)result
{
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
return [self requestWithUploadRequest:[request generateRequest] formData:bodyData progress:progress complete:result];
}
- (NSString *_Nullable)sendUploadRequestWithConfigBlock:(nonnull GHRequestConfigBlock)requestBlock formData:(NSData *)bodyData progress:(void (^)(NSProgress *uploadProgress))progress complete:(nonnull GHNetworkResponseBlock)result
{
GHNetworkRequest *request = [[GHNetworkRequest alloc] init];
requestBlock(request);
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
return [self requestWithUploadRequest:[request generateRequest] formData:bodyData progress:progress complete:result];
}
- (NSString *)requestWithUploadRequest:(NSURLRequest *)request formData:(NSData *)bodyData progress:(void (^)(NSProgress *uploadProgress))progress complete:(GHNetworkResponseBlock)complete
{
if (GHNetworkConfigure.share.startReachability &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWWAN &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWiFi) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"There seems to be a glitch in the network, please try again later", nil) code:-1009 userInfo:nil];
[self requestFinishedWithBlock:complete task:nil data:nil error:error];
return nil;
}
__block NSURLSessionUploadTask *task = nil;
task = [self.sessionManager uploadTaskWithRequest:request fromData:bodyData progress:progress completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error && [response isKindOfClass:NSHTTPURLResponse.class]) {
// 重写ERROR,重新code
NSHTTPURLResponse *rsp = (NSHTTPURLResponse *)response;
error = [NSError errorWithDomain:error.localizedDescription code:rsp.statusCode userInfo:error.userInfo];
}
[self.reqeustDictionary removeObjectForKey:@([task taskIdentifier])];
/** ----- 自定义返回实体 ----- */
NSMutableDictionary *result = NSMutableDictionary.dictionary;
if (error == nil) {
NSMutableDictionary *dic = NSMutableDictionary.dictionary;
[dic setValue:response.URL.absoluteString forKey:@"uploadURL"];
[result setValue:@(200) forKey:@"code"];
[result setValue:dic forKey:@"data"];
[result setValue:@"Upload Success" forKey:@"message"];
} else {
[result setValue:@(error.code) forKey:@"code"];
[result setValue:error.domain forKey:@"message"];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
/** ----- 自定义返回实体 ----- */
[self requestFinishedWithBlock:complete task:task data:data error:error];
}];
NSString *requestId = [[NSString alloc] initWithFormat:@"%@", @([task taskIdentifier])];
self.reqeustDictionary[requestId] = task;
[task resume];
return requestId;
}
// MARK: - 🔥 Download Request
- (NSString *_Nullable)sendDownloadRequest:(nonnull GHNetworkRequest *)request destination:(NSURL * (^)(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response))destination progress:(void (^)(NSProgress *downloadProgress))progress complete:(nonnull GHNetworkResponseBlock)result
{
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
return [self requestWithDownloadRequest:[request generateRequest] destination:destination progress:progress complete:result];
}
- (NSString *_Nullable)sendDownloadRequestWithConfigBlock:(nonnull GHRequestConfigBlock)requestBlock destination:(NSURL * (^)(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response))destination progress:(void (^)(NSProgress *downloadProgress))progress complete:(GHNetworkResponseBlock)result
{
GHNetworkRequest *request = [[GHNetworkRequest alloc] init];
requestBlock(request);
// 拦截器处理
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
return [self requestWithDownloadRequest:[request generateRequest] destination:destination progress:progress complete:result];
}
- (NSString *)requestWithDownloadRequest:(NSURLRequest *)request destination:(NSURL * (^)(NSURL *targetPath, NSURLResponse * _Nonnull response))destination progress:(void (^)(NSProgress *downloadProgress))progress complete:(GHNetworkResponseBlock)complete
{
if (GHNetworkConfigure.share.startReachability &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWWAN &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWiFi) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"There seems to be a glitch in the network, please try again later", nil) code:-1009 userInfo:nil];
[self requestFinishedWithBlock:complete task:nil data:nil error:error];
return nil;
}
__block NSURLSessionDownloadTask *task = nil;
task = [self.sessionManager downloadTaskWithRequest:request progress:progress destination:destination completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (error && [response isKindOfClass:NSHTTPURLResponse.class]) {
// 重写ERROR,重新code
NSHTTPURLResponse *rsp = (NSHTTPURLResponse *)response;
error = [NSError errorWithDomain:error.localizedDescription code:rsp.statusCode userInfo:error.userInfo];
}
[self.reqeustDictionary removeObjectForKey:@([task taskIdentifier])];
/** ----- 自定义返回实体 ----- */
NSMutableDictionary *result = NSMutableDictionary.dictionary;
if (error == nil) {
NSMutableDictionary *dic = NSMutableDictionary.dictionary;
[dic setValue:response.URL.absoluteString forKey:@"downloadURL"];
[dic setValue:filePath.absoluteString forKey:@"filePath"];
[result setValue:@(200) forKey:@"code"];
[result setValue:dic forKey:@"data"];
[result setValue:@"Download Success" forKey:@"message"];
} else {
[result setValue:@(error.code) forKey:@"code"];
[result setValue:error.domain forKey:@"message"];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];
/** ----- 自定义返回实体 ----- */
[self requestFinishedWithBlock:complete task:task data:data error:error];
}];
NSString *requestId = [[NSString alloc] initWithFormat:@"%@", @([task taskIdentifier])];
self.reqeustDictionary[requestId] = task;
[task resume];
return requestId;
}
// MARK: - 🔥 Finish Request
- (void)requestFinishedWithBlock:(GHNetworkResponseBlock)blk task:(NSURLSessionTask *)task data:(NSData *)data error:(NSError *)error
{
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithTask:task data:data error:error];
if (error) {
GHNetworkResponse *rsp = [[GHNetworkResponse alloc] initWithRequestId:@([task taskIdentifier]) request:task.originalRequest responseData:data error:error];
for (id obj in self.responseInterceptorObjectArray)
{
if ([obj respondsToSelector:@selector(validatorResponse:)])
{
[obj validatorResponse:rsp];
break;
}
}
blk ? blk(rsp) : nil;
} else {
GHNetworkResponse *rsp = [[GHNetworkResponse alloc] initWithRequestId:@([task taskIdentifier]) request:task.originalRequest responseData:data status:GHNetworkResponseStatusSuccess];
for (id obj in self.responseInterceptorObjectArray)
{
if ([obj respondsToSelector:@selector(validatorResponse:)])
{
[obj validatorResponse:rsp];
break;
}
}
blk ? blk(rsp) : nil;
}
}
// MARK: - 🔥 Cancle Request
- (void)cancelRequestWithRequestID:(nonnull NSString *)requestID
{
if (requestID) {
NSURLSessionDataTask *requestOperation = self.reqeustDictionary[requestID];
[requestOperation cancel];
[self.reqeustDictionary removeObjectForKey:requestID];
}
}
- (void)cancelRequestWithRequestIDList:(nonnull NSArray<NSString *> *)requestIDList
{
for (NSString *requestId in requestIDList){
[self cancelRequestWithRequestID:requestId];
}
}
// MARK: - 🔥 Intercept Request
- (BOOL)needRequestInterceptor:(GHNetworkRequest *)request
{
BOOL need = YES;
for (id obj in self.requestInterceptorObjectArray) {
if ([obj respondsToSelector:@selector(needRequestWithRequest:)]) {
need = [obj needRequestWithRequest:request];
if (need) {
break;
}
}
}
return need;
}
- (NSString *)requestMultipartData:(GHNetworkRequest *)request data:(NSData *)data imageKey:(NSString *)imageKey progress:(void (^)(NSProgress *downloadProgress))progress complete:(GHNetworkResponseBlock)complete {
__block NSURLSessionUploadTask *task = nil;
if (GHNetworkConfigure.share.startReachability &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWWAN &&
[[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] != AFNetworkReachabilityStatusReachableViaWiFi) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"There seems to be a glitch in the network, please try again later", nil) code:-1009 userInfo:nil];
[self requestFinishedWithBlock:complete task:nil data:nil error:error];
return nil;
}
if (![self needRequestInterceptor:request]) {
if (GHNetworkConfigure.share.enableDebug) NSLog(@"该请求已经取消");
return nil;
}
if (GHNetworkConfigure.share.enableDebug) [GHNetworkLogger logDebugInfoWithRequest:request];
task = [self.sessionManager uploadTaskWithStreamedRequest:[request formDataRequest:data imageKey:imageKey] progress:progress completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error && [response isKindOfClass:NSHTTPURLResponse.class]) {
// 重写ERROR,重新code
NSHTTPURLResponse *rsp = (NSHTTPURLResponse *)response;
error = [NSError errorWithDomain:error.localizedDescription code:rsp.statusCode userInfo:error.userInfo];
}
[self.reqeustDictionary removeObjectForKey:@([task taskIdentifier])];
[self requestFinishedWithBlock:complete task:task data:responseObject error:error];
}];
NSString *requestId = [[NSString alloc] initWithFormat:@"%@", @([task taskIdentifier])];
self.reqeustDictionary[requestId] = task;
[task resume];
return requestId;
}
- (void)clearCache:(BOOL)isAsyc {
[GHNetworkCache clearCache:GHNetworkConfigure.share.userToken isAsyc:isAsyc];
}
@end