Skip to content

Commit ddb3bd5

Browse files
committed
Merge pull request #816 from geekerzp/develop_2.0_objc_auth
[Objc] Add authentication support (API key, HTTP basic)
2 parents 2ec18a1 + 699df00 commit ddb3bd5

28 files changed

+996
-19
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/ObjcClientCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public void processOpts() {
140140
supportingFiles.add(new SupportingFile("SWGFile.m", sourceFolder, "SWGFile.m"));
141141
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.m", sourceFolder, "JSONValueTransformer+ISO8601.m"));
142142
supportingFiles.add(new SupportingFile("JSONValueTransformer+ISO8601.h", sourceFolder, "JSONValueTransformer+ISO8601.h"));
143+
supportingFiles.add(new SupportingFile("SWGConfiguration-body.mustache", sourceFolder, "SWGConfiguration.m"));
144+
supportingFiles.add(new SupportingFile("SWGConfiguration-header.mustache", sourceFolder, "SWGConfiguration.h"));
143145
supportingFiles.add(new SupportingFile("Podfile.mustache", "", "Podfile"));
144146
}
145147

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.h

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/**
55
* A key for `NSError` user info dictionaries.
6-
*
6+
*
77
* The corresponding value is the parsed response body for an HTTP error.
88
*/
99
extern NSString *const SWGResponseObjectErrorKey;
@@ -20,56 +20,197 @@ extern NSString *const SWGResponseObjectErrorKey;
2020
@property(nonatomic, assign) BOOL logHTTP;
2121
@property(nonatomic, readonly) NSOperationQueue* queue;
2222

23+
/**
24+
* Get the Api Client instance from pool
25+
*
26+
* @param baseUrl The base url of api client.
27+
*
28+
* @return The SWGApiClient instance.
29+
*/
2330
+(SWGApiClient *)sharedClientFromPool:(NSString *)baseUrl;
2431

32+
/**
33+
* Get the operations queue
34+
*
35+
* @return The `shardQueue` static variable.
36+
*/
2537
+(NSOperationQueue*) sharedQueue;
2638

39+
/**
40+
* Turn on logging
41+
*
42+
* @param state logging state, must be `YES` or `NO`
43+
*/
2744
+(void)setLoggingEnabled:(bool) state;
2845

46+
/**
47+
* Clear Cache
48+
*/
2949
+(void)clearCache;
3050

51+
/**
52+
* Turn on cache
53+
*
54+
* @param enabled If the cached is enable, must be `YES` or `NO`
55+
*/
3156
+(void)setCacheEnabled:(BOOL) enabled;
3257

58+
/**
59+
* Get the request queue size
60+
*
61+
* @return The size of `queuedRequests` static variable.
62+
*/
3363
+(unsigned long)requestQueueSize;
3464

65+
/**
66+
* Set the client unreachable
67+
*
68+
* @param state off line state, must be `YES` or `NO`
69+
*/
3570
+(void) setOfflineState:(BOOL) state;
3671

72+
/**
73+
* Get the client reachability
74+
*
75+
* @return The client reachability.
76+
*/
3777
+(AFNetworkReachabilityStatus) getReachabilityStatus;
3878

79+
/**
80+
* Get the next request id
81+
*
82+
* @return The next executed request id.
83+
*/
3984
+(NSNumber*) nextRequestId;
4085

86+
/**
87+
* Generate request id and add it to the queue
88+
*
89+
* @return The next executed request id.
90+
*/
4191
+(NSNumber*) queueRequest;
4292

93+
/**
94+
* Remove request id from the queue
95+
*
96+
* @param requestId The request which will be removed.
97+
*/
4398
+(void) cancelRequest:(NSNumber*)requestId;
4499

100+
/**
101+
* URL encode NSString
102+
*
103+
* @param unescaped The string which will be escaped.
104+
*
105+
* @return The escaped string.
106+
*/
45107
+(NSString*) escape:(id)unescaped;
46108

109+
/**
110+
* Customize the behavior when the reachability changed
111+
*
112+
* @param changeBlock The block will be executed when the reachability changed.
113+
*/
47114
+(void) setReachabilityChangeBlock:(void(^)(int))changeBlock;
48115

