@@ -31,13 +31,13 @@ +(void)configureCacheWithMemoryAndDiskCapacity: (unsigned long) memorySize
31
31
diskSize : (unsigned long ) diskSize {
32
32
NSAssert (memorySize > 0 , @" invalid in-memory cache size" );
33
33
NSAssert (diskSize >= 0 , @" invalid disk cache size" );
34
-
34
+
35
35
NSURLCache *cache =
36
36
[[NSURLCache alloc ]
37
37
initWithMemoryCapacity: memorySize
38
38
diskCapacity: diskSize
39
39
diskPath: @" swagger_url_cache" ];
40
-
40
+
41
41
[NSURLCache setSharedURLCache: cache];
42
42
}
43
43
@@ -54,17 +54,17 @@ +(SWGApiClient *)sharedClientFromPool:(NSString *)baseUrl {
54
54
// setup static vars
55
55
// create queue
56
56
sharedQueue = [[NSOperationQueue alloc ] init ];
57
-
57
+
58
58
// create pool
59
59
_pool = [[NSMutableDictionary alloc ] init ];
60
-
60
+
61
61
// initialize URL cache
62
62
[SWGApiClient configureCacheWithMemoryAndDiskCapacity: 4 *1024 *1024 diskSize: 32 *1024 *1024 ];
63
-
63
+
64
64
// configure reachability
65
65
[SWGApiClient configureCacheReachibilityForHost: baseUrl];
66
66
}
67
-
67
+
68
68
@synchronized (self) {
69
69
SWGApiClient * client = [_pool objectForKey: baseUrl];
70
70
if (client == nil ) {
@@ -128,7 +128,7 @@ -(Boolean) executeRequestWithId:(NSNumber*) requestId {
128
128
return TRUE ;
129
129
else return FALSE ;
130
130
}];
131
-
131
+
132
132
if (matchingItems.count == 1 ) {
133
133
if (loggingEnabled)
134
134
NSLog (@" removing request id %@ " , requestId);
@@ -169,19 +169,19 @@ +(void) configureCacheReachibilityForHost:(NSString*)host {
169
169
NSLog (@" reachability changed to AFNetworkReachabilityStatusUnknown" );
170
170
[SWGApiClient setOfflineState: true ];
171
171
break ;
172
-
172
+
173
173
case AFNetworkReachabilityStatusNotReachable:
174
174
if (loggingEnabled)
175
175
NSLog (@" reachability changed to AFNetworkReachabilityStatusNotReachable" );
176
176
[SWGApiClient setOfflineState: true ];
177
177
break ;
178
-
178
+
179
179
case AFNetworkReachabilityStatusReachableViaWWAN:
180
180
if (loggingEnabled)
181
181
NSLog (@" reachability changed to AFNetworkReachabilityStatusReachableViaWWAN" );
182
182
[SWGApiClient setOfflineState: false ];
183
183
break ;
184
-
184
+
185
185
case AFNetworkReachabilityStatusReachableViaWiFi:
186
186
if (loggingEnabled)
187
187
NSLog (@" reachability changed to AFNetworkReachabilityStatusReachableViaWiFi" );
@@ -202,7 +202,7 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path
202
202
queryParams : (NSDictionary *) queryParams {
203
203
NSString * separator = nil ;
204
204
int counter = 0 ;
205
-
205
+
206
206
NSMutableString * requestUrl = [NSMutableString stringWithFormat: @" %@ " , path];
207
207
if (queryParams != nil ){
208
208
for (NSString * key in [queryParams keyEnumerator ]){
@@ -218,7 +218,7 @@ -(NSString*) pathWithQueryParamsToString:(NSString*) path
218
218
SWGQueryParamCollection * coll = (SWGQueryParamCollection*) queryParam;
219
219
NSArray * values = [coll values ];
220
220
NSString * format = [coll format ];
221
-
221
+
222
222
if ([format isEqualToString: @" csv" ]) {
223
223
[requestUrl appendString: [NSString stringWithFormat: @" %@%@ =%@ " , separator,
224
224
[SWGApiClient escape: key], [NSString stringWithFormat: @" %@ " , [values componentsJoinedByString: @" ," ]]]];
@@ -274,7 +274,28 @@ -(NSNumber*) dictionary: (NSString*) path
274
274
requestContentType : (NSString *) requestContentType
275
275
responseContentType : (NSString *) responseContentType
276
276
completionBlock : (void (^)(NSDictionary *, NSError *))completionBlock {
277
-
277
+ // setting request serializer
278
+ if ([requestContentType isEqualToString: @" application/json" ]) {
279
+ self.requestSerializer = [AFJSONRequestSerializer serializer ];
280
+ }
281
+ else if ([requestContentType isEqualToString: @" application/x-www-form-urlencoded" ]) {
282
+ self.requestSerializer = [AFHTTPRequestSerializer serializer ];
283
+ }
284
+ else if ([requestContentType isEqualToString: @" multipart/form-data" ]) {
285
+ self.requestSerializer = [AFHTTPRequestSerializer serializer ];
286
+ }
287
+ else {
288
+ NSAssert (false , @" unsupport request type %@ " , requestContentType);
289
+ }
290
+
291
+ // setting response serializer
292
+ if ([responseContentType isEqualToString: @" application/json" ]) {
293
+ self.responseSerializer = [AFJSONResponseSerializer serializer ];
294
+ }
295
+ else {
296
+ self.responseSerializer = [AFHTTPResponseSerializer serializer ];
297
+ }
298
+
278
299
NSMutableURLRequest * request = nil ;
279
300
if (body != nil && [body isKindOfClass: [NSArray class ]]){
280
301
SWGFile * file;
@@ -291,7 +312,8 @@ -(NSNumber*) dictionary: (NSString*) path
291
312
}
292
313
}
293
314
NSString * urlString = [[NSURL URLWithString: path relativeToURL: self .baseURL] absoluteString ];
294
-
315
+
316
+ // request with multipart form
295
317
if (file != nil ) {
296
318
request = [self .requestSerializer multipartFormRequestWithMethod: @" POST"
297
319
URLString: urlString
@@ -302,20 +324,30 @@ -(NSNumber*) dictionary: (NSString*) path
302
324
NSData * data = [params[key] dataUsingEncoding: NSUTF8StringEncoding];
303
325
[formData appendPartWithFormData: data name: key];
304
326
}
305
-
327
+
306
328
[formData appendPartWithFileData: [file data ]
307
329
name: [file paramName ]
308
330
fileName: [file name ]
309
331
mimeType: [file mimeType ]];
310
-
332
+
311
333
}
312
334
error: nil ];
313
335
}
336
+ // request with form parameters
337
+ else {
338
+ NSString * pathWithQueryParams = [self pathWithQueryParamsToString: path queryParams: queryParams];
339
+ NSString * urlString = [[NSURL URLWithString: pathWithQueryParams relativeToURL: self .baseURL] absoluteString ];
340
+
341
+ request = [self .requestSerializer requestWithMethod: method
342
+ URLString: urlString
343
+ parameters: params
344
+ error: nil ];
345
+ }
314
346
}
315
347
else {
316
348
NSString * pathWithQueryParams = [self pathWithQueryParamsToString: path queryParams: queryParams];
317
349
NSString * urlString = [[NSURL URLWithString: pathWithQueryParams relativeToURL: self .baseURL] absoluteString ];
318
-
350
+
319
351
request = [self .requestSerializer requestWithMethod: method
320
352
URLString: urlString
321
353
parameters: body
@@ -337,11 +369,9 @@ -(NSNumber*) dictionary: (NSString*) path
337
369
[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
338
370
}
339
371
340
- AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer ];
341
-
342
372
if (body != nil ) {
343
373
if ([body isKindOfClass: [NSDictionary class ]] || [body isKindOfClass: [NSArray class ]]){
344
- [requestSerializer setValue: requestContentType forHTTPHeaderField: @" Content-Type" ];
374
+ [self . requestSerializer setValue: requestContentType forHTTPHeaderField: @" Content-Type" ];
345
375
}
346
376
else if ([body isKindOfClass: [SWGFile class ]]) {}
347
377
else {
@@ -353,16 +383,16 @@ -(NSNumber*) dictionary: (NSString*) path
353
383
[request setValue: [headerParams valueForKey: key] forHTTPHeaderField: key];
354
384
}
355
385
}
356
- [requestSerializer setValue: responseContentType forHTTPHeaderField: @" Accept" ];
357
-
386
+ [self . requestSerializer setValue: responseContentType forHTTPHeaderField: @" Accept" ];
387
+
358
388
// Always disable cookies!
359
389
[request setHTTPShouldHandleCookies: NO ];
360
-
361
-
390
+
391
+
362
392
if (self.logRequests ) {
363
393
[self logRequest: request];
364
394
}
365
-
395
+
366
396
NSNumber * requestId = [SWGApiClient queueRequest ];
367
397
AFHTTPRequestOperation *op =
368
398
[self HTTPRequestOperationWithRequest: request
@@ -380,14 +410,14 @@ -(NSNumber*) dictionary: (NSString*) path
380
410
userInfo[SWGResponseObjectErrorKey] = operation.responseObject ;
381
411
}
382
412
NSError *augmentedError = [error initWithDomain: error.domain code: error.code userInfo: userInfo];
383
-
413
+
384
414
if (self.logServerResponses )
385
415
[self logResponse: nil forRequest: request error: augmentedError];
386
416
completionBlock (nil , augmentedError);
387
417
}
388
418
}
389
419
];
390
-
420
+
391
421
[self .operationQueue addOperation: op];
392
422
return requestId;
393
423
}
@@ -400,6 +430,28 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
400
430
requestContentType : (NSString *) requestContentType
401
431
responseContentType : (NSString *) responseContentType
402
432
completionBlock : (void (^)(NSString *, NSError *))completionBlock {
433
+ // setting request serializer
434
+ if ([requestContentType isEqualToString: @" application/json" ]) {
435
+ self.requestSerializer = [AFJSONRequestSerializer serializer ];
436
+ }
437
+ else if ([requestContentType isEqualToString: @" application/x-www-form-urlencoded" ]) {
438
+ self.requestSerializer = [AFHTTPRequestSerializer serializer ];
439
+ }
440
+ else if ([requestContentType isEqualToString: @" multipart/form-data" ]) {
441
+ self.requestSerializer = [AFHTTPRequestSerializer serializer ];
442
+ }
443
+ else {
444
+ NSAssert (false , @" unsupport request type %@ " , requestContentType);
445
+ }
446
+
447
+ // setting response serializer
448
+ if ([responseContentType isEqualToString: @" application/json" ]) {
449
+ self.responseSerializer = [AFJSONResponseSerializer serializer ];
450
+ }
451
+ else {
452
+ self.responseSerializer = [AFHTTPResponseSerializer serializer ];
453
+ }
454
+
403
455
NSMutableURLRequest * request = nil ;
404
456
if (body != nil && [body isKindOfClass: [NSArray class ]]){
405
457
SWGFile * file;
@@ -416,31 +468,43 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
416
468
}
417
469
}
418
470
NSString * urlString = [[NSURL URLWithString: path relativeToURL: self .baseURL] absoluteString ];
419
-
471
+
472
+ // request with multipart form
420
473
if (file != nil ) {
421
474
request = [self .requestSerializer multipartFormRequestWithMethod: @" POST"
422
475
URLString: urlString
423
476
parameters: nil
424
477
constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
425
-
478
+
426
479
for (NSString * key in params) {
427
480
NSData * data = [params[key] dataUsingEncoding: NSUTF8StringEncoding];
428
481
[formData appendPartWithFormData: data name: key];
429
482
}
430
-
483
+
431
484
[formData appendPartWithFileData: [file data ]
432
485
name: [file paramName ]
433
486
fileName: [file name ]
434
487
mimeType: [file mimeType ]];
435
-
488
+
436
489
}
437
490
error: nil ];
438
491
}
492
+ // request with form parameters
493
+ else {
494
+ NSString * pathWithQueryParams = [self pathWithQueryParamsToString: path queryParams: queryParams];
495
+ NSString * urlString = [[NSURL URLWithString: pathWithQueryParams relativeToURL: self .baseURL] absoluteString ];
496
+
497
+ request = [self .requestSerializer requestWithMethod: method
498
+ URLString: urlString
499
+ parameters: params
500
+ error: nil ];
501
+ }
502
+
439
503
}
440
504
else {
441
505
NSString * pathWithQueryParams = [self pathWithQueryParamsToString: path queryParams: queryParams];
442
506
NSString * urlString = [[NSURL URLWithString: pathWithQueryParams relativeToURL: self .baseURL] absoluteString ];
443
-
507
+
444
508
request = [self .requestSerializer requestWithMethod: method
445
509
URLString: urlString
446
510
parameters: body
@@ -461,13 +525,11 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
461
525
NSLog (@" %@ cache disabled" , path);
462
526
[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
463
527
}
464
-
465
-
466
- AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer ];
528
+
467
529
468
530
if (body != nil ) {
469
531
if ([body isKindOfClass: [NSDictionary class ]] || [body isKindOfClass: [NSArray class ]]){
470
- [requestSerializer setValue: requestContentType forHTTPHeaderField: @" Content-Type" ];
532
+ [self . requestSerializer setValue: requestContentType forHTTPHeaderField: @" Content-Type" ];
471
533
}
472
534
else if ([body isKindOfClass: [SWGFile class ]]){}
473
535
else {
@@ -479,12 +541,12 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
479
541
[request setValue: [headerParams valueForKey: key] forHTTPHeaderField: key];
480
542
}
481
543
}
482
- [requestSerializer setValue: responseContentType forHTTPHeaderField: @" Accept" ];
544
+ [self .requestSerializer setValue: responseContentType forHTTPHeaderField: @" Accept" ];
545
+
483
546
484
-
485
547
// Always disable cookies!
486
548
[request setHTTPShouldHandleCookies: NO ];
487
-
549
+
488
550
NSNumber * requestId = [SWGApiClient queueRequest ];
489
551
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest: request
490
552
success: ^(AFHTTPRequestOperation *operation, id responseObject) {
@@ -502,15 +564,15 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
502
564
userInfo[SWGResponseObjectErrorKey] = operation.responseObject ;
503
565
}
504
566
NSError *augmentedError = [error initWithDomain: error.domain code: error.code userInfo: userInfo];
505
-
567
+
506
568
if (self.logServerResponses )
507
569
[self logResponse: nil forRequest: request error: augmentedError];
508
570
completionBlock (nil , augmentedError);
509
571
}
510
572
}];
511
-
573
+
512
574
[self .operationQueue addOperation: op];
513
575
return requestId;
514
576
}
515
577
516
- @end
578
+ @end
0 commit comments