Merge pull request #1189 from mad102190/feature/objc-response-headers

[Obj-C] Added ability to access HTTP Response headers
This commit is contained in:
wing328 2015-09-04 16:42:25 +08:00
commit 2e30a3af27
13 changed files with 88 additions and 8 deletions

View File

@ -9,6 +9,13 @@ static bool cacheEnabled = false;
static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable; static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable;
static void (^reachabilityChangeBlock)(int); static void (^reachabilityChangeBlock)(int);
@interface {{classPrefix}}ApiClient ()
@property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders;
@end
@implementation {{classPrefix}}ApiClient @implementation {{classPrefix}}ApiClient
- (instancetype)init { - (instancetype)init {
@ -385,6 +392,8 @@ static void (^reachabilityChangeBlock)(int);
if([[{{classPrefix}}Configuration sharedConfig] debug]) { if([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil]; [self logResponse:operation forRequest:request error:nil];
} }
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil); completionBlock(response, nil);
} }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
@ -398,6 +407,10 @@ static void (^reachabilityChangeBlock)(int);
if([[{{classPrefix}}Configuration sharedConfig] debug]) if([[{{classPrefix}}Configuration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
} }
}]; }];
@ -441,6 +454,7 @@ static void (^reachabilityChangeBlock)(int);
NSURL *file = [NSURL fileURLWithPath:filepath]; NSURL *file = [NSURL fileURLWithPath:filepath];
[operation.responseData writeToURL:file atomically:YES]; [operation.responseData writeToURL:file atomically:YES];
self.HTTPResponseHeaders = headers;
completionBlock(file, nil); completionBlock(file, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
@ -455,7 +469,8 @@ static void (^reachabilityChangeBlock)(int);
if ([[{{classPrefix}}Configuration sharedConfig] debug]) { if ([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
} }
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
} }
}]; }];

View File

@ -31,6 +31,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
@property(nonatomic, assign) NSTimeInterval timeoutInterval; @property(nonatomic, assign) NSTimeInterval timeoutInterval;
@property(nonatomic, readonly) NSOperationQueue* queue; @property(nonatomic, readonly) NSOperationQueue* queue;
/// In order to ensure the HTTPResponseHeaders are correct, it is recommended to initialize one {{classPrefix}}ApiClient instance per thread.
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
/** /**
* Clears Cache * Clears Cache
*/ */

View File