116+
/**
117+
* Set the client reachability strategy
118+
*
119+
* @param host The host of SWGApiClient.
120+
*/
49121
+(void) configureCacheReachibilityForHost:(NSString*)host;
50122

123+
/**
124+
* Detect Accept header from accepts NSArray
125+
*
126+
* @param accepts NSArray of header
127+
*
128+
* @return The Accept header
129+
*/
51130
+(NSString *) selectHeaderAccept:(NSArray *)accepts;
131+
132+
/**
133+
* Detect Content-Type header from contentTypes NSArray
134+
*
135+
* @param contentTypes NSArray of header
136+
*
137+
* @return The Content-Type header
138+
*/
52139
+(NSString *) selectHeaderContentType:(NSArray *)contentTypes;
53140

141+
/**
142+
* Set header for request
143+
*
144+
* @param value The header value
145+
* @param forKey The header key
146+
*/
54147
-(void)setHeaderValue:(NSString*) value
55-
forKey:(NSString*) forKey;
148+
forKey:(NSString*) forKey;
149+
150+
/**
151+
* Update header parameters and query parameters for authentication
152+
*
153+
* @param headers The header parameter will be udpated, passed by pointer to pointer.
154+
* @param querys The query parameters will be updated, passed by pointer to pointer.
155+
* @param authSettings The authentication names NSArray.
156+
*/
157+
- (void) updateHeaderParams:(NSDictionary **)headers
158+
queryParams:(NSDictionary **)querys
159+
WithAuthSettings:(NSArray *)authSettings;
56160

161+
/**
162+
* Perform request
163+
*
164+
* Request with non-empty response
165+
*
166+
* @param path Request url.
167+
* @param method Request method.
168+
* @param queryParams Request query parameters.
169+
* @param body Request body.
170+
* @param headerParams Request header parameters.
171+
* @param authSettings Request authentication names.
172+
* @param requestContentType Request content-type.
173+
* @param responseContentType Response content-type.
174+
* @param completionBlock The block will be executed when the request completed.
175+
*
176+
* @return The request id.
177+
*/
57178
-(NSNumber*) dictionary:(NSString*) path
58179
method:(NSString*) method
59180
queryParams:(NSDictionary*) queryParams
60181
body:(id) body
61182
headerParams:(NSDictionary*) headerParams
183+
authSettings: (NSArray *) authSettings
62184
requestContentType:(NSString*) requestContentType
63185
responseContentType:(NSString*) responseContentType
64186
completionBlock:(void (^)(NSDictionary*, NSError *))completionBlock;
65187

188+
/**
189+
* Perform request
190+
*
191+
* Request with empty response
192+
*
193+
* @param path Request url.
194+
* @param method Request method.
195+
* @param queryParams Request query parameters.
196+
* @param body Request body.
197+
* @param headerParams Request header parameters.
198+
* @param authSettings Request authentication names.
199+
* @param requestContentType Request content-type.
200+
* @param responseContentType Response content-type.
201+
* @param completionBlock The block will be executed when the request completed.
202+
*
203+
* @return The request id.
204+
*/
66205
-(NSNumber*) stringWithCompletionBlock:(NSString*) path
67206
method:(NSString*) method
68207
queryParams:(NSDictionary*) queryParams
69208
body:(id) body
70209
headerParams:(NSDictionary*) headerParams
210+
authSettings: (NSArray *) authSettings
71211
requestContentType:(NSString*) requestContentType
72212
responseContentType:(NSString*) responseContentType
73213
completionBlock:(void (^)(NSString*, NSError *))completionBlock;
74214
@end
75215

216+

modules/swagger-codegen/src/main/resources/objc/SWGApiClient.m

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "SWGApiClient.h"
22
#import "SWGFile.h"
33
#import "SWGQueryParamCollection.h"
4+
#import "SWGConfiguration.h"
45

56
@implementation SWGApiClient
67

@@ -15,10 +16,22 @@ @implementation SWGApiClient
1516
static void (^reachabilityChangeBlock)(int);
1617
static bool loggingEnabled = true;
1718

19+
#pragma mark - Log Methods
20+
1821
+(void)setLoggingEnabled:(bool) state {
1922
loggingEnabled = state;
2023
}
2124

