Update the convienient API using the Category, actually most use case is HTTP request, data and file URL are rare

This commit is contained in:
DreamPiggy 2020-04-24 18:54:16 +08:00
parent fb517edf37
commit 783353bfe9
4 changed files with 80 additions and 71 deletions

View File

@ -40,20 +40,30 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU
@end @end
/** /**
A convenient request modifier to provide the HTTP request including HTTP method, headers and body. A convenient request modifier to provide the HTTP request including HTTP Method, Headers and Body.
*/ */
@interface SDWebImageDownloaderHTTPRequestModifier : NSObject <SDWebImageDownloaderRequestModifier> @interface SDWebImageDownloaderRequestModifier (HTTPConveniences)
/// Create the request modifier with HTTP Headers /// Create the request modifier with HTTP Method.
/// @param method HTTP Method, nil means to GET.
/// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithMethod:(nullable NSString *)method;
/// Create the request modifier with HTTP Headers.
/// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request.
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead /// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithHeaders:(nullable NSDictionary<NSString *, NSString *> *)headers; - (nonnull instancetype)initWithHeaders:(nullable NSDictionary<NSString *, NSString *> *)headers;
/// Create the request modifier with HTTP Method, Headers and Body /// Create the request modifier with HTTP Body.
/// @param body HTTP Body.
/// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithBody:(nullable NSData *)body;
/// Create the request modifier with HTTP Method, Headers and Body.
/// @param method HTTP Method, nil means to GET. /// @param method HTTP Method, nil means to GET.
/// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original request.
/// @param body HTTP Body /// @param body HTTP Body.
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead /// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary<NSString *, NSString *> *)headers body:(nullable NSData *)body; - (nonnull instancetype)initWithMethod:(nullable NSString *)method headers:(nullable NSDictionary<NSString *, NSString *> *)headers body:(nullable NSData *)body;
@end @end

View File

@ -38,39 +38,34 @@
@end @end
@interface SDWebImageDownloaderHTTPRequestModifier () @implementation SDWebImageDownloaderRequestModifier (HTTPConveniences)
@property (nonatomic, copy, nullable) NSString *method; - (instancetype)initWithMethod:(NSString *)method {
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers; return [self initWithMethod:method headers:nil body:nil];
@property (nonatomic, copy, nullable) NSData *body; }
@end
@implementation SDWebImageDownloaderHTTPRequestModifier
- (instancetype)initWithHeaders:(NSDictionary<NSString *,NSString *> *)headers { - (instancetype)initWithHeaders:(NSDictionary<NSString *,NSString *> *)headers {
return [self initWithMethod:nil headers:headers body:nil]; return [self initWithMethod:nil headers:headers body:nil];
} }
- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary<NSString *,NSString *> *)headers body:(NSData *)body { - (instancetype)initWithBody:(NSData *)body {
self = [super init]; return [self initWithMethod:nil headers:nil body:body];
if (self) {
_method = [method copy];
_headers = [headers copy];
_body = [body copy];
}
return self;
} }
- (NSURLRequest *)modifiedRequestWithRequest:(NSURLRequest *)request { - (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary<NSString *,NSString *> *)headers body:(NSData *)body {
NSMutableURLRequest *mutableRequest = [request mutableCopy]; method = [method copy];
mutableRequest.HTTPMethod = self.method; headers = [headers copy];
mutableRequest.HTTPBody = self.body; body = [body copy];
for (NSString *header in self.headers) { return [self initWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) {
NSString *value = self.headers[header]; NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest setValue:value forHTTPHeaderField:header]; mutableRequest.HTTPMethod = method;
} mutableRequest.HTTPBody = body;
return [mutableRequest copy]; for (NSString *header in headers) {
NSString *value = headers[header];
[mutableRequest setValue:value forHTTPHeaderField:header];
}
return [mutableRequest copy];
}];
} }
@end @end

View File

