Created
April 24, 2014 18:54
-
-
Save patrick99e99/11265405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "HTTPClient.h" | |
#import "KSDeferred.h" | |
@interface HTTPClient () | |
@property (strong, nonatomic) NSURLSession *session; | |
@property (strong, nonatomic) NSOperationQueue *mainQueue; | |
@end | |
@implementation HTTPClient | |
@synthesize injector = _injector; | |
+(BSInitializer *)bsInitializer { | |
return [BSInitializer initializerWithClass:self | |
selector:@selector(initWithNSURLSession:mainQueue:) | |
argumentKeys:[NSURLSession class], [NSOperationQueue class], nil]; | |
} | |
-(instancetype)initWithNSURLSession:(NSURLSession *)session mainQueue:(NSOperationQueue*)mainQueue { | |
if (self = [self init]) { | |
self.session = session; | |
self.mainQueue = mainQueue; | |
} | |
return self; | |
} | |
-(KSPromise *)fetchUrl:(NSString *)urlString { | |
KSDeferred *deferred = [self deferred]; | |
__weak typeof(self) weakSelf = self; | |
[[self.session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
NSLog(@"================> http client: %@", weakSelf); | |
[weakSelf.mainQueue addOperationWithBlock:^{ | |
[weakSelf resolveOrReject:deferred | |
data:data | |
response:response | |
error:error | |
expectedStatus:200]; | |
}]; | |
}] resume]; | |
return deferred.promise; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment