#import "NIKUserApi.h" #import "NIKUser.h" @implementation NIKUserApi static NSString * basePath = @"http://petstore.swagger.wordnik.com/api"; @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]; } -(void) createUsersWithArrayInputWithCompletionBlock :(NSArray*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/createWithArray", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) createUserWithCompletionBlock :(NIKUser*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) createUsersWithListInputWithCompletionBlock :(NSArray*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/createWithList", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) updateUserWithCompletionBlock :(NSString*) username body:(NIKUser*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(username == nil) { // error } if(body == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"PUT" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) deleteUserWithCompletionBlock :(NSString*) username completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"DELETE" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) getUserByNameWithCompletionBlock :(NSString*) username completionHandler:(void (^)(NIKUser*, NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(nil, error);return; } completionBlock( [[NIKUser alloc]initWithValues: data], nil); }]; } -(void) loginUserWithCompletionBlock :(NSString*) username password:(NSString*) password completionHandler:(void (^)(NSString*, NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/login", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; if(username != nil) [queryParams setValue:username forKey:@"username"]; if(password != nil) [queryParams setValue:password forKey:@"password"]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } if(password == nil) { // error } [_api stringWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(nil, error);return; } completionBlock( [[NSString alloc]initWithString: data], nil); }]; } -(void) logoutUserWithCompletionBlock : completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/logout", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@".json"]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; [_api stringWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSString *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) createUsersWithArrayInputAsJsonWithCompletionBlock :(NSArray*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/createWithArray", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) createUserAsJsonWithCompletionBlock :(NIKUser*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) createUsersWithListInputAsJsonWithCompletionBlock :(NSArray*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/createWithList", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(body == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"POST" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) updateUserAsJsonWithCompletionBlock :(NSString*) username body:(NIKUser*) body completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; 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); } if(username == nil) { // error } if(body == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"PUT" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) deleteUserAsJsonWithCompletionBlock :(NSString*) username completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"DELETE" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } -(void) getUserByNameAsJsonWithCompletionBlock :(NSString*) username completionHandler:(void (^)(NSString*, NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/{username}", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [_api escapeString:username]]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(nil, error);return; } 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); }]; } -(void) loginUserAsJsonWithCompletionBlock :(NSString*) username password:(NSString*) password completionHandler:(void (^)(NSString*, NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/login", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; if(username != nil) [queryParams setValue:username forKey:@"username"]; if(password != nil) [queryParams setValue:password forKey:@"password"]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; if(username == nil) { // error } if(password == nil) { // error } [_api dictionaryWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(nil, error);return; } 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); }]; } -(void) logoutUserAsJsonWithCompletionBlock : completionHandler:(void (^)(NSError *))completionBlock{ NSMutableString* requestUrl = [NSMutableString stringWithFormat:@"%@/user.{format}/logout", basePath]; // remove format in URL if needed if ([requestUrl rangeOfString:@".{format}"].location != NSNotFound) [requestUrl replaceCharactersInRange: [requestUrl rangeOfString:@".{format}"] withString:@""]; NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init]; NSMutableDictionary* headerParams = [[NSMutableDictionary alloc] init]; id bodyDictionary = nil; [_api dictionaryWithCompletionBlock: requestUrl method: @"GET" queryParams: queryParams body: bodyDictionary headerParams: headerParams completionHandler: ^(NSDictionary *data, NSError *error) { if (error) { completionBlock(error);return; } completionBlock(nil); }]; } @end