iOS: remoteResource support headers setting
This commit is contained in:
parent
2c52403d5f
commit
fd1e626bf2
@ -21,4 +21,5 @@
|
||||
#import "DoricResource.h"
|
||||
|
||||
@interface DoricRemoteResource : DoricResource
|
||||
- (void)setHeaderWithKey:(NSString *)key withValue:(NSString *)value;
|
||||
@end
|
||||
|
@ -19,15 +19,35 @@
|
||||
|
||||
#import "DoricRemoteResource.h"
|
||||
|
||||
@interface DoricRemoteResource ()
|
||||
@property(nonatomic, strong) NSMutableDictionary *headers;
|
||||
@end
|
||||
|
||||
@implementation DoricRemoteResource
|
||||
- (void)setHeaderWithKey:(NSString *)key withValue:(NSString *)value {
|
||||
if (!self.headers) {
|
||||
self.headers = [NSMutableDictionary new];
|
||||
}
|
||||
self.headers[key] = value;
|
||||
}
|
||||
|
||||
- (DoricAsyncResult <NSData *> *)fetchRaw {
|
||||
DoricAsyncResult *result = [DoricAsyncResult new];
|
||||
NSURL *url = [NSURL URLWithString:self.identifier];
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
NSData *urlData = [NSData dataWithContentsOfURL:url];
|
||||
[result setupResult:urlData];
|
||||
});
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.identifier]];
|
||||
if (self.headers) {
|
||||
[self.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
|
||||
[request setValue:obj forHTTPHeaderField:key];
|
||||
}];
|
||||
}
|
||||
[[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]
|
||||
dataTaskWithRequest:request
|
||||
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
||||
if (!error) {
|
||||
[result setupResult:data];
|
||||
} else {
|
||||
[result setupError:(id) error.description];
|
||||
}
|
||||
}] resume];
|
||||
return result;
|
||||
}
|
||||
@end
|
||||
|
@ -20,6 +20,7 @@
|
||||
#import "DoricResourceManager.h"
|
||||
#import "DoricContext.h"
|
||||
#import "DoricArrayBufferResource.h"
|
||||
#import "DoricRemoteResource.h"
|
||||
|
||||
@interface DoricResourceManager ()
|
||||
@property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders;
|
||||
@ -60,6 +61,14 @@ - (__kindof DoricResource *)load:(NSDictionary *)resource
|
||||
doricResource = [loader load:identifier withContext:context];
|
||||
if ([doricResource isKindOfClass:DoricArrayBufferResource.class]) {
|
||||
((DoricArrayBufferResource *) doricResource).data = resource[@"data"];
|
||||
} else if ([doricResource isKindOfClass:DoricRemoteResource.class]) {
|
||||
NSDictionary *headers = resource[@"headers"];
|
||||
if (headers) {
|
||||
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
|
||||
[(DoricRemoteResource *) doricResource setHeaderWithKey:key
|
||||
withValue:value];
|
||||
}];
|
||||
}
|
||||
}
|
||||
[context.cachedResources setObject:doricResource forKey:resId];
|
||||
}
|
||||
|
Reference in New Issue
Block a user