Skip to content

[Obj-C] Added ability to access HTTP Response headers #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ static bool cacheEnabled = false;
static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable;
static void (^reachabilityChangeBlock)(int);


@interface {{classPrefix}}ApiClient ()

@property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders;

@end

@implementation {{classPrefix}}ApiClient

- (instancetype)init {
Expand Down Expand Up @@ -385,6 +392,8 @@ static void (^reachabilityChangeBlock)(int);
if([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Expand All @@ -398,6 +407,10 @@ static void (^reachabilityChangeBlock)(int);

if([[{{classPrefix}}Configuration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError];

NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;

completionBlock(nil, augmentedError);
}
}];
Expand Down Expand Up @@ -441,6 +454,7 @@ static void (^reachabilityChangeBlock)(int);
NSURL *file = [NSURL fileURLWithPath:filepath];

[operation.responseData writeToURL:file atomically:YES];
self.HTTPResponseHeaders = headers;
completionBlock(file, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

Expand All @@ -455,7 +469,8 @@ static void (^reachabilityChangeBlock)(int);
if ([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError];
}

NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError);
}
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
@property(nonatomic, assign) NSTimeInterval timeoutInterval;
@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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@implementation {{classname}}

static {{classname}}* singletonAPI = nil;

#pragma mark - Initialize methods

- (id) init {
Expand Down Expand Up @@ -38,7 +40,6 @@
#pragma mark -

+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static {{classname}}* singletonAPI = nil;

if (singletonAPI == nil) {
singletonAPI = [[{{classname}} alloc] init];
Expand All @@ -47,6 +48,14 @@
return singletonAPI;
}

+({{classname}}*) sharedAPI {

if (singletonAPI == nil) {
singletonAPI = [[{{classname}} alloc] init];
}
return singletonAPI;
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize;
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+({{classname}}*) sharedAPI;
{{#operation}}
///
///
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/objc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

## Recommendation

It's recommended to create an instance of ApiClient per thread in a multithreaded environment to avoid any potential issue.

## Author

[email protected]
Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/objc/SwaggerClient/SWGApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ extern NSString *const SWGResponseObjectErrorKey;
@property(nonatomic, assign) NSTimeInterval timeoutInterval;
@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
*/
Expand Down
17 changes: 16 additions & 1 deletion samples/client/petstore/objc/SwaggerClient/SWGApiClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilityStatusNotReachable;
static void (^reachabilityChangeBlock)(int);


@interface SWGApiClient ()

@property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders;

@end

@implementation SWGApiClient

- (instancetype)init {
Expand Down Expand Up @@ -385,6 +392,8 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request
if([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
Expand All @@ -398,6 +407,10 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request

if([[SWGConfiguration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError];

NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;

completionBlock(nil, augmentedError);
}
}];
Expand Down Expand Up @@ -441,6 +454,7 @@ - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request
NSURL *file = [NSURL fileURLWithPath:filepath];

[operation.responseData writeToURL:file atomically:YES];
self.HTTPResponseHeaders = headers;
completionBlock(file, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

Expand All @@ -455,7 +469,8 @@ - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request
if ([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError];
}

NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError);
}
}];
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/objc/SwaggerClient/SWGPetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize;
+(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGPetApi*) sharedAPI;
///
///
/// Update an existing pet
Expand Down
11 changes: 10 additions & 1 deletion samples/client/petstore/objc/SwaggerClient/SWGPetApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ @interface SWGPetApi ()

@implementation SWGPetApi

static SWGPetApi* singletonAPI = nil;

#pragma mark - Initialize methods

- (id) init {
Expand Down Expand Up @@ -36,7 +38,6 @@ - (id) initWithApiClient:(SWGApiClient *)apiClient {
#pragma mark -

+(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGPetApi* singletonAPI = nil;

if (singletonAPI == nil) {
singletonAPI = [[SWGPetApi alloc] init];
Expand All @@ -45,6 +46,14 @@ +(SWGPetApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
return singletonAPI;
}

+(SWGPetApi*) sharedAPI {

if (singletonAPI == nil) {
singletonAPI = [[SWGPetApi alloc] init];
}
return singletonAPI;
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key];
}
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/objc/SwaggerClient/SWGStoreApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize;
+(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGStoreApi*) sharedAPI;
///
///
/// Returns pet inventories by status
Expand Down
11 changes: 10 additions & 1 deletion samples/client/petstore/objc/SwaggerClient/SWGStoreApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ @interface SWGStoreApi ()

@implementation SWGStoreApi

static SWGStoreApi* singletonAPI = nil;

#pragma mark - Initialize methods

- (id) init {
Expand Down Expand Up @@ -36,7 +38,6 @@ - (id) initWithApiClient:(SWGApiClient *)apiClient {
#pragma mark -

+(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGStoreApi* singletonAPI = nil;

if (singletonAPI == nil) {
singletonAPI = [[SWGStoreApi alloc] init];
Expand All @@ -45,6 +46,14 @@ +(SWGStoreApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
return singletonAPI;
}

+(SWGStoreApi*) sharedAPI {

if (singletonAPI == nil) {
singletonAPI = [[SWGStoreApi alloc] init];
}
return singletonAPI;
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key];
}
Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore/objc/SwaggerClient/SWGUserApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
-(unsigned long) requestQueueSize;
+(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
+(SWGUserApi*) sharedAPI;
///
///
/// Create user
Expand Down Expand Up @@ -98,7 +99,7 @@
/// Get user by user name
///
///
/// @param username The name that needs to be fetched. Use user1 for testing.
/// @param username The name that needs to be fetched. Use user1 for testing.
///
///
/// @return SWGUser*
Expand Down
13 changes: 11 additions & 2 deletions samples/client/petstore/objc/SwaggerClient/SWGUserApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ @interface SWGUserApi ()

@implementation SWGUserApi

static SWGUserApi* singletonAPI = nil;

#pragma mark - Initialize methods

- (id) init {
Expand Down Expand Up @@ -36,7 +38,6 @@ - (id) initWithApiClient:(SWGApiClient *)apiClient {
#pragma mark -

+(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
static SWGUserApi* singletonAPI = nil;

if (singletonAPI == nil) {
singletonAPI = [[SWGUserApi alloc] init];
Expand All @@ -45,6 +46,14 @@ +(SWGUserApi*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
return singletonAPI;
}

+(SWGUserApi*) sharedAPI {

if (singletonAPI == nil) {
singletonAPI = [[SWGUserApi alloc] init];
}
return singletonAPI;
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
[self.defaultHeaders setValue:value forKey:key];
}
Expand Down Expand Up @@ -461,7 +470,7 @@ -(NSNumber*) logoutUserWithCompletionBlock:
///
/// Get user by user name
///
/// @param username The name that needs to be fetched. Use user1 for testing.
/// @param username The name that needs to be fetched. Use user1 for testing.
///
/// @returns SWGUser*
///
Expand Down