Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created April 24, 2014 18:54
Show Gist options
  • Save patrick99e99/11265405 to your computer and use it in GitHub Desktop.
Save patrick99e99/11265405 to your computer and use it in GitHub Desktop.
#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