updated logic, merged

This commit is contained in:
Tony Tam 2015-04-04 22:12:30 -07:00
parent 14cbc2d90d
commit 448462552f
6 changed files with 265 additions and 103 deletions

View File

@ -129,8 +129,10 @@ static NSString * basePath = @"{{basePath}}";
if(bodyDictionary == nil) {
bodyDictionary = [[NSMutableArray alloc] init];
}
[bodyDictionary addObject:{{paramName}}];
{{paramName}}.paramName = @"{{baseName}}";
if({{paramName}} != nil) {
[bodyDictionary addObject:{{paramName}}];
{{paramName}}.paramName = @"{{baseName}}";
}
{{/isFile}}
if(bodyDictionary == nil) {
bodyDictionary = [[NSMutableArray alloc] init];

View File

@ -154,7 +154,7 @@
}
- (void)testDeletePet {
XCTestExpectation *expectation = [self expectationWithDescription:@"testGetPetById"];
XCTestExpectation *expectation = [self expectationWithDescription:@"testDeletePet"];
SWGPet* pet = [self createPet];
@ -181,6 +181,42 @@
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
- (void)testUploadFile {
XCTestExpectation *expectation = [self expectationWithDescription:@"testUploadFile"];
NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
SWGFile * file = [[SWGFile alloc] initWithNameData: @"myFile.txt" mimeType:@"text/plain" data:data];
[api uploadFileWithCompletionBlock:@1 additionalMetadata:@"special-metadata" file:file completionHandler:^(NSError *error) {
if(error) {
// good
XCTFail(@"expected a failure");
}
else {
[expectation fulfill];
}
}];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
- (void)TestUploadWithoutFile {
XCTestExpectation *expectation = [self expectationWithDescription:@"testUploadWithoutFile"];
[api uploadFileWithCompletionBlock:@1 additionalMetadata:@"special-metadata" file:nil completionHandler:^(NSError *error) {
if(error) {
XCTFail(@"failed to upload");
}
else {
[expectation fulfill];
}
}];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
- (SWGPet*) createPet {
SWGPet * pet = [[SWGPet alloc] init];
pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]];

View File

@ -274,6 +274,27 @@ static bool loggingEnabled = true;
requestContentType: (NSString*) requestContentType
responseContentType: (NSString*) responseContentType
completionBlock: (void (^)(NSDictionary*, NSError *))completionBlock {
// setting request serializer
if ([requestContentType isEqualToString:@"application/json"]) {
self.requestSerializer = [AFJSONRequestSerializer serializer];
}
else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
}
else if ([requestContentType isEqualToString:@"multipart/form-data"]) {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
}
else {
NSAssert(false, @"unsupport request type %@", requestContentType);
}
// setting response serializer
if ([responseContentType isEqualToString:@"application/json"]) {
self.responseSerializer = [AFJSONResponseSerializer serializer];
}
else {
self.responseSerializer = [AFHTTPResponseSerializer serializer];
}
NSMutableURLRequest * request = nil;
if (body != nil && [body isKindOfClass:[NSArray class]]){
@ -292,6 +313,7 @@ static bool loggingEnabled = true;
}
NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString];
// request with multipart form
if(file != nil) {
request = [self.requestSerializer multipartFormRequestWithMethod: @"POST"
URLString: urlString
@ -311,6 +333,16 @@ static bool loggingEnabled = true;
}
error:nil];
}
// request with form parameters
else {
NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
request = [self.requestSerializer requestWithMethod:method
URLString:urlString
parameters:params
error:nil];
}
}
else {
NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
@ -337,11 +369,9 @@ static bool loggingEnabled = true;
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
if(body != nil) {
if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){
[requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
[self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
}
else if ([body isKindOfClass:[SWGFile class]]) {}
else {
@ -353,7 +383,7 @@ static bool loggingEnabled = true;
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
[self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
// Always disable cookies!
[request setHTTPShouldHandleCookies:NO];
@ -400,6 +430,28 @@ static bool loggingEnabled = true;
requestContentType: (NSString*) requestContentType
responseContentType: (NSString*) responseContentType
completionBlock: (void (^)(NSString*, NSError *))completionBlock {
// setting request serializer
if ([requestContentType isEqualToString:@"application/json"]) {
self.requestSerializer = [AFJSONRequestSerializer serializer];
}
else if ([requestContentType isEqualToString:@"application/x-www-form-urlencoded"]) {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
}
else if ([requestContentType isEqualToString:@"multipart/form-data"]) {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
}
else {
NSAssert(false, @"unsupport request type %@", requestContentType);
}
// setting response serializer
if ([responseContentType isEqualToString:@"application/json"]) {
self.responseSerializer = [AFJSONResponseSerializer serializer];
}
else {
self.responseSerializer = [AFHTTPResponseSerializer serializer];
}
NSMutableURLRequest * request = nil;
if (body != nil && [body isKindOfClass:[NSArray class]]){
SWGFile * file;
@ -417,6 +469,7 @@ static bool loggingEnabled = true;
}
NSString * urlString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString];
// request with multipart form
if(file != nil) {
request = [self.requestSerializer multipartFormRequestWithMethod: @"POST"
URLString: urlString
@ -436,6 +489,17 @@ static bool loggingEnabled = true;
}
error:nil];
}
// request with form parameters
else {
NSString* pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
NSString* urlString = [[NSURL URLWithString:pathWithQueryParams relativeToURL:self.baseURL] absoluteString];
request = [self.requestSerializer requestWithMethod:method
URLString:urlString
parameters:params
error:nil];
}
}
else {
NSString * pathWithQueryParams = [self pathWithQueryParamsToString:path queryParams:queryParams];
@ -463,11 +527,9 @@ static bool loggingEnabled = true;
}
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
if(body != nil) {
if([body isKindOfClass:[NSDictionary class]] || [body isKindOfClass:[NSArray class]]){
[requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
[self.requestSerializer setValue:requestContentType forHTTPHeaderField:@"Content-Type"];
}
else if ([body isKindOfClass:[SWGFile class]]){}
else {
@ -479,7 +541,7 @@ static bool loggingEnabled = true;
[request setValue:[headerParams valueForKey:key] forHTTPHeaderField:key];
}
}
[requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
[self.requestSerializer setValue:responseContentType forHTTPHeaderField:@"Accept"];
// Always disable cookies!

View File

@ -65,8 +65,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[@"application/json", @"application/xml", ];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -146,8 +149,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[@"application/json", @"application/xml", ];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -227,8 +233,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
if(status != nil) {
@ -304,8 +313,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
if(tags != nil) {
@ -382,8 +394,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -457,8 +472,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[@"application/x-www-form-urlencoded", ];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -533,8 +551,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -596,8 +617,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"petId", @"}"]] withString: [SWGApiClient escape:petId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[@"multipart/form-data", ];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -624,8 +648,10 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
if(bodyDictionary == nil) {
bodyDictionary = [[NSMutableArray alloc] init];
}
[bodyDictionary addObject:file];
file.paramName = @"file";
if(file != nil) {
[bodyDictionary addObject:file];
file.paramName = @"file";
}
if(bodyDictionary == nil) {
bodyDictionary = [[NSMutableArray alloc] init];

View File

@ -63,8 +63,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -127,8 +130,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -223,8 +229,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -296,8 +305,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"orderId", @"}"]] withString: [SWGApiClient escape:orderId]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];

View File

@ -64,8 +64,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -145,8 +148,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -226,8 +232,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -308,8 +317,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
if(username != nil) {
@ -385,8 +397,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -444,8 +459,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -518,8 +536,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
@ -600,8 +621,11 @@ static NSString * basePath = @"http://petstore.swagger.io/v2";
[requestUrl replaceCharactersInRange: [requestUrl rangeOfString:[NSString stringWithFormat:@"%@%@%@", @"{", @"username", @"}"]] withString: [SWGApiClient escape:username]];
NSString* requestContentType = @"application/json";
NSString* responseContentType = @"application/json";
NSArray* requestContentTypes = @[];
NSString* requestContentType = [requestContentTypes count] > 0 ? requestContentTypes[0] : @"application/json";
NSArray* responseContentTypes = @[@"application/json", @"application/xml"];
NSString* responseContentType = [responseContentTypes count] > 0 ? responseContentTypes[0] : @"application/json";
NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];