@ -40,20 +40,30 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N
@end @end
/** /**
A convenient response modifier to provide the HTTP response including HTTP method, headers and body. A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers.
*/ */
@interface SDWebImageDownloaderHTTPResponseModifier : NSObject <SDWebImageDownloaderResponseModifier> @interface SDWebImageDownloaderResponseModifier (HTTPConveniences)
/// Create the response modifier with HTTP Status code.
/// @param statusCode HTTP Status Code.
/// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode;
/// Create the response modifier with HTTP Version. Status code defaults to 200.
/// @param version HTTP Version, nil means "HTTP/1.1".
/// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithVersion:(nullable NSString *)version;
/// Create the response modifier with HTTP Headers. Status code defaults to 200. /// Create the response modifier with HTTP Headers. Status code defaults to 200.
/// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response.
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderResponseModifier` instead /// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithHeaders:(nullable NSDictionary<NSString *, NSString *> *)headers; - (nonnull instancetype)initWithHeaders:(nullable NSDictionary<NSString *, NSString *> *)headers;
/// Create the response modifier with HTTP Version, Status Code and Headers /// Create the response modifier with HTTP Status Code, Version and Headers.
/// @param version HTTP Version, nil means "HTTP/1.1" /// @param statusCode HTTP Status Code.
/// @param statusCode HTTP Status Code /// @param version HTTP Version, nil means "HTTP/1.1".
/// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response. /// @param headers HTTP Headers. Case insensitive according to HTTP/1.1(HTTP/2) standard. The headers will overide the same fileds from original response.
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderResponseModifier` instead /// @note This is for convenience, if you need code to control the logic, use block API instead.
- (nonnull instancetype)initWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary<NSString *, NSString *> *)headers; - (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode version:(nullable NSString *)version headers:(nullable NSDictionary<NSString *, NSString *> *)headers;
@end @end

View File

@ -39,41 +39,35 @@
@end @end
@interface SDWebImageDownloaderHTTPResponseModifier () @implementation SDWebImageDownloaderResponseModifier (HTTPConveniences)
@property (nonatomic, copy, nullable) NSString *version; - (instancetype)initWithStatusCode:(NSInteger)statusCode {
@property (nonatomic) NSInteger statusCode; return [self initWithStatusCode:statusCode version:nil headers:nil];
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers; }
@end - (instancetype)initWithVersion:(NSString *)version {
return [self initWithStatusCode:200 version:version headers:nil];
@implementation SDWebImageDownloaderHTTPResponseModifier }
- (instancetype)initWithHeaders:(NSDictionary<NSString *,NSString *> *)headers { - (instancetype)initWithHeaders:(NSDictionary<NSString *,NSString *> *)headers {
return [self initWithVersion:nil statusCode:200 headers:headers]; return [self initWithStatusCode:200 version:nil headers:headers];
} }
- (instancetype)initWithVersion:(NSString *)version statusCode:(NSInteger)statusCode headers:(NSDictionary<NSString *,NSString *> *)headers { - (instancetype)initWithStatusCode:(NSInteger)statusCode version:(NSString *)version headers:(NSDictionary<NSString *,NSString *> *)headers {
self = [super init]; version = version ? [version copy] : @"HTTP/1.1";
if (self) { headers = [headers copy];
_version = [version copy]; return [self initWithBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) {
_statusCode = statusCode; if (![response isKindOfClass:NSHTTPURLResponse.class]) {
_headers = [headers copy]; return response;
} }
return self; NSMutableDictionary *mutableHeaders = [((NSHTTPURLResponse *)response).allHeaderFields mutableCopy];
} for (NSString *header in headers) {
NSString *value = headers[header];
- (NSURLResponse *)modifiedResponseWithResponse:(NSURLResponse *)response { mutableHeaders[header] = value;
if (![response isKindOfClass:NSHTTPURLResponse.class]) { }
return response; NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:statusCode HTTPVersion:version headerFields:[mutableHeaders copy]];
} return httpResponse;
NSMutableDictionary *mutableHeaders = [((NSHTTPURLResponse *)response).allHeaderFields mutableCopy]; }];
for (NSString *header in self.headers) {
NSString *value = self.headers[header];
mutableHeaders[header] = value;
}
NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:self.statusCode HTTPVersion:self.version ?: @"HTTP/1.1" headerFields:[mutableHeaders copy]];
return httpResponse;
} }
@end @end