25+
- (void)logRequest:(NSURLRequest*)request {
26+
NSLog(@"request: %@", [self descriptionForRequest:request]);
27+
}
28+
29+
- (void)logResponse:(id)data forRequest:(NSURLRequest*)request error:(NSError*)error {
30+
NSLog(@"request: %@ response: %@ ", [self descriptionForRequest:request], data );
31+
}
32+
33+
#pragma mark -
34+
2235
+(void)clearCache {
2336
[[NSURLCache sharedURLCache] removeAllCachedResponses];
2437
}
@@ -305,19 +318,47 @@ - (NSString*)descriptionForRequest:(NSURLRequest*)request {
305318
return [[request URL] absoluteString];
306319
}
307320

308-
- (void)logRequest:(NSURLRequest*)request {
309-
NSLog(@"request: %@", [self descriptionForRequest:request]);
310-
}
311321

312-
- (void)logResponse:(id)data forRequest:(NSURLRequest*)request error:(NSError*)error {
313-
NSLog(@"request: %@ response: %@ ", [self descriptionForRequest:request], data );
322+
/**
323+
* Update header and query params based on authentication settings
324+
*/
325+
- (void) updateHeaderParams:(NSDictionary *__autoreleasing *)headers
326+
queryParams:(NSDictionary *__autoreleasing *)querys
327+
WithAuthSettings:(NSArray *)authSettings {
328+
329+
if (!authSettings || [authSettings count] == 0) {
330+
return;
331+
}
332+
333+
NSMutableDictionary *headersWithAuth = [NSMutableDictionary dictionaryWithDictionary:*headers];
334+
NSMutableDictionary *querysWithAuth = [NSMutableDictionary dictionaryWithDictionary:*querys];
335+
336+
SWGConfiguration *config = [SWGConfiguration sharedConfig];
337+
for (NSString *auth in authSettings) {
338+
NSDictionary *authSetting = [[config authSettings] objectForKey:auth];
339+
340+
if (authSetting) {
341+
if ([authSetting[@"in"] isEqualToString:@"header"]) {
342+
[headersWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
343+
}
344+
else if ([authSetting[@"in"] isEqualToString:@"query"]) {
345+
[querysWithAuth setObject:authSetting[@"value"] forKey:authSetting[@"key"]];
346+
}
347+
}
348+
}
349+
350+
*headers = [NSDictionary dictionaryWithDictionary:headersWithAuth];
351+
*querys = [NSDictionary dictionaryWithDictionary:querysWithAuth];
314352
}
315353

354+
#pragma mark - Perform Request Methods
355+
316356
-(NSNumber*) dictionary: (NSString*) path
317357
method: (NSString*) method
318358
queryParams: (NSDictionary*) queryParams
319359
body: (id) body
320360
headerParams: (NSDictionary*) headerParams
361+
authSettings: (NSArray *) authSettings
321362
requestContentType: (NSString*) requestContentType
322363
responseContentType: (NSString*) responseContentType
323364
completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock {
@@ -342,6 +383,9 @@ -(NSNumber*) dictionary: (NSString*) path
342383
else {
343384
self.responseSerializer = [AFHTTPResponseSerializer serializer];
344385
}
386+
387+
// auth setting
388+
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
345389

346390
NSMutableURLRequest * request = nil;
347391
if (body != nil && [body isKindOfClass:[NSArray class]]){
@@ -476,6 +520,7 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
476520
queryParams: (NSDictionary*) queryParams
477521
body: (id) body
478522
headerParams: (NSDictionary*) headerParams
523+
authSettings: (NSArray *) authSettings
479524
requestContentType: (NSString*) requestContentType
480525
responseContentType: (NSString*) responseContentType
481526
completionBlock: (void (^)(NSString*, NSError *))completionBlock {
@@ -500,6 +545,9 @@ -(NSNumber*) stringWithCompletionBlock: (NSString*) path
500545
else {
501546
self.responseSerializer = [AFHTTPResponseSerializer serializer];
502547
}
548+
549+
// auth setting
550+
[self updateHeaderParams:&headerParams queryParams:&queryParams WithAuthSettings:authSettings];
503551

504552
NSMutableURLRequest * request = nil;
505553
if (body != nil && [body isKindOfClass:[NSArray class]]){

0 commit comments

Comments
 (0)