forked from loafle/openapi-generator-original
added support for retrieving HTTP response headers in obj-c. Also added a +sharedAPI class method for easy initialization of an API class without passing headers
This commit is contained in:
parent
9e8e5db7b3
commit
2822e9ea38
@ -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 {
|
||||
@ -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) {
|
||||
@ -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);
|
||||
}
|
||||
}];
|
||||
@ -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) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}];
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
@implementation {{classname}}
|
||||
|
||||
static {{classname}}* singletonAPI = nil;
|
||||
|
||||
#pragma mark - Initialize methods
|
||||
|
||||
- (id) init {
|
||||
@ -38,7 +40,6 @@
|
||||
#pragma mark -
|
||||
|
||||
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key {
|
||||
static {{classname}}* singletonAPI = nil;
|
||||
|
||||
if (singletonAPI == nil) {
|
||||
singletonAPI = [[{{classname}} alloc] init];
|
||||
@ -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];
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
-(void) addHeader:(NSString*)value forKey:(NSString*)key;
|
||||
-(unsigned long) requestQueueSize;
|
||||
+({{classname}}*) apiWithHeader:(NSString*)headerValue key:(NSString*)key;
|
||||
+({{classname}}*) sharedAPI;
|
||||
{{#operation}}
|
||||
///
|
||||
///
|
||||
|
Loading…
x
Reference in New Issue
Block a user