@ -11,6 +11,8 @@
@implementation {{classname}} @implementation {{classname}}
static {{classname}}* singletonAPI = nil;
#pragma mark - Initialize methods #pragma mark - Initialize methods
- (id) init { - (id) init {
@ -38,7 +40,6 @@
#pragma mark - #pragma mark -
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key { +({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static {{classname}}* singletonAPI = nil;
if (singletonAPI == nil) { if (singletonAPI == nil) {
singletonAPI = [[{{classname}} alloc] init]; singletonAPI = [[{{classname}} alloc] init];
@ -47,6 +48,14 @@
return singletonAPI; return singletonAPI;
} }
+({{classname}}*) sharedAPI {
if (singletonAPI == nil) {
singletonAPI = [[{{classname}} alloc] init];
}
return singletonAPI;
}
-(void) addHeader:(NSString*)value forKey:(NSString*)key { -(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key]; [self.defaultHeaders setValue:value forKey:key];
} }

View File

@ -20,6 +20,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key; -(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize; -(unsigned long) requestQueueSize;
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key; +({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+({{classname}}*) sharedAPI;
{{#operation}} {{#operation}}
/// ///
/// ///

View File

@ -12,6 +12,10 @@ To install it, put the API client library in your project and then simply add th
pod "SwaggerClient", :path => "/path/to/lib" pod "SwaggerClient", :path => "/path/to/lib"
``` ```
## Recommendation
It's recommended to create an instance of ApiClient per thread in a multithreaded environment to avoid any potential issue.
## Author ## Author
apiteam@swagger.io apiteam@swagger.io

View File

@ -35,6 +35,9 @@ extern NSString *const SWGResponseObjectErrorKey;
@property(nonatomic, assign) NSTimeInterval timeoutInterval; @property(nonatomic, assign) NSTimeInterval timeoutInterval;
@property(nonatomic, readonly) NSOperationQueue* queue; @property(nonatomic, readonly) NSOperationQueue* queue;
/// In order to ensure the HTTPResponseHeaders are correct, it is recommended to initialize one SWGApiClient instance per thread.
@property(nonatomic, readonly) NSDictionary* HTTPResponseHeaders;
/** /**
* Clears Cache * Clears Cache
*/ */

View File

@ -9,6 +9,13 @@ static bool cacheEnabled = false;
static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable; static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable;
static void (^reachabilityChangeBlock)(int); static void (^reachabilityChangeBlock)(int);
@interface SWGApiClient ()
@property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders;
@end
@implementation SWGApiClient @implementation SWGApiClient
- (instancetype)init { - (instancetype)init {
@ -385,6 +392,8 @@ static void (^reachabilityChangeBlock)(int);
if([[SWGConfiguration sharedConfig] debug]) { if([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil]; [self logResponse:operation forRequest:request error:nil];
} }
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil); completionBlock(response, nil);
} }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
@ -398,6 +407,10 @@ static void (^reachabilityChangeBlock)(int);
if([[SWGConfiguration sharedConfig] debug]) if([[SWGConfiguration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
} }
}]; }];
@ -441,6 +454,7 @@ static void (^reachabilityChangeBlock)(int);
NSURL *file = [NSURL fileURLWithPath:filepath]; NSURL *file = [NSURL fileURLWithPath:filepath];
[operation.responseData writeToURL:file atomically:YES]; [operation.responseData writeToURL:file atomically:YES];
self.HTTPResponseHeaders = headers;
completionBlock(file, nil); completionBlock(file, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
@ -455,7 +469,8 @@ static void (^reachabilityChangeBlock)(int);
if ([[SWGConfiguration sharedConfig] debug]) { if ([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
} }
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
} }
}]; }];

View File

@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key; -(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize; -(unsigned long) requestQueueSize;
+(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key; +(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGPetApi*) sharedAPI;
/// ///
/// ///
/// Update an existing pet /// Update an existing pet

View File

@ -9,6 +9,8 @@
@implementation SWGPetApi @implementation SWGPetApi
static SWGPetApi* singletonAPI = nil;
#pragma mark - Initialize methods #pragma mark - Initialize methods
- (id) init { - (id) init {
@ -36,7 +38,6 @@
#pragma mark - #pragma mark -
+(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key { +(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGPetApi* singletonAPI = nil;
if (singletonAPI == nil) { if (singletonAPI == nil) {
singletonAPI = [[SWGPetApi alloc] init]; singletonAPI = [[SWGPetApi alloc] init];
@ -45,6 +46,14 @@
return singletonAPI; return singletonAPI;
} }
+(SWGPetApi*) sharedAPI {
if (singletonAPI == nil) {
singletonAPI = [[SWGPetApi alloc] init];
}
return singletonAPI;
}
-(void) addHeader:(NSString*)value forKey:(NSString*)key { -(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key]; [self.defaultHeaders setValue:value forKey:key];
} }

View File

@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key; -(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize; -(unsigned long) requestQueueSize;
+(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key; +(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGStoreApi*) sharedAPI;
/// ///
/// ///
/// Returns pet inventories by status /// Returns pet inventories by status

View File

@ -9,6 +9,8 @@
@implementation SWGStoreApi @implementation SWGStoreApi
static SWGStoreApi* singletonAPI = nil;
#pragma mark - Initialize methods #pragma mark - Initialize methods
- (id) init { - (id) init {
@ -36,7 +38,6 @@
#pragma mark - #pragma mark -
+(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key { +(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGStoreApi* singletonAPI = nil;
if (singletonAPI == nil) { if (singletonAPI == nil) {
singletonAPI = [[SWGStoreApi alloc] init]; singletonAPI = [[SWGStoreApi alloc] init];
@ -45,6 +46,14 @@
return singletonAPI; return singletonAPI;
} }
+(SWGStoreApi*) sharedAPI {
if (singletonAPI == nil) {
singletonAPI = [[SWGStoreApi alloc] init];
}
return singletonAPI;
}
-(void) addHeader:(NSString*)value forKey:(NSString*)key { -(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key]; [self.defaultHeaders setValue:value forKey:key];
} }

View File

@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key; -(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize; -(unsigned long) requestQueueSize;
+(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key; +(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGUserApi*) sharedAPI;
/// ///
/// ///
/// Create user /// Create user

View File

@ -9,6 +9,8 @@
@implementation SWGUserApi @implementation SWGUserApi
static SWGUserApi* singletonAPI = nil;
#pragma mark - Initialize methods #pragma mark - Initialize methods
- (id) init { - (id) init {
@ -36,7 +38,6 @@
#pragma mark - #pragma mark -
+(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key { +(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGUserApi* singletonAPI = nil;
if (singletonAPI == nil) { if (singletonAPI == nil) {
singletonAPI = [[SWGUserApi alloc] init]; singletonAPI = [[SWGUserApi alloc] init];
@ -45,6 +46,14 @@
return singletonAPI; return singletonAPI;
} }
+(SWGUserApi*) sharedAPI {
if (singletonAPI == nil) {
singletonAPI = [[SWGUserApi alloc] init];
}
return singletonAPI;
}
-(void) addHeader:(NSString*)value forKey:(NSString*)key { -(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key]; [self.defaultHeaders setValue:value forKey:key];
} }