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:
parent
fb517edf37
commit
783353bfe9
|
@ -40,20 +40,30 @@ typedef NSURLRequest * _Nullable (^SDWebImageDownloaderRequestModifierBlock)(NSU
|
|||
@end
|
||||
|
||||
/**
|
||||
A convenient request modifier to provide the HTTP request including HTTP method, headers and body.
|
||||
*/
|
||||
@interface SDWebImageDownloaderHTTPRequestModifier : NSObject <SDWebImageDownloaderRequestModifier>
|
||||
A convenient request modifier to provide the HTTP request including HTTP Method, Headers and Body.
|
||||
*/
|
||||
@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.
|
||||
/// @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;
|
||||
|
||||
/// 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 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
|
||||
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderRequestModifier` instead
|
||||
/// @param body HTTP Body.
|
||||
/// @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;
|
||||
|
||||
@end
|
||||
|
|
|
@ -38,39 +38,34 @@
|
|||
|
||||
@end
|
||||
|
||||
@interface SDWebImageDownloaderHTTPRequestModifier ()
|
||||
@implementation SDWebImageDownloaderRequestModifier (HTTPConveniences)
|
||||
|
||||
@property (nonatomic, copy, nullable) NSString *method;
|
||||
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers;
|
||||
@property (nonatomic, copy, nullable) NSData *body;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDWebImageDownloaderHTTPRequestModifier
|
||||
- (instancetype)initWithMethod:(NSString *)method {
|
||||
return [self initWithMethod:method headers:nil body:nil];
|
||||
}
|
||||
|
||||
- (instancetype)initWithHeaders:(NSDictionary<NSString *,NSString *> *)headers {
|
||||
return [self initWithMethod:nil headers:headers body:nil];
|
||||
}
|
||||
|
||||
- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary<NSString *,NSString *> *)headers body:(NSData *)body {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_method = [method copy];
|
||||
_headers = [headers copy];
|
||||
_body = [body copy];
|
||||
}
|
||||
return self;
|
||||
- (instancetype)initWithBody:(NSData *)body {
|
||||
return [self initWithMethod:nil headers:nil body:body];
|
||||
}
|
||||
|
||||
- (NSURLRequest *)modifiedRequestWithRequest:(NSURLRequest *)request {
|
||||
- (instancetype)initWithMethod:(NSString *)method headers:(NSDictionary<NSString *,NSString *> *)headers body:(NSData *)body {
|
||||
method = [method copy];
|
||||
headers = [headers copy];
|
||||
body = [body copy];
|
||||
return [self initWithBlock:^NSURLRequest * _Nullable(NSURLRequest * _Nonnull request) {
|
||||
NSMutableURLRequest *mutableRequest = [request mutableCopy];
|
||||
mutableRequest.HTTPMethod = self.method;
|
||||
mutableRequest.HTTPBody = self.body;
|
||||
for (NSString *header in self.headers) {
|
||||
NSString *value = self.headers[header];
|
||||
mutableRequest.HTTPMethod = method;
|
||||
mutableRequest.HTTPBody = body;
|
||||
for (NSString *header in headers) {
|
||||
NSString *value = headers[header];
|
||||
[mutableRequest setValue:value forHTTPHeaderField:header];
|
||||
}
|
||||
return [mutableRequest copy];
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -40,20 +40,30 @@ typedef NSURLResponse * _Nullable (^SDWebImageDownloaderResponseModifierBlock)(N
|
|||
@end
|
||||
|
||||
/**
|
||||
A convenient response modifier to provide the HTTP response including HTTP method, headers and body.
|
||||
*/
|
||||
@interface SDWebImageDownloaderHTTPResponseModifier : NSObject <SDWebImageDownloaderResponseModifier>
|
||||
A convenient response modifier to provide the HTTP response including HTTP Status Code, Version and Headers.
|
||||
*/
|
||||
@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.
|
||||
/// @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;
|
||||
|
||||
/// Create the response modifier with HTTP Version, Status Code and Headers
|
||||
/// @param version HTTP Version, nil means "HTTP/1.1"
|
||||
/// @param statusCode HTTP Status Code
|
||||
/// Create the response modifier with HTTP Status Code, Version and Headers.
|
||||
/// @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.
|
||||
/// @note This is for convenience, if you need code to control the logic, use `SDWebImageDownloaderResponseModifier` instead
|
||||
- (nonnull instancetype)initWithVersion:(nullable NSString *)version statusCode:(NSInteger)statusCode headers:(nullable NSDictionary<NSString *, NSString *> *)headers;
|
||||
/// @note This is for convenience, if you need code to control the logic, use block API instead.
|
||||
- (nonnull instancetype)initWithStatusCode:(NSInteger)statusCode version:(nullable NSString *)version headers:(nullable NSDictionary<NSString *, NSString *> *)headers;
|
||||
|
||||
@end
|
||||
|
|
|
@ -39,41 +39,35 @@
|
|||
|
||||
@end
|
||||
|
||||
@interface SDWebImageDownloaderHTTPResponseModifier ()
|
||||
@implementation SDWebImageDownloaderResponseModifier (HTTPConveniences)
|
||||
|
||||
@property (nonatomic, copy, nullable) NSString *version;
|
||||
@property (nonatomic) NSInteger statusCode;
|
||||
@property (nonatomic, copy, nullable) NSDictionary<NSString *,NSString *> *headers;
|
||||
- (instancetype)initWithStatusCode:(NSInteger)statusCode {
|
||||
return [self initWithStatusCode:statusCode version:nil headers:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SDWebImageDownloaderHTTPResponseModifier
|
||||
- (instancetype)initWithVersion:(NSString *)version {
|
||||
return [self initWithStatusCode:200 version:version headers:nil];
|
||||
}
|
||||
|
||||
- (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 {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_version = [version copy];
|
||||
_statusCode = statusCode;
|
||||
_headers = [headers copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSURLResponse *)modifiedResponseWithResponse:(NSURLResponse *)response {
|
||||
- (instancetype)initWithStatusCode:(NSInteger)statusCode version:(NSString *)version headers:(NSDictionary<NSString *,NSString *> *)headers {
|
||||
version = version ? [version copy] : @"HTTP/1.1";
|
||||
headers = [headers copy];
|
||||
return [self initWithBlock:^NSURLResponse * _Nullable(NSURLResponse * _Nonnull response) {
|
||||
if (![response isKindOfClass:NSHTTPURLResponse.class]) {
|
||||
return response;
|
||||
}
|
||||
NSMutableDictionary *mutableHeaders = [((NSHTTPURLResponse *)response).allHeaderFields mutableCopy];
|
||||
for (NSString *header in self.headers) {
|
||||
NSString *value = self.headers[header];
|
||||
for (NSString *header in headers) {
|
||||
NSString *value = headers[header];
|
||||
mutableHeaders[header] = value;
|
||||
}
|
||||
NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:self.statusCode HTTPVersion:self.version ?: @"HTTP/1.1" headerFields:[mutableHeaders copy]];
|
||||
NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:response.URL statusCode:statusCode HTTPVersion:version headerFields:[mutableHeaders copy]];
|
||||
return httpResponse;
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue