GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Coding-iOS/Coding_iOS/Util/Manager/CodingNetAPIClient.m at master · coding/Coding-iOS Expand file tree/ Copy pathCodingNetAPIClient.mMore file actions executable file ·444 lines (406 loc) · 19.7 KB / Copy pathCodingNetAPIClient.mFile metadata and controlsexecutable file ·444 lines (406 loc) · 19.7 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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 // // 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 CodingNetAPIClient *_eClient = nil; static dispatch_once_t onceToken; static dispatch_once_t e_Token; + (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)changeSharedJsonClient{ _sharedClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[NSObject baseURLStr]]]; return _sharedClient; } + (CodingNetAPIClient *)e_JsonClient { dispatch_once(&e_Token, ^{ _eClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[NSObject e_URLStr]]]; }); return _eClient; } + (id)changeE_JsonClient{ _eClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:[NSObject e_URLStr]]]; return _eClient; } - (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; } //CSRF - 跨站请求伪造 NSHTTPCookie *_CSRF = nil; for (NSHTTPCookie *tempC in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { if ([tempC.name isEqualToString:@"XSRF-TOKEN"]) { _CSRF = tempC; } } if (_CSRF) { [self.requestSerializer setValue:_CSRF.value forHTTPHeaderField:@"X-XSRF-TOKEN"]; } //log请求数据 DebugLog(@"\n===========request===========\n%@\n%@:\n%@", kNetworkMethodName[method], aPath, params); aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//卧槽了,有些把 params 放在 path 里面的 GET 方法 // aPath = [aPath stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]; // 发起请求 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); id responseObject = [NSObject loadResponseWithPath:localPath]; !autoShowError || (error.code == NSURLErrorNotConnectedToInternet && responseObject != nil) || [NSObject showError:error]; 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]; aPath = [aPath stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]; // Data NSData *data; NSString *name, *fileName; if (file) { UIImage *image = file[@"image"]; // 压缩 data = [image dataForCodingUpload]; 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{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [image dataForCodingUpload]; [self p_uploadImageData:data path:path name:name successBlock:success failureBlock:failure progerssBlock:progress]; }); // NSData *data = [image dataForCodingUpload]; // NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, [NSUUID UUID].UUIDString]; // DebugLog(@"\nuploadImageSize\n%@ : %.0f", fileName, (float)data.length/1024); // // __weak typeof(self) weakSelf = self; // void (^uploadBlock)(NSDictionary *) = ^(NSDictionary *uploadParams){ // AFHTTPRequestOperation *operation = [weakSelf POST:path parameters:uploadParams 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]; // }; // if ([path isEqualToString:@"https://up.qbox.me/"]) {//先拿 token // NSDictionary *params = @{ // @"fileName": fileName, // @"fileSize": @(data.length) // }; // [[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/upload_token/public/images" withParams:params withMethodType:Get andBlock:^(id data, NSError *error) { // if (data) { // NSDictionary *result = data[@"data"]; // NSMutableDictionary *uploadParams = @{}.mutableCopy; // uploadParams[@"token"] = result[@"uptoken"]; // uploadParams[@"x:time"] = result[@"time"]; // uploadParams[@"x:authToken"] = result[@"authToken"]; // uploadParams[@"x:userId"] = result[@"userId"]; // uploadParams[@"key"] = fileName; // uploadBlock(uploadParams); // } // }]; // }else{ // uploadBlock(nil); // } } - (void)p_uploadImageData:(NSData *)data 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{ NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, [NSUUID UUID].UUIDString]; DebugLog(@"\nuploadImageSize\n%@ : %.0f", fileName, (float)data.length/1024); __weak typeof(self) weakSelf = self; void (^uploadBlock)(NSDictionary *) = ^(NSDictionary *uploadParams){ AFHTTPRequestOperation *operation = [weakSelf POST:path parameters:uploadParams 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]; }; if ([path isEqualToString:@"https://up.qbox.me/"]) {//先拿 token NSDictionary *params = @{ @"fileName": fileName, @"fileSize": @(data.length) }; [[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/upload_token/public/images" withParams:params withMethodType:Get andBlock:^(id data, NSError *error) { if (data) { NSDictionary *result = data[@"data"]; NSMutableDictionary *uploadParams = @{}.mutableCopy; uploadParams[@"token"] = result[@"uptoken"]; uploadParams[@"x:time"] = result[@"time"]; uploadParams[@"x:authToken"] = result[@"authToken"]; uploadParams[@"x:userId"] = result[@"userId"]; uploadParams[@"key"] = fileName; uploadBlock(uploadParams); } }]; }else{ uploadBlock(nil); } } - (void)uploadAssets:(NSArray *)assets path:(NSString *)path name:(NSString *)name params:(NSDictionary *)params successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure progerssBlock:(void (^)(CGFloat progressValue))progress{ AFHTTPRequestOperation *operation = [self POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { for (PHAsset *asset in assets) { NSString *fileName = asset.fileName;; NSData *data = [asset.loadImage dataForCodingUpload]; [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 |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |