diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache index 07e000450d0..b1e2c3fc8bf 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -60,20 +60,26 @@ static void (^reachabilityChangeBlock)(int); #pragma mark - Log Methods -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request - error:(NSError*)error { ++ (void)debugLog:(NSString *)method + message:(NSString *)format, ... { {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig]; - if (!config.debug) { return; } - NSString *message = [NSString stringWithFormat:@"\n[DEBUG] Request body \n~BEGIN~\n %@\n~END~\n"\ - "[DEBUG] HTTP Response body \n~BEGIN~\n %@\n~END~\n", - [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding], - operation.responseString]; + NSMutableString *message = [NSMutableString stringWithCapacity:1]; + if (method) { + [message appendString:[NSString stringWithFormat:@"%@: ", method]]; + } + + va_list args; + va_start(args, format); + + [message appendString:[[NSString alloc] initWithFormat:format arguments:args]]; + + // If set logging file handler, log into file, + // otherwise log into console. if (config.loggingFileHanlder) { [config.loggingFileHanlder seekToEndOfFile]; [config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]]; @@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int); else { NSLog(@"%@", message); } + + va_end(args); +} + +- (void)logResponse:(AFHTTPRequestOperation *)operation + forRequest:(NSURLRequest *)request + error:(NSError*)error { + + NSString *message = [NSString stringWithFormat:@"\n[DEBUG] HTTP request body \n~BEGIN~\n %@\n~END~\n"\ + "[DEBUG] HTTP response body \n~BEGIN~\n %@\n~END~\n", + [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding], + operation.responseString]; + + DebugLog(message); } #pragma mark - Cache Methods @@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int); +(NSNumber*) nextRequestId { @synchronized(self) { long nextId = ++requestId; - if([[{{classPrefix}}Configuration sharedConfig] debug]) - NSLog(@"got id %ld", nextId); + DebugLog(@"got id %ld", nextId); return [NSNumber numberWithLong:nextId]; } } +(NSNumber*) queueRequest { NSNumber* requestId = [{{classPrefix}}ApiClient nextRequestId]; - if([[{{classPrefix}}Configuration sharedConfig] debug]) { - NSLog(@"added %@ to request queue", requestId); - } + DebugLog(@"added %@ to request queue", requestId); [queuedRequests addObject:requestId]; return requestId; } @@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int); }]; if(matchingItems.count == 1) { - if([[{{classPrefix}}Configuration sharedConfig] debug]){ - NSLog(@"removing request id %@", requestId); - } + DebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; } @@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int); reachabilityStatus = status; switch (status) { case AFNetworkReachabilityStatusUnknown: - if([[{{classPrefix}}Configuration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); [{{classPrefix}}ApiClient setOfflineState:true]; break; case AFNetworkReachabilityStatusNotReachable: - if([[{{classPrefix}}Configuration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); [{{classPrefix}}ApiClient setOfflineState:true]; break; case AFNetworkReachabilityStatusReachableViaWWAN: - if([[{{classPrefix}}Configuration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); [{{classPrefix}}ApiClient setOfflineState:false]; break; case AFNetworkReachabilityStatusReachableViaWiFi: - if([[{{classPrefix}}Configuration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); [{{classPrefix}}ApiClient setOfflineState:false]; break; default: @@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int); AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) { if([self executeRequestWithId:requestId]) { - if([[{{classPrefix}}Configuration sharedConfig] debug]) { - [self logResponse:operation forRequest:request error:nil]; - } + [self logResponse:operation forRequest:request error:nil]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(response, nil); @@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int); userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - - if([[{{classPrefix}}Configuration sharedConfig] debug]) - [self logResponse:nil forRequest:request error:augmentedError]; + [self logResponse:nil forRequest:request error:augmentedError]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; @@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int); NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - if ([[{{classPrefix}}Configuration sharedConfig] debug]) { - [self logResponse:nil forRequest:request error:augmentedError]; - } + + [self logResponse:nil forRequest:request error:augmentedError]; + NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(nil, augmentedError); @@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int); hasHeaderParams = true; } if(offlineState) { - if ([[{{classPrefix}}Configuration sharedConfig] debug]){ - NSLog(@"%@ cache forced", resourcePath); - } + DebugLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - if ([[{{classPrefix}}Configuration sharedConfig] debug]){ - NSLog(@"%@ cache enabled", resourcePath); - } + DebugLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - if ([[{{classPrefix}}Configuration sharedConfig] debug]){ - NSLog(@"%@ cache disabled", resourcePath); - } + DebugLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } diff --git a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache index 6a9aadfa08b..212e361bb79 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache @@ -24,6 +24,10 @@ */ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; +/** + * Log debug message macro + */ +#define DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__]; @interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager @@ -215,4 +219,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; */ - (NSString *) parameterToString: (id) param; +/** + * Log debug message + */ ++(void)debugLog:(NSString *)method message:(NSString *)format, ...; + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h index c703d8a22d2..b6e6cf57a47 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h @@ -28,6 +28,10 @@ */ extern NSString *const SWGResponseObjectErrorKey; +/** + * Log debug message macro + */ +#define DebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__]; @interface SWGApiClient : AFHTTPRequestOperationManager @@ -219,4 +223,9 @@ extern NSString *const SWGResponseObjectErrorKey; */ - (NSString *) parameterToString: (id) param; +/** + * Log debug message + */ ++(void)debugLog:(NSString *)method message:(NSString *)format, ...; + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index 8197569e0b5..fc5ca43440d 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -60,20 +60,26 @@ static void (^reachabilityChangeBlock)(int); #pragma mark - Log Methods -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request - error:(NSError*)error { ++ (void)debugLog:(NSString *)method + message:(NSString *)format, ... { SWGConfiguration *config = [SWGConfiguration sharedConfig]; - if (!config.debug) { return; } - NSString *message = [NSString stringWithFormat:@"\n[DEBUG] Request body \n~BEGIN~\n %@\n~END~\n"\ - "[DEBUG] HTTP Response body \n~BEGIN~\n %@\n~END~\n", - [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding], - operation.responseString]; + NSMutableString *message = [NSMutableString stringWithCapacity:1]; + if (method) { + [message appendString:[NSString stringWithFormat:@"%@: ", method]]; + } + + va_list args; + va_start(args, format); + + [message appendString:[[NSString alloc] initWithFormat:format arguments:args]]; + + // If set logging file handler, log into file, + // otherwise log into console. if (config.loggingFileHanlder) { [config.loggingFileHanlder seekToEndOfFile]; [config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]]; @@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int); else { NSLog(@"%@", message); } + + va_end(args); +} + +- (void)logResponse:(AFHTTPRequestOperation *)operation + forRequest:(NSURLRequest *)request + error:(NSError*)error { + + NSString *message = [NSString stringWithFormat:@"\n[DEBUG] HTTP request body \n~BEGIN~\n %@\n~END~\n"\ + "[DEBUG] HTTP response body \n~BEGIN~\n %@\n~END~\n", + [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding], + operation.responseString]; + + DebugLog(message); } #pragma mark - Cache Methods @@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int); +(NSNumber*) nextRequestId { @synchronized(self) { long nextId = ++requestId; - if([[SWGConfiguration sharedConfig] debug]) - NSLog(@"got id %ld", nextId); + DebugLog(@"got id %ld", nextId); return [NSNumber numberWithLong:nextId]; } } +(NSNumber*) queueRequest { NSNumber* requestId = [SWGApiClient nextRequestId]; - if([[SWGConfiguration sharedConfig] debug]) { - NSLog(@"added %@ to request queue", requestId); - } + DebugLog(@"added %@ to request queue", requestId); [queuedRequests addObject:requestId]; return requestId; } @@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int); }]; if(matchingItems.count == 1) { - if([[SWGConfiguration sharedConfig] debug]){ - NSLog(@"removing request id %@", requestId); - } + DebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; } @@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int); reachabilityStatus = status; switch (status) { case AFNetworkReachabilityStatusUnknown: - if([[SWGConfiguration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); [SWGApiClient setOfflineState:true]; break; case AFNetworkReachabilityStatusNotReachable: - if([[SWGConfiguration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); [SWGApiClient setOfflineState:true]; break; case AFNetworkReachabilityStatusReachableViaWWAN: - if([[SWGConfiguration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); [SWGApiClient setOfflineState:false]; break; case AFNetworkReachabilityStatusReachableViaWiFi: - if([[SWGConfiguration sharedConfig] debug]) - NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); + DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); [SWGApiClient setOfflineState:false]; break; default: @@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int); AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id response) { if([self executeRequestWithId:requestId]) { - if([[SWGConfiguration sharedConfig] debug]) { - [self logResponse:operation forRequest:request error:nil]; - } + [self logResponse:operation forRequest:request error:nil]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(response, nil); @@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int); userInfo[SWGResponseObjectErrorKey] = operation.responseObject; } NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - - if([[SWGConfiguration sharedConfig] debug]) - [self logResponse:nil forRequest:request error:augmentedError]; + [self logResponse:nil forRequest:request error:augmentedError]; NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; @@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int); NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - if ([[SWGConfiguration sharedConfig] debug]) { - [self logResponse:nil forRequest:request error:augmentedError]; - } + + [self logResponse:nil forRequest:request error:augmentedError]; + NSDictionary *responseHeaders = [[operation response] allHeaderFields]; self.HTTPResponseHeaders = responseHeaders; completionBlock(nil, augmentedError); @@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int); hasHeaderParams = true; } if(offlineState) { - if ([[SWGConfiguration sharedConfig] debug]){ - NSLog(@"%@ cache forced", resourcePath); - } + DebugLog(@"%@ cache forced", resourcePath); [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; } else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { - if ([[SWGConfiguration sharedConfig] debug]){ - NSLog(@"%@ cache enabled", resourcePath); - } + DebugLog(@"%@ cache enabled", resourcePath); [request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; } else { - if ([[SWGConfiguration sharedConfig] debug]){ - NSLog(@"%@ cache disabled", resourcePath); - } + DebugLog(@"%@ cache disabled", resourcePath); [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; } diff --git a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SwaggerClient-Info.plist b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SwaggerClient-Info.plist index d2cd320c27d..d617744f3f4 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SwaggerClient-Info.plist +++ b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SwaggerClient-Info.plist @@ -2,6 +2,11 @@ + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + CFBundleDevelopmentRegion en CFBundleDisplayName