GLD_NetworkAPIManager.m
5.1 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
//
// GLD_NetworkAPIManager.m
// GLD_Networking
//
// Created by yiyangkeji on 2017/6/27.
// Copyright © 2017年 yiyangkeji. All rights reserved.
//
#import <CommonCrypto/CommonDigest.h>
#import "GLD_NetworkAPIManager.h"
#import "GLD_CacheManager.h"
#import "GLD_NetworkClient.h"
#import "GLD_NetworkError.h"
@implementation GLD_APIConfiguration
- (instancetype)init {
if (self = [super init]) {
self.useHttps = YES;
self.requestType = gld_networkRequestTypePOST;
}
return self;
}
@end
@interface GLD_NetworkAPIManager ()
@property (nonatomic, strong)NSMutableDictionary *requestCaches;
@end
@implementation GLD_NetworkAPIManager
- (void)dealloc{
[GLD_NetworkAPIManager cancelAlltask];
}
+ (void)cancelTaskWith:(NSNumber *)taskIdentifier{
[GLD_NetworkClient cancleTaskWithTaskIdentifier:taskIdentifier];
}
+ (void)cancelAlltask{
[GLD_NetworkClient cancelAlltask];
}
- (NSNumber *)dispatchDataTaskWith:(GLD_APIConfiguration *)config andCompletionHandler:(completionHandleBlock)completionHandle{
//防止多次相同网络请求
NSMutableString *requestCache = [NSMutableString stringWithString:config.urlPath];;
if (config) {
for (NSString *key in config.requestParameters.allKeys) {
[requestCache stringByAppendingPathComponent:key];
}
}
NSNumber *task = [self.requestCaches objectForKey:requestCache];
if (task && task.integerValue != -1) return @(-1);
NSString *cacheKey;
if(config.cacheValidTimeInterval > 0){
NSMutableString *stringM = [NSMutableString stringWithString:config.urlPath];
NSMutableArray *keyArr = [config.requestHeader.allKeys mutableCopy];
if (keyArr.count > 0) {
[keyArr sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString * _Nonnull obj2) {
return [obj1 compare:obj2];
}];
}
for (NSString *key in keyArr) {
[stringM appendString:[NSString stringWithFormat:@"&%@=%@",key,config.requestHeader[key]]];
}
cacheKey = [self md5WithString:stringM.copy];
GLD_Cache *cache = [[GLD_CacheManager shareCacheManager] objectForKey:cacheKey];
if (cache.isValid) {
completionHandle ? completionHandle(nil,cache.data) : nil;
return @-1;
}else{
[[GLD_CacheManager shareCacheManager] removeObjectForKey:cacheKey];
}
// cacheKey __NSCFString * @"5444c431ac01ce2e14afee2e6b2f694f" 0x00000001742799c0
}
WS(weakSelf);
NSNumber *taskIdentifier = [[GLD_NetworkClient shareInstance] dispatchTaskWithPath:config.urlPath useHttps:config.useHttps requestType:config.requestType params:config.requestParameters headers:config.requestHeader completionHandle:^(NSURLResponse * response, id data, NSError *error) {
[weakSelf.requestCaches removeObjectForKey:requestCache];
if (error.code == NSURLErrorCancelled) {
NSLog(@"请求被取消了~");
return ;
}
if (!error && config.cacheValidTimeInterval > 0) {
GLD_Cache *cacheData = [GLD_Cache cacheWithData:data validTime:config.cacheValidTimeInterval];
// [[GLD_CacheManager shareCacheManager] setObjcet:cacheData forKey:cacheKey];
[[GLD_CacheManager shareCacheManager] setObjcet:cacheData key:cacheKey];
}
completionHandle ? completionHandle([weakSelf formatError:error], data):nil;
}];
[self.requestCaches setObject:taskIdentifier forKey:requestCache];
return taskIdentifier;
}
- (NSError *)formatError:(NSError *)error{
if(!error)return error;
switch (error.code) {
case NSURLErrorCancelled:
error = gld_Error(gld_CanceledErrorNotice, gld_NetworkTaskErrorCanceled);
break;
case NSURLErrorTimedOut: error = gld_Error(gld_TimeoutErrorNotice, gld_NetworkTaskErrorTimeOut);
break;
case NSURLErrorCannotFindHost:
case NSURLErrorCannotConnectToHost:
case NSURLErrorNotConnectedToInternet: error = gld_Error(gld_NetworkErrorNotice, gld_NetworkTaskErrorCannotConnectedToInternet);
break;
default: {
error = gld_Error(gld_DefaultErrorNotice, gld_NetworkTaskErrorDefault);
} break;
}
return error;
}
- (NSString *)md5WithString:(NSString *)string {
const char *cStr = [string UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
// CC_MD5( cStr, strlen(cStr), result );
return [[[NSString alloc] initWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
] lowercaseString];
}
- (NSMutableDictionary *)requestCaches{
if (!_requestCaches) {
_requestCaches = [NSMutableDictionary dictionary];
}
return _requestCaches;
}
@end