added objc sample

This commit is contained in:
Tony Tam 2012-09-24 22:55:17 -07:00
parent 89ec4010e2
commit 85a0a728de
18 changed files with 1049 additions and 0 deletions

5
bin/objc-petstore.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export CLASSPATH="$DIR/../target/*:$DIR/../target/lib/*"
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
scala $WORDNIK_OPTS $JAVA_CONFIG_OPTIONS -cp $CLASSPATH "$@" samples/client/petstore/objc/ObjcPetstoreCodegen.scala http://petstore.swagger.wordnik.com/api/resources.json special-key

View File

@ -0,0 +1,33 @@
#import <Foundation/Foundation.h>
@interface NIKApiInvoker : NSObject {
@private
NSOperationQueue *_queue;
NSMutableDictionary * _defaultHeaders;
}
@property(nonatomic, readonly) NSOperationQueue* queue;
@property(nonatomic, readonly) NSMutableDictionary * defaultHeaders;
-(void) addHeader:(NSString*) value
forKey:(NSString*)key;
-(NSString*) escapeString:(NSString*) string;
-(id) dictionaryWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id)body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSDictionary*, NSError *))completionBlock;
-(id) stringWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id)body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSString*, NSError *))completionBlock;
@end

View File

@ -0,0 +1,223 @@
#import "NIKApiInvoker.h"
@implementation NIKApiInvoker
@synthesize queue = _queue;
@synthesize defaultHeaders = _defaultHeaders;
- (id) init {
self = [super init];
_queue = [[NSOperationQueue alloc] init];
_defaultHeaders = [[NSMutableDictionary alloc] init];
return self;
}
-(void) addHeader:(NSString*) value
forKey:(NSString*)key {
[_defaultHeaders setValue:value forKey:key];
}
-(NSString*) escapeString:(NSString *)unescaped {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef) unescaped,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}
-(id) dictionaryWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id) body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSDictionary*, NSError *))completionBlock
{
NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path];
NSString * separator = nil;
int counter = 0;
if(queryParams != nil){
for(NSString * key in [queryParams keyEnumerator]){
if(counter == 0) separator = @"?";
else separator = @"&";
NSString * value;
if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){
value = [self escapeString:[queryParams valueForKey:key]];
}
else {
value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]];
}
[requestUrl appendFormat:[NSString stringWithFormat:@"%@%@=%@", separator,
[self escapeString:key], value]];
counter += 1;
}
}
NSLog(@"request url: %@", requestUrl);
NSURL* URL = [NSURL URLWithString:requestUrl];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
for(NSString * key in [_defaultHeaders keyEnumerator]){
[request setValue:[_defaultHeaders valueForKey:key] forHTTPHeaderField:key];
}
if(headerParams != nil){
for(NSString * key in [headerParams keyEnumerator]){
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[request setHTTPMethod:method];
if(body != nil) {
NSError * error = [NSError new];
NSData * data = nil;
if([body isKindOfClass:[NSDictionary class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else if ([body isKindOfClass:[NSArray class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else {
data = [body dataUsingEncoding:NSUTF8StringEncoding];
}
NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSLog(@"request: %@", request);
}
[NSURLConnection sendAsynchronousRequest:request queue:_queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
int statusCode = [(NSHTTPURLResponse*)response statusCode];
NSString* jsonData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error) {
completionBlock(nil, error);
return;
}
else if (!NSLocationInRange(statusCode, NSMakeRange(200, 99))){
error = [NSError errorWithDomain:@"swagger"
code:statusCode
userInfo:[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error]];
completionBlock(nil, error);
return;
}
else {
NSDictionary* results = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
completionBlock(results, nil);
}
}];
return nil;
}
-(id) stringWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id) body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSString*, NSError *))completionBlock
{
NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path];
NSString * separator = nil;
int counter = 0;
if(queryParams != nil){
for(NSString * key in [queryParams keyEnumerator]){
if(counter == 0) separator = @"?";
else separator = @"&";
NSString * value;
if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){
value = [self escapeString:[queryParams valueForKey:key]];
}
else {
value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]];
}
[requestUrl appendFormat:[NSString stringWithFormat:@"%@%@=%@", separator,
[self escapeString:key], value]];
counter += 1;
}
}
NSLog(@"request url: %@", requestUrl);
NSURL* URL = [NSURL URLWithString:requestUrl];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
for(NSString * key in [_defaultHeaders keyEnumerator]){
[request setValue:[_defaultHeaders valueForKey:key] forHTTPHeaderField:key];
}
if(headerParams != nil){
for(NSString * key in [headerParams keyEnumerator]){
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[request setHTTPMethod:method];
if(body != nil) {
NSError * error = [NSError new];
NSData * data = nil;
if([body isKindOfClass:[NSDictionary class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else if ([body isKindOfClass:[NSArray class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else {
data = [body dataUsingEncoding:NSUTF8StringEncoding];
}
NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSLog(@"request: %@", request);
}
[NSURLConnection sendAsynchronousRequest:request queue:_queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
int statusCode = [(NSHTTPURLResponse*)response statusCode];
if (error) {
completionBlock(nil, error);
return;
}
else if (!NSLocationInRange(statusCode, NSMakeRange(200, 99))){
error = [NSError errorWithDomain:@"swagger"
code:statusCode
userInfo:[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error]];
completionBlock(nil, error);
return;
}
else {
NSString* results = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(results && [results length] >= 2) {
if(([results characterAtIndex:0] == '\"') && ([results characterAtIndex:([results length] - 1) == '\"'])){
results = [results substringWithRange:NSMakeRange(1, [results length] -2)];
}
}
completionBlock(results, nil);
}
}];
return nil;
}
@end

View File

@ -0,0 +1,13 @@
#import <Foundation/Foundation.h>
#import "NIKSwaggerObject.h"
@interface NIKDate : NIKSwaggerObject {
@private
NSDate *_date;
}
@property(nonatomic, readonly) NSDate* date;
- (id) initWithValues: (NSString*)input;
-(NSString*) toString;
@end

View File

@ -0,0 +1,34 @@
#import "NIKDate.h"
@implementation NIKDate
@synthesize date = _date;
- (id) initWithValues:(NSString*)input {
if([input isKindOfClass:[NSString class]]){
NSDateFormatter* df = [NSDateFormatter new];
NSLocale *locale = [[NSLocale new]
initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
_date = [df dateFromString:input];
}
else if([input isKindOfClass:[NSNumber class]]) {
NSTimeInterval interval = [input doubleValue];
_date = [[NSDate alloc] initWithTimeIntervalSince1970:interval];
}
return self;
}
-(NSString*) toString {
NSDateFormatter* df = [NSDateFormatter new];
NSLocale *locale = [[NSLocale new]
initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
return [df stringFromDate:_date];
}
@end

View File

@ -0,0 +1,6 @@
#import <Foundation/Foundation.h>
@interface NIKSwaggerObject : NSObject
- (id) initWithValues: (NSDictionary*)dict;
- (NSDictionary*) asDictionary;
@end

View File

@ -0,0 +1,10 @@
#import "NIKSwaggerObject.h"
@implementation NIKSwaggerObject
- (id) initWithValues: (NSDictionary*)dict {
return self;
}
- (NSDictionary*) asDictionary{
return [NSDictionary init];
}
@end

View File

@ -0,0 +1,29 @@
import com.wordnik.swagger.codegen.BasicObjcGenerator
import com.wordnik.swagger.core._
object ObjcPetstoreCodegen extends BasicObjcGenerator {
def main(args: Array[String]) = generateClient(args)
// template used for models
modelTemplateFiles += "model-header.mustache" -> ".h"
modelTemplateFiles += "model-body.mustache" -> ".m"
// template used for apis
apiTemplateFiles += "api-header.mustache" -> ".h"
apiTemplateFiles += "api-body.mustache" -> ".m"
// where to write generated code
override def destinationDir = "samples/client/petstore/objc"
// supporting classes
override def supportingFiles =
List(
("NIKSwaggerObject.h", destinationDir, "NIKSwaggerObject.h"),
("NIKSwaggerObject.m", destinationDir, "NIKSwaggerObject.m"),
("NIKApiInvoker.h", destinationDir, "NIKApiInvoker.h"),
("NIKApiInvoker.m", destinationDir, "NIKApiInvoker.m"),
("NIKDate.h", destinationDir, "NIKDate.h"),
("NIKDate.m", destinationDir, "NIKDate.m"))
}

View File

@ -0,0 +1,33 @@
#import <Foundation/Foundation.h>
@interface NIKApiInvoker : NSObject {
@private
NSOperationQueue *_queue;
NSMutableDictionary * _defaultHeaders;
}
@property(nonatomic, readonly) NSOperationQueue* queue;
@property(nonatomic, readonly) NSMutableDictionary * defaultHeaders;
-(void) addHeader:(NSString*) value
forKey:(NSString*)key;
-(NSString*) escapeString:(NSString*) string;
-(id) dictionaryWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id)body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSDictionary*, NSError *))completionBlock;
-(id) stringWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id)body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSString*, NSError *))completionBlock;
@end

View File

@ -0,0 +1,223 @@
#import "NIKApiInvoker.h"
@implementation NIKApiInvoker
@synthesize queue = _queue;
@synthesize defaultHeaders = _defaultHeaders;
- (id) init {
self = [super init];
_queue = [[NSOperationQueue alloc] init];
_defaultHeaders = [[NSMutableDictionary alloc] init];
return self;
}
-(void) addHeader:(NSString*) value
forKey:(NSString*)key {
[_defaultHeaders setValue:value forKey:key];
}
-(NSString*) escapeString:(NSString *)unescaped {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef) unescaped,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}
-(id) dictionaryWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id) body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSDictionary*, NSError *))completionBlock
{
NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path];
NSString * separator = nil;
int counter = 0;
if(queryParams != nil){
for(NSString * key in [queryParams keyEnumerator]){
if(counter == 0) separator = @"?";
else separator = @"&";
NSString * value;
if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){
value = [self escapeString:[queryParams valueForKey:key]];
}
else {
value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]];
}
[requestUrl appendFormat:[NSString stringWithFormat:@"%@%@=%@", separator,
[self escapeString:key], value]];
counter += 1;
}
}
NSLog(@"request url: %@", requestUrl);
NSURL* URL = [NSURL URLWithString:requestUrl];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
for(NSString * key in [_defaultHeaders keyEnumerator]){
[request setValue:[_defaultHeaders valueForKey:key] forHTTPHeaderField:key];
}
if(headerParams != nil){
for(NSString * key in [headerParams keyEnumerator]){
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[request setHTTPMethod:method];
if(body != nil) {
NSError * error = [NSError new];
NSData * data = nil;
if([body isKindOfClass:[NSDictionary class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else if ([body isKindOfClass:[NSArray class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else {
data = [body dataUsingEncoding:NSUTF8StringEncoding];
}
NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSLog(@"request: %@", request);
}
[NSURLConnection sendAsynchronousRequest:request queue:_queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
int statusCode = [(NSHTTPURLResponse*)response statusCode];
NSString* jsonData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error) {
completionBlock(nil, error);
return;
}
else if (!NSLocationInRange(statusCode, NSMakeRange(200, 99))){
error = [NSError errorWithDomain:@"swagger"
code:statusCode
userInfo:[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error]];
completionBlock(nil, error);
return;
}
else {
NSDictionary* results = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
completionBlock(results, nil);
}
}];
return nil;
}
-(id) stringWithCompletionBlock:(NSString*) path
method:(NSString*) method
queryParams:(NSDictionary*) queryParams
body:(id) body
headerParams:(NSDictionary*) headerParams
completionHandler:(void (^)(NSString*, NSError *))completionBlock
{
NSMutableString * requestUrl = [NSMutableString stringWithFormat:@"%@", path];
NSString * separator = nil;
int counter = 0;
if(queryParams != nil){
for(NSString * key in [queryParams keyEnumerator]){
if(counter == 0) separator = @"?";
else separator = @"&";
NSString * value;
if([[queryParams valueForKey:key] isKindOfClass:[NSString class]]){
value = [self escapeString:[queryParams valueForKey:key]];
}
else {
value = [NSString stringWithFormat:@"%@", [queryParams valueForKey:key]];
}
[requestUrl appendFormat:[NSString stringWithFormat:@"%@%@=%@", separator,
[self escapeString:key], value]];
counter += 1;
}
}
NSLog(@"request url: %@", requestUrl);
NSURL* URL = [NSURL URLWithString:requestUrl];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
for(NSString * key in [_defaultHeaders keyEnumerator]){
[request setValue:[_defaultHeaders valueForKey:key] forHTTPHeaderField:key];
}
if(headerParams != nil){
for(NSString * key in [headerParams keyEnumerator]){
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[request setHTTPMethod:method];
if(body != nil) {
NSError * error = [NSError new];
NSData * data = nil;
if([body isKindOfClass:[NSDictionary class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else if ([body isKindOfClass:[NSArray class]]){
data = [NSJSONSerialization dataWithJSONObject:body
options:kNilOptions error:&error];
}
else {
data = [body dataUsingEncoding:NSUTF8StringEncoding];
}
NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSLog(@"request: %@", request);
}
[NSURLConnection sendAsynchronousRequest:request queue:_queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
int statusCode = [(NSHTTPURLResponse*)response statusCode];
if (error) {
completionBlock(nil, error);
return;
}
else if (!NSLocationInRange(statusCode, NSMakeRange(200, 99))){
error = [NSError errorWithDomain:@"swagger"
code:statusCode
userInfo:[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error]];
completionBlock(nil, error);
return;
}
else {
NSString* results = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(results && [results length] >= 2) {
if(([results characterAtIndex:0] == '\"') && ([results characterAtIndex:([results length] - 1) == '\"'])){
results = [results substringWithRange:NSMakeRange(1, [results length] -2)];
}
}
completionBlock(results, nil);
}
}];
return nil;
}
@end

View File

@ -0,0 +1,13 @@
#import <Foundation/Foundation.h>
#import "NIKSwaggerObject.h"
@interface NIKDate : NIKSwaggerObject {
@private
NSDate *_date;
}
@property(nonatomic, readonly) NSDate* date;
- (id) initWithValues: (NSString*)input;
-(NSString*) toString;
@end

View File

@ -0,0 +1,34 @@
#import "NIKDate.h"
@implementation NIKDate
@synthesize date = _date;
- (id) initWithValues:(NSString*)input {
if([input isKindOfClass:[NSString class]]){
NSDateFormatter* df = [NSDateFormatter new];
NSLocale *locale = [[NSLocale new]
initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
_date = [df dateFromString:input];
}
else if([input isKindOfClass:[NSNumber class]]) {
NSTimeInterval interval = [input doubleValue];
_date = [[NSDate alloc] initWithTimeIntervalSince1970:interval];
}
return self;
}
-(NSString*) toString {
NSDateFormatter* df = [NSDateFormatter new];
NSLocale *locale = [[NSLocale new]
initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
return [df stringFromDate:_date];
}
@end

View File

@ -0,0 +1,6 @@
#import <Foundation/Foundation.h>
@interface NIKSwaggerObject : NSObject
- (id) initWithValues: (NSDictionary*)dict;
- (NSDictionary*) asDictionary;
@end

View File

@ -0,0 +1,10 @@
#import "NIKSwaggerObject.h"
@implementation NIKSwaggerObject
- (id) initWithValues: (NSDictionary*)dict {
return self;
}
- (NSDictionary*) asDictionary{
return [NSDictionary init];
}
@end

View File

@ -0,0 +1,225 @@
{{#operations}}
#import "{{classname}}.h"
{{#imports}}#import "{{import}}.h"
{{/imports}}
{{newline}}
@implementation {{classname}}
static NSString * basePath = @"{{basePath}}";
@synthesize queue = _queue;
@synthesize api = _api;
- (id) init {
self = [super init];
_queue = [[NSOperationQueue alloc] init];
_api = [[NIKApiInvoker alloc] init];
return self;
}
-(void) addHeader:(NSString*) value
forKey:(NSString*)key {
[_api addHeader:value forKey:key];
}
{{#operation}}
-(void) {{nickname}}WithCompletionBlock {{^allParams}}:{{/allParams}}{{#allParams}}{{#secondaryParam}} {{paramName}}{{/secondaryParam}}:({{{dataType}}}) {{paramName}} {{#hasMore}}{{newline}}{{/hasMore}}{{/allParams}}{{newline}}
{{#returnBaseType}}completionHandler:(void (^)({{returnType}}, NSError *))completionBlock{{/returnBaseType}}
{{^returnBaseType}}completionHandler:(void (^)(NSError *))completionBlock{{/returnBaseType}} {
NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@{{path}}", basePath];
// remove format in URL if needed
if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound)
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"];
{{#pathParams}}[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [_api escapeString:{{paramName}}]];
{{/pathParams}}
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
{{#queryParams}}if({{paramName}} != nil)
[queryParams setValue:{{paramName}} forKey:@"{{baseName}}"];
{{/queryParams}}
NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init];
{{#headerParams}}if({{paramName}} != nil)
[headerParams setValue:{{paramName}} forKey:@"{{baseName}}"];
{{/headerParams}}
id bodyDictionary = nil;
{{#bodyParam}}
if(body != nil && [body isKindOfClass:[NSArray class]]){
NSMutableArray * objs = [[NSMutableArray alloc] init];
for (id dict in (NSArray*)body) {
if([dict respondsToSelector:@selector(asDictionary)]) {
[objs addObject:[(NIKSwaggerObject*)dict asDictionary]];
}
else{
[objs addObject:dict];
}
}
bodyDictionary = objs;
}
else if([body respondsToSelector:@selector(asDictionary)]) {
bodyDictionary = [(NIKSwaggerObject*)body asDictionary];
}
else if([body isKindOfClass:[NSString class]]) {
bodyDictionary = body;
}
else{
NSLog(@"don't know what to do with %@", body);
}
{{/bodyParam}}
{{#requiredParamCount}}
{{#requiredParams}}
if({{paramName}} == nil) {
// error
}
{{/requiredParams}}
{{/requiredParamCount}}
{{#returnTypeIsPrimitive}}
[_api stringWithCompletionBlock: requestUrl
method: @"{{httpMethod}}"
queryParams: queryParams
body: bodyDictionary
headerParams: headerParams
completionHandler: ^(NSString *data, NSError *error) {
{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}}
[_api dictionaryWithCompletionBlock: requestUrl
method: @"{{httpMethod}}"
queryParams: queryParams
body: bodyDictionary
headerParams: headerParams
completionHandler: ^(NSDictionary *data, NSError *error) {
{{/returnTypeIsPrimitive}}
if (error) {
{{#returnBaseType}}completionBlock(nil, error);{{/returnBaseType}}
{{^returnBaseType}}completionBlock(error);{{/returnBaseType}}
return;
}
{{#returnBaseType}}
{{#returnContainer}}
if([data isKindOfClass:[NSArray class]]){
NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[data count]];
for (NSDictionary* dict in (NSArray*)data) {
{{{returnBaseType}}}* d = [[{{{returnBaseType}}} alloc]initWithValues: dict];
[objs addObject:d];
}
completionBlock(objs, nil);
}
{{/returnContainer}}
{{#returnSimpleType}}
{{#returnTypeIsPrimitive}}{{#returnBaseType}}completionBlock( [[{{returnBaseType}} alloc]initWithString: data], nil);{{/returnBaseType}}
{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}}
{{#returnBaseType}}completionBlock( [[{{returnBaseType}} alloc]initWithValues: data], nil);{{/returnBaseType}}
{{/returnTypeIsPrimitive}}
{{/returnSimpleType}}
{{/returnBaseType}}
{{^returnBaseType}}completionBlock(nil);{{/returnBaseType}}{{newline}}
}];
{{newline}}
}
{{/operation}}
{{#operation}}
-(void) {{nickname}}AsJsonWithCompletionBlock {{^allParams}}:{{/allParams}}{{#allParams}}{{#secondaryParam}} {{paramName}}{{/secondaryParam}}:({{{dataType}}}) {{paramName}} {{#hasMore}}{{newline}}{{/hasMore}}{{/allParams}}{{newline}}
{{#returnBaseType}}completionHandler:(void (^)(NSString*, NSError *))completionBlock{{/returnBaseType}}
{{^returnBaseType}}completionHandler:(void (^)(NSError *))completionBlock{{/returnBaseType}} {
NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@{{path}}", basePath];
// remove format in URL if needed
if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound)
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""];
{{#pathParams}}[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"{{baseName}}", @"}"]] withString: [_api escapeString:{{paramName}}]];
{{/pathParams}}
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
{{#queryParams}}if({{paramName}} != nil)
[queryParams setValue:{{paramName}} forKey:@"{{baseName}}"];
{{/queryParams}}
NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init];
{{#headerParams}}if({{paramName}} != nil)
[headerParams setValue:{{paramName}} forKey:@"{{baseName}}"];
{{/headerParams}}
id bodyDictionary = nil;
{{#bodyParam}}
if(body != nil && [body isKindOfClass:[NSArray class]]){
NSMutableArray * objs = [[NSMutableArray alloc] init];
for (id dict in (NSArray*)body) {
if([dict respondsToSelector:@selector(asDictionary)]) {
[objs addObject:[(NIKSwaggerObject*)dict asDictionary]];
}
else{
[objs addObject:dict];
}
}
bodyDictionary = objs;
}
else if([body respondsToSelector:@selector(asDictionary)]) {
bodyDictionary = [(NIKSwaggerObject*)body asDictionary];
}
else if([body isKindOfClass:[NSString class]]) {
bodyDictionary = body;
}
else{
NSLog(@"don't know what to do with %@", body);
}
{{/bodyParam}}
{{#requiredParamCount}}
{{#requiredParams}}
if({{paramName}} == nil) {
// error
}
{{/requiredParams}}
{{/requiredParamCount}}
[_api dictionaryWithCompletionBlock: requestUrl
method: @"{{httpMethod}}"
queryParams: queryParams
body: bodyDictionary
headerParams: headerParams
completionHandler: ^(NSDictionary *data, NSError *error) {
if (error) {
{{#returnBaseType}}completionBlock(nil, error);{{/returnBaseType}}
{{^returnBaseType}}completionBlock(error);{{/returnBaseType}}
return;
}
{{#returnBaseType}}
NSData * responseData = nil;
if([data isKindOfClass:[NSDictionary class]]){
responseData = [NSJSONSerialization dataWithJSONObject:data
options:kNilOptions error:&error];
}
else if ([data isKindOfClass:[NSArray class]]){
responseData = [NSJSONSerialization dataWithJSONObject:data
options:kNilOptions error:&error];
}
NSString * json = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
completionBlock(json, nil);
{{/returnBaseType}}
{{^returnBaseType}}completionBlock(nil);{{/returnBaseType}}{{newline}}
}];
{{newline}}
}
{{/operation}}
{{newline}}
{{/operations}}
@end

View File

@ -0,0 +1,29 @@
#import <Foundation/Foundation.h>
#import "NIKApiInvoker.h"
{{#imports}}#import "{{import}}.h"
{{/imports}}
{{newline}}
{{#operations}}
@interface {{classname}}: NSObject {
@private
NSOperationQueue *_queue;
NIKApiInvoker * _api;
}
@property(nonatomic, readonly) NSOperationQueue* queue;
@property(nonatomic, readonly) NIKApiInvoker* api;
-(void) addHeader:(NSString*) value
forKey:(NSString*)key;
{{#operation}}
-(void) {{nickname}}WithCompletionBlock {{^allParams}}:{{/allParams}}{{#allParams}}{{#secondaryParam}} {{paramName}}{{/secondaryParam}}:({{{dataType}}}) {{paramName}} {{#hasMore}}{{newline}}{{/hasMore}}{{/allParams}}{{newline}}
{{#returnBaseType}}completionHandler:(void (^)({{returnType}}, NSError *))completionBlock;{{/returnBaseType}}
{{^returnBaseType}}completionHandler:(void (^)(NSError *))completionBlock;{{/returnBaseType}}
{{newline}}
{{/operation}}
{{/operations}}
@end

View File

@ -0,0 +1,92 @@
{{#models}}
{{#model}}
#import "NIKDate.h"
#import "{{classname}}.h"
@implementation {{classname}}
@synthesize raw = _raw;
{{#vars}}
@synthesize {{name}} = _{{name}};
{{/vars}}
- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}
{{/vars}}
{
{{#vars}}_{{name}} = {{name}};
{{/vars}}
return self;
}
- (id) initWithValues: (NSDictionary*)dict
{
{{#vars}}
{{#isPrimitiveType}}
_{{name}} = [dict objectForKey:@"{{baseName}}"];
{{/isPrimitiveType}}
{{#complexType}}
id {{name}}_dict = [dict objectForKey:@"{{name}}"];
{{#isContainer}}
if([{{name}}_dict isKindOfClass:[NSArray class]]) {
if([(NSArray*){{name}}_dict count] > 0) {
NSMutableArray * objs = [[NSMutableArray alloc] initWithCapacity:[(NSArray*){{name}}_dict count]];
for (NSDictionary* dict in (NSArray*){{name}}_dict) {
{{{complexType}}}* d = [[{{{complexType}}} alloc]initWithValues:dict];
[objs addObject:d];
}
_{{{name}}} = [[NSArray alloc] initWithArray:objs];
}
}
else if([{{name}}_dict isKindOfClass:[NSDictionary class]] && [(NSDictionary*){{name}}_dict count] > 0) {
_{{name}} = [[{{complexType}} alloc]initWithValues:(NSDictionary*){{name}}_dict];
}
{{/isContainer}}
{{#isNotContainer}}
_{{name}} = [[{{complexType}} alloc]initWithValues:{{name}}_dict];
{{/isNotContainer}}
{{/complexType}}
{{/vars}}
self.raw = [[NSDictionary alloc] initWithDictionary:dict];
return self;
}
-(NSDictionary*) asDictionary {
NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
{{#vars}}
{{#complexType}}
if(_{{name}} != nil){
if([_{{name}} isKindOfClass:[NSArray class]]){
NSMutableArray * array = [[NSMutableArray alloc] init];
for( {{complexType}} * {{name}} in (NSArray*)_{{name}}) {
[array addObject:[(NIKSwaggerObject*){{name}} asDictionary]];
}
[dict setObject:array forKey:@"{{baseName}}"];
}
else if(_{{name}} && [_{{name}} isKindOfClass:[NIKDate class]]) {
NSString * dateString = [(NIKDate*)_{{name}} toString];
if(dateString){
[dict setObject:dateString forKey:@"{{name}}"];
}
}
}
else {
{{/complexType}}
if(_{{name}} != nil) [dict setObject:{{#complexType}}[(NIKSwaggerObject*){{/complexType}}_{{name}} {{#complexType}}asDictionary]{{/complexType}} forKey:@"{{baseName}}"];
{{#complexType}}
}
{{/complexType}}
{{/vars}}
NSDictionary* output = [dict copy];
return output;
}
-(NSDictionary*) asRaw {
return _raw;
}
{{/model}}
@end
{{/models}}

View File

@ -0,0 +1,31 @@
#import <Foundation/Foundation.h>
#import "NIKSwaggerObject.h"
{{#imports}}#import "{{import}}.h"
{{/imports}}
{{newline}}
{{#models}}
{{#model}}
@interface {{classname}} : NIKSwaggerObject {
@private
NSDictionary* raw;
{{#vars}}
{{datatype}} _{{name}}; //{{baseType}}
{{/vars}}
}
@property(nonatomic) NSDictionary* raw;
{{newline}}
{{#vars}}
@property(nonatomic) {{datatype}} {{name}};
{{/vars}}
- (id) {{#vars}}{{name}}: ({{datatype}}) {{name}}{{#hasMore}}{{newline}} {{/hasMore}}{{^hasMore}};{{/hasMore}}
{{/vars}}
{{newline}}
- (id) initWithValues: (NSDictionary*)dict;
- (NSDictionary*) asDictionary;
- (NSDictionary*) asRaw;
{{newline}}
@end
{{/model}}
{{/models}}