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 a8644bab030..b5a0c30349a 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache @@ -10,6 +10,30 @@ static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilitySta static void (^reachabilityChangeBlock)(int); +static NSDictionary * {{classPrefix}}__headerFieldsForResponse(NSURLResponse *response) { + if(![response isKindOfClass:[NSHTTPURLResponse class]]) { + return nil; + } + return ((NSHTTPURLResponse*)response).allHeaderFields; +} + +static NSString * {{classPrefix}}__fileNameForResponse(NSURLResponse *response) { + NSDictionary * headers = {{classPrefix}}__headerFieldsForResponse(response); + if(!headers[@"Content-Disposition"]) { + return [NSString stringWithFormat:@"%@", [[NSProcessInfo processInfo] globallyUniqueString]]; + } + NSString *pattern = @"filename=['\"]?([^'\"\\s]+)['\"]?"; + NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:pattern + options:NSRegularExpressionCaseInsensitive + error:nil]; + NSString *contentDispositionHeader = headers[@"Content-Disposition"]; + NSTextCheckingResult *match = [regexp firstMatchInString:contentDispositionHeader + options:0 + range:NSMakeRange(0, [contentDispositionHeader length])]; + return [contentDispositionHeader substringWithRange:[match rangeAtIndex:1]]; +} + + @interface {{classPrefix}}ApiClient () @property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders; @@ -96,14 +120,12 @@ static void (^reachabilityChangeBlock)(int); va_end(args); } -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request - error:(NSError*)error { +- (void)logResponse:(NSURLResponse *)response responseObject:(id)responseObject request:(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]; + responseObject]; {{classPrefix}}DebugLog(message); } @@ -219,20 +241,14 @@ static void (^reachabilityChangeBlock)(int); -(Boolean) executeRequestWithId:(NSNumber*) requestId { NSSet* matchingItems = [queuedRequests objectsPassingTest:^BOOL(id obj, BOOL *stop) { - if ([obj intValue] == [requestId intValue]) { - return YES; - } - else { - return NO; - } + return [obj intValue] == [requestId intValue]; }]; if (matchingItems.count == 1) { {{classPrefix}}DebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; - } - else { + } else { return NO; } } @@ -243,7 +259,7 @@ static void (^reachabilityChangeBlock)(int); return reachabilityStatus; } -+(bool) getOfflineState { ++(BOOL) getOfflineState { return offlineState; } @@ -254,29 +270,8 @@ static void (^reachabilityChangeBlock)(int); - (void) configureCacheReachibility { [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { reachabilityStatus = status; - switch (status) { - case AFNetworkReachabilityStatusUnknown: - {{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); - [{{classPrefix}}ApiClient setOfflineState:true]; - break; - - case AFNetworkReachabilityStatusNotReachable: - {{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); - [{{classPrefix}}ApiClient setOfflineState:true]; - break; - - case AFNetworkReachabilityStatusReachableViaWWAN: - {{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); - [{{classPrefix}}ApiClient setOfflineState:false]; - break; - - case AFNetworkReachabilityStatusReachableViaWiFi: - {{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); - [{{classPrefix}}ApiClient setOfflineState:false]; - break; - default: - break; - } + {{classPrefix}}DebugLog(@"reachability changed to %@",AFStringFromNetworkReachabilityStatus(status)); + [{{classPrefix}}ApiClient setOfflineState:status == AFNetworkReachabilityStatusUnknown || status == AFNetworkReachabilityStatusNotReachable]; // call the reachability block, if configured if (reachabilityChangeBlock != nil) { @@ -292,92 +287,60 @@ static void (^reachabilityChangeBlock)(int); - (void) operationWithCompletionBlock: (NSURLRequest *)request requestId: (NSNumber *) requestId completionBlock: (void (^)(id, NSError *))completionBlock { - AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request - success:^(AFHTTPRequestOperation *operation, id response) { - if ([self executeRequestWithId:requestId]) { - [self logResponse:operation forRequest:request error:nil]; - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - completionBlock(response, nil); - } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - if ([self executeRequestWithId:requestId]) { - NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if (operation.responseObject) { - // Add in the (parsed) response body. - userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject; - } - NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - [self logResponse:nil forRequest:request error:augmentedError]; - - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - - completionBlock(nil, augmentedError); - } - }]; - - [self.operationQueue addOperation:op]; + __weak __typeof(self)weakSelf = self; + NSURLSessionDataTask* op = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if (![strongSelf executeRequestWithId:requestId]) { + return; + } + [strongSelf logResponse:response responseObject:responseObject request:request error:error]; + strongSelf.HTTPResponseHeaders = {{classPrefix}}__headerFieldsForResponse(response); + if(!error) { + completionBlock(responseObject, nil); + return; + } + NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; + if (responseObject) { + // Add in the (parsed) response body. + userInfo[{{classPrefix}}ResponseObjectErrorKey] = responseObject; + } + NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; + completionBlock(nil, augmentedError); + }]; + [op resume]; } - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request requestId: (NSNumber *) requestId completionBlock: (void (^)(id, NSError *))completionBlock { - AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request - success:^(AFHTTPRequestOperation *operation, id responseObject) { - {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig]; - NSString *directory = nil; - if (config.tempFolderPath) { - directory = config.tempFolderPath; - } - else { - directory = NSTemporaryDirectory(); - } + __weak __typeof(self)weakSelf = self; + NSURLSessionDataTask* op = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if (![strongSelf executeRequestWithId:requestId]) { + return; + } + strongSelf.HTTPResponseHeaders = {{classPrefix}}__headerFieldsForResponse(response); + [strongSelf logResponse:response responseObject:responseObject request:request error:error]; + if(error) { + NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; + if (responseObject) { + userInfo[{{classPrefix}}ResponseObjectErrorKey] = responseObject; + } + NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; + completionBlock(nil, augmentedError); + } + {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig]; + NSString *directory = config.tempFolderPath ?: NSTemporaryDirectory(); + NSString * filename = {{classPrefix}}__fileNameForResponse(response); - NSDictionary *headers = operation.response.allHeaderFields; - NSString *filename = nil; - if ([headers objectForKey:@"Content-Disposition"]) { + NSString *filepath = [directory stringByAppendingPathComponent:filename]; + NSURL *file = [NSURL fileURLWithPath:filepath]; - NSString *pattern = @"filename=['\"]?([^'\"\\s]+)['\"]?"; - NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:pattern - options:NSRegularExpressionCaseInsensitive - error:nil]; - NSString *contentDispositionHeader = [headers objectForKey:@"Content-Disposition"]; - NSTextCheckingResult *match = [regexp firstMatchInString:contentDispositionHeader - options:0 - range:NSMakeRange(0, [contentDispositionHeader length])]; - filename = [contentDispositionHeader substringWithRange:[match rangeAtIndex:1]]; - } - else { - filename = [NSString stringWithFormat:@"%@", [[NSProcessInfo processInfo] globallyUniqueString]]; - } + [responseObject writeToURL:file atomically:YES]; - NSString *filepath = [directory stringByAppendingPathComponent:filename]; - NSURL *file = [NSURL fileURLWithPath:filepath]; - - [operation.responseData writeToURL:file atomically:YES]; - self.HTTPResponseHeaders = headers; - completionBlock(file, nil); - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - - if ([self executeRequestWithId:requestId]) { - NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if (operation.responseObject) { - userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject; - } - - NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - - - [self logResponse:nil forRequest:request error:augmentedError]; - - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - completionBlock(nil, augmentedError); - } - }]; - - [self.operationQueue addOperation:op]; + completionBlock(file, nil); + }]; + [op resume]; } #pragma mark - Perform Request Methods @@ -502,7 +465,7 @@ static void (^reachabilityChangeBlock)(int); [request setHTTPShouldHandleCookies:NO]; NSNumber* requestId = [{{classPrefix}}ApiClient queueRequest]; - if ([responseType isEqualToString:@"NSURL*"]) { + if ([responseType isEqualToString:@"NSURL*"] || [responseType isEqualToString:@"NSURL"]) { [self downloadOperationWithCompletionBlock:request requestId:requestId completionBlock:^(id data, NSError *error) { completionBlock(data, error); }]; 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 c7caa7ab11e..f2abaee7725 100644 --- a/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache @@ -1,6 +1,6 @@ #import #import -#import +#import #import "{{classPrefix}}JSONResponseSerializer.h" #import "{{classPrefix}}JSONRequestSerializer.h" #import "{{classPrefix}}QueryParamCollection.h" @@ -30,7 +30,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; */ #define {{classPrefix}}DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__]; -@interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager +@interface {{classPrefix}}ApiClient : AFHTTPSessionManager @property(nonatomic, assign) NSURLRequestCachePolicy cachePolicy; @property(nonatomic, assign) NSTimeInterval timeoutInterval; @@ -71,7 +71,7 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; * * @return The client offline state */ -+(bool) getOfflineState; ++(BOOL) getOfflineState; /** * Sets the client reachability, this may be overridden by the reachability manager if reachability changes @@ -170,12 +170,14 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey; /** * Logs request and response * - * @param operation AFHTTPRequestOperation for the HTTP request. + * @param response NSURLResponse for the HTTP request. + * @param responseObject response object of the HTTP request. * @param request The HTTP request. * @param error The error of the HTTP request. */ -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request +- (void)logResponse:(NSURLResponse *)response + responseObject:(id)responseObject + request:(NSURLRequest *)request error:(NSError *)error; /** diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h index 7dea87a3327..117b930ba9f 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.h @@ -1,6 +1,6 @@ #import #import -#import +#import #import "SWGJSONResponseSerializer.h" #import "SWGJSONRequestSerializer.h" #import "SWGQueryParamCollection.h" @@ -34,7 +34,7 @@ extern NSString *const SWGResponseObjectErrorKey; */ #define SWGDebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__]; -@interface SWGApiClient : AFHTTPRequestOperationManager +@interface SWGApiClient : AFHTTPSessionManager @property(nonatomic, assign) NSURLRequestCachePolicy cachePolicy; @property(nonatomic, assign) NSTimeInterval timeoutInterval; @@ -75,7 +75,7 @@ extern NSString *const SWGResponseObjectErrorKey; * * @return The client offline state */ -+(bool) getOfflineState; ++(BOOL) getOfflineState; /** * Sets the client reachability, this may be overridden by the reachability manager if reachability changes @@ -174,12 +174,14 @@ extern NSString *const SWGResponseObjectErrorKey; /** * Logs request and response * - * @param operation AFHTTPRequestOperation for the HTTP request. + * @param response NSURLResponse for the HTTP request. + * @param responseObject response object of the HTTP request. * @param request The HTTP request. * @param error The error of the HTTP request. */ -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request +- (void)logResponse:(NSURLResponse *)response + responseObject:(id)responseObject + request:(NSURLRequest *)request error:(NSError *)error; /** diff --git a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m index 5d5d2019b0f..eec71ed05e8 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGApiClient.m @@ -10,6 +10,30 @@ static AFNetworkReachabilityStatus reachabilityStatus = AFNetworkReachabilitySta static void (^reachabilityChangeBlock)(int); +static NSDictionary * SWG__headerFieldsForResponse(NSURLResponse *response) { + if(![response isKindOfClass:[NSHTTPURLResponse class]]) { + return nil; + } + return ((NSHTTPURLResponse*)response).allHeaderFields; +} + +static NSString * SWG__fileNameForResponse(NSURLResponse *response) { + NSDictionary * headers = SWG__headerFieldsForResponse(response); + if(!headers[@"Content-Disposition"]) { + return [NSString stringWithFormat:@"%@", [[NSProcessInfo processInfo] globallyUniqueString]]; + } + NSString *pattern = @"filename=['\"]?([^'\"\\s]+)['\"]?"; + NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:pattern + options:NSRegularExpressionCaseInsensitive + error:nil]; + NSString *contentDispositionHeader = headers[@"Content-Disposition"]; + NSTextCheckingResult *match = [regexp firstMatchInString:contentDispositionHeader + options:0 + range:NSMakeRange(0, [contentDispositionHeader length])]; + return [contentDispositionHeader substringWithRange:[match rangeAtIndex:1]]; +} + + @interface SWGApiClient () @property (readwrite, nonatomic) NSDictionary *HTTPResponseHeaders; @@ -96,14 +120,12 @@ static void (^reachabilityChangeBlock)(int); va_end(args); } -- (void)logResponse:(AFHTTPRequestOperation *)operation - forRequest:(NSURLRequest *)request - error:(NSError*)error { +- (void)logResponse:(NSURLResponse *)response responseObject:(id)responseObject request:(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]; + responseObject]; SWGDebugLog(message); } @@ -219,20 +241,14 @@ static void (^reachabilityChangeBlock)(int); -(Boolean) executeRequestWithId:(NSNumber*) requestId { NSSet* matchingItems = [queuedRequests objectsPassingTest:^BOOL(id obj, BOOL *stop) { - if ([obj intValue] == [requestId intValue]) { - return YES; - } - else { - return NO; - } + return [obj intValue] == [requestId intValue]; }]; if (matchingItems.count == 1) { SWGDebugLog(@"removed request id %@", requestId); [queuedRequests removeObject:requestId]; return YES; - } - else { + } else { return NO; } } @@ -243,7 +259,7 @@ static void (^reachabilityChangeBlock)(int); return reachabilityStatus; } -+(bool) getOfflineState { ++(BOOL) getOfflineState { return offlineState; } @@ -254,29 +270,8 @@ static void (^reachabilityChangeBlock)(int); - (void) configureCacheReachibility { [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { reachabilityStatus = status; - switch (status) { - case AFNetworkReachabilityStatusUnknown: - SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown"); - [SWGApiClient setOfflineState:true]; - break; - - case AFNetworkReachabilityStatusNotReachable: - SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable"); - [SWGApiClient setOfflineState:true]; - break; - - case AFNetworkReachabilityStatusReachableViaWWAN: - SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN"); - [SWGApiClient setOfflineState:false]; - break; - - case AFNetworkReachabilityStatusReachableViaWiFi: - SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi"); - [SWGApiClient setOfflineState:false]; - break; - default: - break; - } + SWGDebugLog(@"reachability changed to %@",AFStringFromNetworkReachabilityStatus(status)); + [SWGApiClient setOfflineState:status == AFNetworkReachabilityStatusUnknown || status == AFNetworkReachabilityStatusNotReachable]; // call the reachability block, if configured if (reachabilityChangeBlock != nil) { @@ -292,92 +287,60 @@ static void (^reachabilityChangeBlock)(int); - (void) operationWithCompletionBlock: (NSURLRequest *)request requestId: (NSNumber *) requestId completionBlock: (void (^)(id, NSError *))completionBlock { - AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request - success:^(AFHTTPRequestOperation *operation, id response) { - if ([self executeRequestWithId:requestId]) { - [self logResponse:operation forRequest:request error:nil]; - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - completionBlock(response, nil); - } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - if ([self executeRequestWithId:requestId]) { - NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if (operation.responseObject) { - // Add in the (parsed) response body. - userInfo[SWGResponseObjectErrorKey] = operation.responseObject; - } - NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - [self logResponse:nil forRequest:request error:augmentedError]; - - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - - completionBlock(nil, augmentedError); - } - }]; - - [self.operationQueue addOperation:op]; + __weak __typeof(self)weakSelf = self; + NSURLSessionDataTask* op = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if (![strongSelf executeRequestWithId:requestId]) { + return; + } + [strongSelf logResponse:response responseObject:responseObject request:request error:error]; + strongSelf.HTTPResponseHeaders = SWG__headerFieldsForResponse(response); + if(!error) { + completionBlock(responseObject, nil); + return; + } + NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; + if (responseObject) { + // Add in the (parsed) response body. + userInfo[SWGResponseObjectErrorKey] = responseObject; + } + NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; + completionBlock(nil, augmentedError); + }]; + [op resume]; } - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request requestId: (NSNumber *) requestId completionBlock: (void (^)(id, NSError *))completionBlock { - AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request - success:^(AFHTTPRequestOperation *operation, id responseObject) { - SWGConfiguration *config = [SWGConfiguration sharedConfig]; - NSString *directory = nil; - if (config.tempFolderPath) { - directory = config.tempFolderPath; - } - else { - directory = NSTemporaryDirectory(); - } + __weak __typeof(self)weakSelf = self; + NSURLSessionDataTask* op = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + __strong __typeof(weakSelf)strongSelf = weakSelf; + if (![strongSelf executeRequestWithId:requestId]) { + return; + } + strongSelf.HTTPResponseHeaders = SWG__headerFieldsForResponse(response); + [strongSelf logResponse:response responseObject:responseObject request:request error:error]; + if(error) { + NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; + if (responseObject) { + userInfo[SWGResponseObjectErrorKey] = responseObject; + } + NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; + completionBlock(nil, augmentedError); + } + SWGConfiguration *config = [SWGConfiguration sharedConfig]; + NSString *directory = config.tempFolderPath ?: NSTemporaryDirectory(); + NSString * filename = SWG__fileNameForResponse(response); - NSDictionary *headers = operation.response.allHeaderFields; - NSString *filename = nil; - if ([headers objectForKey:@"Content-Disposition"]) { + NSString *filepath = [directory stringByAppendingPathComponent:filename]; + NSURL *file = [NSURL fileURLWithPath:filepath]; - NSString *pattern = @"filename=['\"]?([^'\"\\s]+)['\"]?"; - NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:pattern - options:NSRegularExpressionCaseInsensitive - error:nil]; - NSString *contentDispositionHeader = [headers objectForKey:@"Content-Disposition"]; - NSTextCheckingResult *match = [regexp firstMatchInString:contentDispositionHeader - options:0 - range:NSMakeRange(0, [contentDispositionHeader length])]; - filename = [contentDispositionHeader substringWithRange:[match rangeAtIndex:1]]; - } - else { - filename = [NSString stringWithFormat:@"%@", [[NSProcessInfo processInfo] globallyUniqueString]]; - } + [responseObject writeToURL:file atomically:YES]; - NSString *filepath = [directory stringByAppendingPathComponent:filename]; - NSURL *file = [NSURL fileURLWithPath:filepath]; - - [operation.responseData writeToURL:file atomically:YES]; - self.HTTPResponseHeaders = headers; - completionBlock(file, nil); - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - - if ([self executeRequestWithId:requestId]) { - NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - if (operation.responseObject) { - userInfo[SWGResponseObjectErrorKey] = operation.responseObject; - } - - NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; - - - [self logResponse:nil forRequest:request error:augmentedError]; - - NSDictionary *responseHeaders = [[operation response] allHeaderFields]; - self.HTTPResponseHeaders = responseHeaders; - completionBlock(nil, augmentedError); - } - }]; - - [self.operationQueue addOperation:op]; + completionBlock(file, nil); + }]; + [op resume]; } #pragma mark - Perform Request Methods @@ -502,7 +465,7 @@ static void (^reachabilityChangeBlock)(int); [request setHTTPShouldHandleCookies:NO]; NSNumber* requestId = [SWGApiClient queueRequest]; - if ([responseType isEqualToString:@"NSURL*"]) { + if ([responseType isEqualToString:@"NSURL*"] || [responseType isEqualToString:@"NSURL"]) { [self downloadOperationWithCompletionBlock:request requestId:requestId completionBlock:^(id data, NSError *error) { completionBlock(data, error); }];