From 0120310b9116f6905f2a2853891f629b544e7304 Mon Sep 17 00:00:00 2001 From: Levey Date: Fri, 18 Aug 2017 14:19:04 +0800 Subject: [PATCH 1/2] Use NSURLSession api to make it compatible with AFNetworking 3 --- AFAmazonS3Manager/AFAmazonS3Manager.h | 111 ++++----- AFAmazonS3Manager/AFAmazonS3Manager.m | 227 ++++++++---------- .../AFAmazonS3RequestSerializer.h | 2 +- .../AFAmazonS3RequestSerializer.m | 2 +- .../AFAmazonS3ResponseSerializer.h | 1 + 5 files changed, 156 insertions(+), 187 deletions(-) diff --git a/AFAmazonS3Manager/AFAmazonS3Manager.h b/AFAmazonS3Manager/AFAmazonS3Manager.h index b93084f..6814529 100644 --- a/AFAmazonS3Manager/AFAmazonS3Manager.h +++ b/AFAmazonS3Manager/AFAmazonS3Manager.h @@ -20,13 +20,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#import "AFHTTPRequestOperationManager.h" +#import #import "AFAmazonS3RequestSerializer.h" /** AFAmazonS3Manager` is an `AFHTTPRequestOperationManager` subclass for interacting with the Amazon S3 webservice API (http://aws.amazon.com/s3/). */ -@interface AFAmazonS3Manager : AFHTTPRequestOperationManager +@interface AFAmazonS3Manager : AFHTTPSessionManager /** The base URL for the S3 manager. @@ -59,13 +59,13 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued on operationQueue + @return The data task */ -- (AFHTTPRequestOperation *)enqueueS3RequestOperationWithMethod:(NSString *)method - path:(NSString *)path - parameters:(NSDictionary *)parameters - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)enqueueS3DataTaskWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; ///------------------------- /// @name Service Operations @@ -77,10 +77,11 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued on operationQueue + @return The data task */ -- (AFHTTPRequestOperation *)getServiceWithSuccess:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)getServiceWithSuccess:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; + ///------------------------ @@ -94,11 +95,11 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued on operationQueue + @return The data task */ -- (AFHTTPRequestOperation *)getBucket:(NSString *)bucket - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)getBucket:(NSString *)bucket + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; /** Creates a new bucket belonging to the account of the authenticated request sender. Optionally, you can specify a EU (Ireland) or US-West (N. California) location constraint. @@ -110,10 +111,10 @@ @return The operation that was enqueued on operationQueue */ -- (AFHTTPRequestOperation *)putBucket:(NSString *)bucket +- (NSURLSessionDataTask *)putBucket:(NSString *)bucket parameters:(NSDictionary *)parameters - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; /** Deletes the specified bucket. All objects in the bucket must be deleted before the bucket itself can be deleted. @@ -122,11 +123,11 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued on operationQueue + @return The data task */ -- (AFHTTPRequestOperation *)deleteBucket:(NSString *)bucket - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)deleteBucket:(NSString *)bucket + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; ///---------------------------------------------- /// @name Object Operations @@ -139,41 +140,25 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. */ -- (AFHTTPRequestOperation *)headObjectWithPath:(NSString *)path - success:(void (^)(NSHTTPURLResponse *response))success - failure:(void (^)(NSError *error))failure; - -/** - Gets an object for a user that has read access to the object. - - @param path The object path. Must not be `nil`. - @param progress A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. - @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. - @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - - @return The operation that was enqueued - */ -- (AFHTTPRequestOperation *)getObjectWithPath:(NSString *)path - progress:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))progress - success:(void (^)(id responseObject, NSData *responseData))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)headObjectWithPath:(NSString *)path + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; /** Gets an object for a user that has read access to the object. @param path The object path. Must not be `nil`. - @param outputStream The `NSOutputStream` object receiving data from the request. @param progress A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued + @return The data task */ -- (AFHTTPRequestOperation *)getObjectWithPath:(NSString *)path - outputStream:(NSOutputStream *)outputStream - progress:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination + success:(void (^)(NSURLResponse *, NSURL *))success + failure:(void (^)(NSURLResponse *, NSError *))failure; /** Adds an object to a bucket using forms. @@ -187,12 +172,12 @@ @discussion `destinationPath` is relative to the bucket's root, and will create any intermediary directories as necessary. For example, specifying "/a/b/c.txt" will create the "/a" and / or "/a/b" directories within the bucket, if they do not already exist, and upload the source file as "c.txt" into "/a/b". */ -- (AFHTTPRequestOperation *)postObjectWithFile:(NSString *)path - destinationPath:(NSString *)destinationPath - parameters:(NSDictionary *)parameters - progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)postObjectWithFile:(NSString *)path + destinationPath:(NSString *)destinationPath + parameters:(NSDictionary *)parameters + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + success:(void (^)(NSURLResponse *, id))success + failure:(void (^)(NSURLResponse *, NSError *))failure; /** Adds an object to a bucket for a user that has write access to the bucket. A success response indicates the object was successfully stored; if the object already exists, it will be overwritten. @@ -208,12 +193,12 @@ @discussion `destinationPath` is relative to the bucket's root, and will create any intermediary directories as necessary. For example, specifying "/a/b/c.txt" will create the "/a" and / or "/a/b" directories within the bucket, if they do not already exist, and upload the source file as "c.txt" into "/a/b". */ -- (AFHTTPRequestOperation *)putObjectWithFile:(NSString *)path - destinationPath:(NSString *)destinationPath - parameters:(NSDictionary *)parameters - progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)putObjectWithFile:(NSString *)path + destinationPath:(NSString *)destinationPath + parameters:(NSDictionary *)parameters + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + success:(void (^)(NSURLResponse *, id))success + failure:(void (^)(NSURLResponse *, NSError *))failure; /** Deletes the specified object. Once deleted, there is no method to restore or undelete an object. @@ -222,11 +207,11 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. - @return The operation that was enqueued on operationQueue + @return The data task */ -- (AFHTTPRequestOperation *)deleteObjectWithPath:(NSString *)path - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure; +- (NSURLSessionDataTask *)deleteObjectWithPath:(NSString *)path + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; @end diff --git a/AFAmazonS3Manager/AFAmazonS3Manager.m b/AFAmazonS3Manager/AFAmazonS3Manager.m index 8075821..13f9629 100644 --- a/AFAmazonS3Manager/AFAmazonS3Manager.m +++ b/AFAmazonS3Manager/AFAmazonS3Manager.m @@ -35,7 +35,6 @@ @interface AFAmazonS3Manager () @implementation AFAmazonS3Manager @synthesize baseURL = _s3_baseURL; -@dynamic requestSerializer; - (instancetype)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; @@ -72,177 +71,159 @@ - (NSURL *)baseURL { #pragma mark - -- (AFHTTPRequestOperation *)enqueueS3RequestOperationWithMethod:(NSString *)method - path:(NSString *)path - parameters:(NSDictionary *)parameters - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)enqueueS3DataTaskWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; { NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[self.baseURL URLByAppendingPathComponent:path] absoluteString] parameters:parameters error:nil]; - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(__unused AFHTTPRequestOperation *operation, id responseObject) { - if (success) { - success(responseObject); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); + + __block NSURLSessionDataTask *dataTask = nil; + dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { + if (error) { + if (failure) { + failure(dataTask, error); + } + } else { + if (success) { + success(dataTask, responseObject); + } } }]; - - [self.operationQueue addOperation:requestOperation]; + [dataTask resume]; - return requestOperation; + return dataTask; } #pragma mark Service Operations -- (AFHTTPRequestOperation *)getServiceWithSuccess:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)getServiceWithSuccess:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { - return [self enqueueS3RequestOperationWithMethod:@"GET" path:@"/" parameters:nil success:success failure:failure]; + return [self enqueueS3DataTaskWithMethod:@"GET" path:@"/" parameters:nil success:success failure:failure]; } #pragma mark Bucket Operations -- (AFHTTPRequestOperation *)getBucket:(NSString *)bucket - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)getBucket:(NSString *)bucket + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { NSParameterAssert(bucket); - return [self enqueueS3RequestOperationWithMethod:@"GET" path:bucket parameters:nil success:success failure:failure]; + return [self enqueueS3DataTaskWithMethod:@"GET" path:bucket parameters:nil success:success failure:failure]; } -- (AFHTTPRequestOperation *)putBucket:(NSString *)bucket +- (NSURLSessionDataTask *)putBucket:(NSString *)bucket parameters:(NSDictionary *)parameters - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { NSParameterAssert(bucket); - return [self enqueueS3RequestOperationWithMethod:@"PUT" path:bucket parameters:parameters success:success failure:failure]; + return [self enqueueS3DataTaskWithMethod:@"PUT" path:bucket parameters:parameters success:success failure:failure]; } -- (AFHTTPRequestOperation *)deleteBucket:(NSString *)bucket - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)deleteBucket:(NSString *)bucket + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { NSParameterAssert(bucket); - return [self enqueueS3RequestOperationWithMethod:@"DELETE" path:bucket parameters:nil success:success failure:failure]; + return [self enqueueS3DataTaskWithMethod:@"DELETE" path:bucket parameters:nil success:success failure:failure]; } #pragma mark Object Operations -- (AFHTTPRequestOperation *)headObjectWithPath:(NSString *)path - success:(void (^)(NSHTTPURLResponse *response))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)headObjectWithPath:(NSString *)path + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { NSParameterAssert(path); path = AFPathByEscapingSpacesWithPlusSigns(path); NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"HEAD" URLString:[[self.baseURL URLByAppendingPathComponent:path] absoluteString] parameters:nil error:nil]; - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, __unused id responseObject) { - if (success) { - success(operation.response); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); - } - }]; - [self.operationQueue addOperation:requestOperation]; - - return requestOperation; -} - -- (AFHTTPRequestOperation *)getObjectWithPath:(NSString *)path - progress:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))progress - success:(void (^)(id responseObject, NSData *responseData))success - failure:(void (^)(NSError *error))failure -{ - NSParameterAssert(path); - - path = AFPathByEscapingSpacesWithPlusSigns(path); - - NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[self.baseURL URLByAppendingPathComponent:path] absoluteString] parameters:nil error:nil]; - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { - if (success) { - success(responseObject, operation.responseData); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); + __block NSURLSessionDataTask *dataTask = nil; + dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) { + if (error) { + if (failure) { + failure(dataTask, error); + } + } else { + if (success) { + success(dataTask, responseObject); + } } }]; - - [requestOperation setDownloadProgressBlock:progress]; - - [self.operationQueue addOperation:requestOperation]; - return requestOperation; + [dataTask resume]; + + return dataTask; } -- (AFHTTPRequestOperation *)getObjectWithPath:(NSString *)path - outputStream:(NSOutputStream *)outputStream - progress:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination + success:(void (^)(NSURLResponse *, NSURL *))success + failure:(void (^)(NSURLResponse *, NSError *))failure { NSParameterAssert(path); path = AFPathByEscapingSpacesWithPlusSigns(path); NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[self.baseURL URLByAppendingPathComponent:path] absoluteString] parameters:nil error:nil]; - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(__unused AFHTTPRequestOperation *operation, id responseObject) { - if (success) { - success(responseObject); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); - } - }]; - - requestOperation.outputStream = outputStream; - - [requestOperation setDownloadProgressBlock:progress]; + + __block NSURLSessionDataTask *dataTask = nil; + + dataTask = [self downloadTaskWithRequest:request progress:progress destination:destination + completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { + if (error) { + if (failure) { + failure(response, error); + } + } else { + if (success) { + success(response, filePath); + } + } + }]; + [dataTask resume]; - [self.operationQueue addOperation:requestOperation]; - return requestOperation; + return dataTask; } -- (AFHTTPRequestOperation *)postObjectWithFile:(NSString *)path - destinationPath:(NSString *)destinationPath - parameters:(NSDictionary *)parameters - progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)postObjectWithFile:(NSString *)path + destinationPath:(NSString *)destinationPath + parameters:(NSDictionary *)parameters + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + success:(void (^)(NSURLResponse *, id))success + failure:(void (^)(NSURLResponse *, NSError *))failure { return [self setObjectWithMethod:@"POST" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure]; } -- (AFHTTPRequestOperation *)putObjectWithFile:(NSString *)path +- (NSURLSessionDataTask *)putObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + success:(void (^)(NSURLResponse *, id))success + failure:(void (^)(NSURLResponse *, NSError *))failure { return [self setObjectWithMethod:@"PUT" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure]; } -- (AFHTTPRequestOperation *)setObjectWithMethod:(NSString *)method +- (NSURLSessionDataTask *)setObjectWithMethod:(NSString *)method file:(NSString *)filePath destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure + progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + success:(void (^)(NSURLResponse *, id))success + failure:(void (^)(NSURLResponse *, NSError *))failure { NSParameterAssert(method); NSParameterAssert(filePath); @@ -257,7 +238,7 @@ - (AFHTTPRequestOperation *)setObjectWithMethod:(NSString *)method if (fileError || !response || !data) { if (failure) { - failure(fileError); + failure(response, fileError); } return nil; @@ -278,7 +259,7 @@ - (AFHTTPRequestOperation *)setObjectWithMethod:(NSString *)method if (requestError || !request) { if (failure) { - failure(requestError); + failure(response, requestError); } return nil; @@ -295,33 +276,35 @@ - (AFHTTPRequestOperation *)setObjectWithMethod:(NSString *)method request.HTTPBody = data; } + __block NSURLSessionDataTask *dataTask = nil; - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(__unused AFHTTPRequestOperation *operation, id responseObject) { - if (success) { - success(responseObject); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); + + dataTask = [self uploadTaskWithRequest:request fromFile:filePath progress:progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + if (error) { + if (failure) { + failure(response, error); + } + } else { + if (success) { + success(response, filePath); + } } }]; - - [requestOperation setUploadProgressBlock:progress]; - - [self.operationQueue addOperation:requestOperation]; - return requestOperation; + [dataTask resume]; + + return dataTask; } -- (AFHTTPRequestOperation *)deleteObjectWithPath:(NSString *)path - success:(void (^)(id responseObject))success - failure:(void (^)(NSError *error))failure +- (NSURLSessionDataTask *)deleteObjectWithPath:(NSString *)path + success:(void (^)(NSURLSessionDataTask *, id))success + failure:(void (^)(NSURLSessionDataTask *, NSError *))failure { NSParameterAssert(path); path = AFPathByEscapingSpacesWithPlusSigns(path); - return [self enqueueS3RequestOperationWithMethod:@"DELETE" path:path parameters:nil success:success failure:failure]; + return [self enqueueS3DataTaskWithMethod:@"DELETE" path:path parameters:nil success:success failure:failure]; } #pragma mark - NSKeyValueObserving diff --git a/AFAmazonS3Manager/AFAmazonS3RequestSerializer.h b/AFAmazonS3Manager/AFAmazonS3RequestSerializer.h index 739682b..cc4bf4f 100644 --- a/AFAmazonS3Manager/AFAmazonS3RequestSerializer.h +++ b/AFAmazonS3Manager/AFAmazonS3RequestSerializer.h @@ -20,7 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#import "AFURLRequestSerialization.h" +#import /** `AFAmazonS3RequestSerializer` is an `AFHTTPRequestSerializer` subclass with convenience methods for creating requests for the Amazon S3 webservice, including creating an authorization header and building an endpoint URL for a given bucket, region, and TLS preferences. diff --git a/AFAmazonS3Manager/AFAmazonS3RequestSerializer.m b/AFAmazonS3Manager/AFAmazonS3RequestSerializer.m index 2fad707..3d0af12 100644 --- a/AFAmazonS3Manager/AFAmazonS3RequestSerializer.m +++ b/AFAmazonS3Manager/AFAmazonS3RequestSerializer.m @@ -32,7 +32,7 @@ NSString * const AFAmazonS3EUCentral1Region = @"s3-eu-central-1.amazonaws.com"; NSString * const AFAmazonS3APSoutheast1Region = @"s3-ap-southeast-1.amazonaws.com"; NSString * const AFAmazonS3APSoutheast2Region = @"s3-ap-southeast-2.amazonaws.com"; -NSString * const AFAmazonS3APNortheast1Region = @"s3-ap-northeast-1.amazonaws.com"; +NSString * const AFAmazonS3APNortheast2Region = @"s3-ap-northeast-1.amazonaws.com"; NSString * const AFAmazonS3SAEast1Region = @"s3-sa-east-1.amazonaws.com"; static NSTimeInterval const AFAmazonS3DefaultExpirationTimeInterval = 60 * 60; diff --git a/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h b/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h index 7e4e666..976289f 100644 --- a/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h +++ b/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h @@ -21,6 +21,7 @@ // THE SOFTWARE. #import "AFURLResponseSerialization.h" +#import /** Returns an `AFAmazonS3ResponseObject` object from the AmazonS3 HTTP response From 1d60ebab25d3555191171dd38dfb5a68a045bebf Mon Sep 17 00:00:00 2001 From: Levey Date: Fri, 18 Aug 2017 16:24:17 +0800 Subject: [PATCH 2/2] update API --- AFAmazonS3Manager/AFAmazonS3Manager.h | 24 +++++------ AFAmazonS3Manager/AFAmazonS3Manager.m | 42 ++++++++++--------- .../AFAmazonS3ResponseSerializer.h | 1 - 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/AFAmazonS3Manager/AFAmazonS3Manager.h b/AFAmazonS3Manager/AFAmazonS3Manager.h index 6814529..55f0b94 100644 --- a/AFAmazonS3Manager/AFAmazonS3Manager.h +++ b/AFAmazonS3Manager/AFAmazonS3Manager.h @@ -155,10 +155,10 @@ @return The data task */ - (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination - success:(void (^)(NSURLResponse *, NSURL *))success - failure:(void (^)(NSURLResponse *, NSError *))failure; + progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock + destination:(nullable NSURL * _Nullable (^)(NSURL * _Nullable targetPath, NSURLResponse * _Nullable response))destination + success:(void (^_Nullable)(NSURLResponse * _Nullable response, NSURL * _Nullable filePath))success + failure:(void (^_Nullable)(NSURLResponse * _Nullable response, NSError * _Nullable error))failure; /** Adds an object to a bucket using forms. @@ -175,9 +175,9 @@ - (NSURLSessionDataTask *)postObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - success:(void (^)(NSURLResponse *, id))success - failure:(void (^)(NSURLResponse *, NSError *))failure; + progress:(nullable void (^)(NSProgress * _Nullable uploadProgress))uploadProgressBlock + success:(void (^_Nullable)(NSURLResponse * _Nullable response, id _Nullable responseObject))success + failure:(void (^_Nullable)(NSURLResponse * _Nullable response, NSError * _Nullable error))failure; /** Adds an object to a bucket for a user that has write access to the bucket. A success response indicates the object was successfully stored; if the object already exists, it will be overwritten. @@ -196,9 +196,9 @@ - (NSURLSessionDataTask *)putObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - success:(void (^)(NSURLResponse *, id))success - failure:(void (^)(NSURLResponse *, NSError *))failure; + progress:(nullable void (^)(NSProgress * _Nullable uploadProgress))uploadProgressBlock + success:(void (^_Nullable)(NSURLResponse * _Nullable response, id _Nullable responseObject))success + failure:(void (^_Nullable)(NSURLResponse * _Nullable response, NSError * _Nullable error))failure; /** Deletes the specified object. Once deleted, there is no method to restore or undelete an object. @@ -210,8 +210,8 @@ @return The data task */ - (NSURLSessionDataTask *)deleteObjectWithPath:(NSString *)path - success:(void (^)(NSURLSessionDataTask *, id))success - failure:(void (^)(NSURLSessionDataTask *, NSError *))failure; + success:(void (^_Nullable)(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject))success + failure:(void (^_Nullable)(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error))failure; @end diff --git a/AFAmazonS3Manager/AFAmazonS3Manager.m b/AFAmazonS3Manager/AFAmazonS3Manager.m index 13f9629..ac46193 100644 --- a/AFAmazonS3Manager/AFAmazonS3Manager.m +++ b/AFAmazonS3Manager/AFAmazonS3Manager.m @@ -166,10 +166,10 @@ - (NSURLSessionDataTask *)headObjectWithPath:(NSString *)path } - (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - destination:(nullable NSURL * (^)(NSURL *targetPath, NSURLResponse *response))destination - success:(void (^)(NSURLResponse *, NSURL *))success - failure:(void (^)(NSURLResponse *, NSError *))failure + progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgressBlock + destination:(nullable NSURL * _Nullable (^)(NSURL * _Nullable targetPath, NSURLResponse * _Nullable response))destination + success:(void (^_Nullable)(NSURLResponse * _Nullable response, NSURL * _Nullable filePath))success + failure:(void (^_Nullable)(NSURLResponse * _Nullable response, NSError * _Nullable error))failure { NSParameterAssert(path); @@ -179,7 +179,7 @@ - (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path __block NSURLSessionDataTask *dataTask = nil; - dataTask = [self downloadTaskWithRequest:request progress:progress destination:destination + dataTask = [self downloadTaskWithRequest:request progress:downloadProgressBlock destination:destination completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { if (error) { if (failure) { @@ -200,36 +200,38 @@ - (NSURLSessionDataTask *)getObjectWithPath:(NSString *)path - (NSURLSessionDataTask *)postObjectWithFile:(NSString *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - success:(void (^)(NSURLResponse *, id))success - failure:(void (^)(NSURLResponse *, NSError *))failure + progress:(void (^)(NSProgress * uploadProgress))uploadProgressBlock + success:(void (^)(NSURLResponse *response, id responseObject))success + failure:(void (^)(NSURLResponse *response, NSError *error))failure { - return [self setObjectWithMethod:@"POST" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure]; + return [self setObjectWithMethod:@"POST" file:path destinationPath:destinationPath parameters:parameters progress:uploadProgressBlock success:success failure:failure]; } - (NSURLSessionDataTask *)putObjectWithFile:(NSString *)path - destinationPath:(NSString *)destinationPath - parameters:(NSDictionary *)parameters - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress - success:(void (^)(NSURLResponse *, id))success - failure:(void (^)(NSURLResponse *, NSError *))failure + destinationPath:(NSString *)destinationPath + parameters:(NSDictionary *)parameters + progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgressBlock + success:(void (^)(NSURLResponse *response, id responseObject))success + failure:(void (^)(NSURLResponse *response, NSError * error))failure { - return [self setObjectWithMethod:@"PUT" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure]; + return [self setObjectWithMethod:@"PUT" file:path destinationPath:destinationPath parameters:parameters progress:uploadProgressBlock success:success failure:failure]; } - (NSURLSessionDataTask *)setObjectWithMethod:(NSString *)method file:(NSString *)filePath destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters - progress:(NSProgress * __nullable __autoreleasing * __nullable)progress + progress:(void (^)(NSProgress *downloadProgress))uploadProgressBlock success:(void (^)(NSURLResponse *, id))success failure:(void (^)(NSURLResponse *, NSError *))failure { NSParameterAssert(method); NSParameterAssert(filePath); NSParameterAssert(destinationPath); + + NSURL *fileURL = [NSURL fileURLWithPath:filePath]; - NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]; + NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:fileURL]; fileRequest.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; NSURLResponse *response = nil; @@ -279,7 +281,7 @@ - (NSURLSessionDataTask *)setObjectWithMethod:(NSString *)method __block NSURLSessionDataTask *dataTask = nil; - dataTask = [self uploadTaskWithRequest:request fromFile:filePath progress:progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { + dataTask = [self uploadTaskWithRequest:request fromFile:fileURL progress:uploadProgressBlock completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { if (error) { if (failure) { failure(response, error); @@ -297,8 +299,8 @@ - (NSURLSessionDataTask *)setObjectWithMethod:(NSString *)method } - (NSURLSessionDataTask *)deleteObjectWithPath:(NSString *)path - success:(void (^)(NSURLSessionDataTask *, id))success - failure:(void (^)(NSURLSessionDataTask *, NSError *))failure + success:(void (^)(NSURLSessionDataTask * task, id responseObject))success + failure:(void (^)(NSURLSessionDataTask * task, NSError * error))failure { NSParameterAssert(path); diff --git a/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h b/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h index 976289f..a3d5cdb 100644 --- a/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h +++ b/AFAmazonS3Manager/AFAmazonS3ResponseSerializer.h @@ -20,7 +20,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#import "AFURLResponseSerialization.h" #import /**