forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodingNetAPIClient.m
More file actions
executable file
·308 lines (273 loc) · 12.9 KB
/
CodingNetAPIClient.m
File metadata and controls
executable file
·308 lines (273 loc) · 12.9 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
//
// CodingNetAPIClient.m
// Coding_iOS
//
// Created by 王 原闯 on 14-7-30.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kNetworkMethodName @[@"Get", @"Post", @"Put", @"Delete"]
#import "CodingNetAPIClient.h"
#import "Login.h"
@implementation CodingNetAPIClient
static CodingNetAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
+ (CodingNetAPIClient *)sharedJsonClient {
dispatch_once(&onceToken, ^{
_sharedClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[NSObject baseURLStr]]];
});
return _sharedClient;
}
+ (id)changeJsonClient{
_sharedClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[NSObject baseURLStr]]];
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
self.securityPolicy.allowInvalidCertificates = YES;
return self;
}
- (void)requestJsonDataWithPath:(NSString *)aPath
withParams:(NSDictionary*)params
withMethodType:(NetworkMethod)method
andBlock:(void (^)(id data, NSError *error))block{
[self requestJsonDataWithPath:aPath withParams:params withMethodType:method autoShowError:YES andBlock:block];
}
- (void)requestJsonDataWithPath:(NSString *)aPath
withParams:(NSDictionary*)params
withMethodType:(NetworkMethod)method
autoShowError:(BOOL)autoShowError
andBlock:(void (^)(id data, NSError *error))block{
if (!aPath || aPath.length <= 0) {
return;
}
//log请求数据
DebugLog(@"\n===========request===========\n%@\n%@:\n%@", kNetworkMethodName[method], aPath, params);
aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 发起请求
switch (method) {
case Get:{
//所有 Get 请求,增加缓存机制
NSMutableString *localPath = [aPath mutableCopy];
if (params) {
[localPath appendString:params.description];
}
[self GET:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
id error = [self handleResponse:responseObject autoShowError:autoShowError];
if (error) {
responseObject = [NSObject loadResponseWithPath:localPath];
block(responseObject, error);
}else{
if ([responseObject isKindOfClass:[NSDictionary class]]) {
//判断数据是否符合预期,给出提示
if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
if (responseObject[@"data"][@"too_many_files"]) {
if (autoShowError) {
[NSObject showHudTipStr:@"文件太多,不能正常显示"];
}
}
}
[NSObject saveResponseData:responseObject toPath:localPath];
}
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
!autoShowError || [NSObject showError:error];
id responseObject = [NSObject loadResponseWithPath:localPath];
block(responseObject, error);
}];
break;}
case Post:{
[self POST:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
id error = [self handleResponse:responseObject autoShowError:autoShowError];
if (error) {
block(nil, error);
}else{
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
!autoShowError || [NSObject showError:error];
block(nil, error);
}];
break;}
case Put:{
[self PUT:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
id error = [self handleResponse:responseObject autoShowError:autoShowError];
if (error) {
block(nil, error);
}else{
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
!autoShowError || [NSObject showError:error];
block(nil, error);
}];
break;}
case Delete:{
[self DELETE:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
id error = [self handleResponse:responseObject autoShowError:autoShowError];
if (error) {
block(nil, error);
}else{
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
!autoShowError || [NSObject showError:error];
block(nil, error);
}];
break;}
default:
break;
}
}
-(void)requestJsonDataWithPath:(NSString *)aPath file:(NSDictionary *)file withParams:(NSDictionary *)params withMethodType:(NetworkMethod)method andBlock:(void (^)(id, NSError *))block{
//log请求数据
DebugLog(@"\n===========request===========\n%@:\n%@", aPath, params);
aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Data
NSData *data;
NSString *name, *fileName;
if (file) {
UIImage *image = file[@"image"];
// 缩小到最大 800x800
// image = [image scaledToMaxSize:CGSizeMake(500, 500)];
// 压缩
data = UIImageJPEGRepresentation(image, 1.0);
if ((float)data.length/1024 > 1000) {
data = UIImageJPEGRepresentation(image, 1024*1000.0/(float)data.length);
}
name = file[@"name"];
fileName = file[@"fileName"];
}
switch (method) {
case Post:{
AFHTTPRequestOperation *operation = [self POST:aPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (file) {
[formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
id error = [self handleResponse:responseObject];
if (error) {
block(nil, error);
}else{
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
[NSObject showError:error];
block(nil, error);
}];
[operation start];
break;
}
default:
break;
}
}
- (void)reportIllegalContentWithType:(IllegalContentType)type
withParams:(NSDictionary*)params{
NSString *aPath;
switch (type) {
case IllegalContentTypeTweet:
aPath = @"/api/inform/tweet";
break;
case IllegalContentTypeTopic:
aPath = @"/api/inform/topic";
break;
case IllegalContentTypeProject:
aPath = @"/api/inform/project";
break;
case IllegalContentTypeWebsite:
aPath = @"/api/inform/website";
break;
default:
aPath = @"/api/inform/tweet";
break;
}
DebugLog(@"\n===========request===========\n%@:\n%@", aPath, params);
[self POST:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
}];
}
- (void)uploadImage:(UIImage *)image path:(NSString *)path name:(NSString *)name
successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
progerssBlock:(void (^)(CGFloat progressValue))progress{
NSData *data = UIImageJPEGRepresentation(image, 1.0);
if ((float)data.length/1024 > 1000) {
data = UIImageJPEGRepresentation(image, 1024*1000.0/(float)data.length);
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, str];
DebugLog(@"\nuploadImageSize\n%@ : %.0f", fileName, (float)data.length/1024);
AFHTTPRequestOperation *operation = [self POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
id error = [self handleResponse:responseObject];
if (error && failure) {
failure(operation, error);
}else{
success(operation, responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"Error: %@ ***** %@", operation.responseString, error);
if (failure) {
failure(operation, error);
}
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
CGFloat progressValue = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
if (progress) {
progress(progressValue);
}
}];
[operation start];
}
- (void)uploadVoice:(NSString *)file
withPath:(NSString *)path
withParams:(NSDictionary*)params
andBlock:(void (^)(id data, NSError *error))block {
if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
return;
}
NSData *data = [NSData dataWithContentsOfFile:file];
NSString *fileName = [file lastPathComponent];
DebugLog(@"\nuploadVoiceSize\n%@ : %.0f", fileName, (float)data.length/1024);
AFHTTPRequestOperation *operation = [self POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"audio/amr"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
DebugLog(@"\n===========response===========\n%@:\n%@", path, responseObject);
id error = [self handleResponse:responseObject autoShowError:YES];
if (error) {
block(nil, error);
}else{
block(responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DebugLog(@"\n===========response===========\n%@:\n%@", path, error);
[NSObject showError:error];
block(nil, error);
}];
[operation start];
}
@end