From 11143036bd9ec7e1b25fdf050972299f9185dd6c Mon Sep 17 00:00:00 2001 From: Sam Dean Date: Tue, 24 Jun 2014 22:49:07 +0100 Subject: [PATCH] Update to use AFNetworking 2.0 --- librato-iOS.podspec | 4 +- librato-iOS/Classes/LibratoClient.h | 2 +- librato-iOS/Classes/LibratoClient.m | 97 +++++++++++++---------------- 3 files changed, 48 insertions(+), 55 deletions(-) diff --git a/librato-iOS.podspec b/librato-iOS.podspec index 4bd386d..0d14aa9 100644 --- a/librato-iOS.podspec +++ b/librato-iOS.podspec @@ -9,7 +9,7 @@ Pod::Spec.new do |s| s.license = { :type => 'MIT', :file => 'LICENSE.md' } s.author = { "Adam Yanalunas" => "adam@yanalunas.com" } s.source = { :git => "https://github.com/amco/librato-iOS.git", :tag => "#{s.version}" } - s.platform = :ios, '6.0' + s.platform = :ios, '7.0' s.source_files = 'Librato-iOS/**/*.{h,m}' s.frameworks = 'QuartzCore', 'Foundation', 'SystemConfiguration', 'MobileCoreServices', 'UIKit' s.prefix_header_file = 'librato-iOS/librato-iOS-Prefix.pch' @@ -17,6 +17,6 @@ Pod::Spec.new do |s| s.exclude_files = 'Demo' s.requires_arc = true - s.dependency 'AFNetworking', '~> 1.0' + s.dependency 'AFNetworking', '~> 2.0' s.dependency 'Mantle', '~> 1.3' end diff --git a/librato-iOS/Classes/LibratoClient.h b/librato-iOS/Classes/LibratoClient.h index cbeb90e..cbc868c 100644 --- a/librato-iOS/Classes/LibratoClient.h +++ b/librato-iOS/Classes/LibratoClient.h @@ -15,7 +15,7 @@ typedef void (^ClientFailureBlock)(NSError *error, NSDictionary *JSON); @class LibratoConnection, LibratoQueue; -@interface LibratoClient : AFHTTPClient +@interface LibratoClient : AFHTTPSessionManager @property (nonatomic, copy) NSString *agentIdentifier; @property (nonatomic, copy) NSString *APIEndpoint; diff --git a/librato-iOS/Classes/LibratoClient.m b/librato-iOS/Classes/LibratoClient.m index 33ceeaf..754088d 100644 --- a/librato-iOS/Classes/LibratoClient.m +++ b/librato-iOS/Classes/LibratoClient.m @@ -31,16 +31,18 @@ - (instancetype)init return nil; } - [self setDefaultHeader:@"Accept" value:@"application/json"]; - self.parameterEncoding = AFJSONParameterEncoding; + self.responseSerializer = [[AFJSONResponseSerializer alloc] init]; self.online = NO; __weak __block LibratoClient *weakself = self; - [self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { + [self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { weakself.online = (status != AFNetworkReachabilityStatusNotReachable); }]; - [self addObserver:self forKeyPath:NSStringFromSelector(@selector(online)) options:NSKeyValueObservingOptionNew context:nil]; + [self.reachabilityManager addObserver:self + forKeyPath:NSStringFromSelector(@selector(isReachable)) + options:NSKeyValueObservingOptionNew + context:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleForegroundNotificaiton:) @@ -62,7 +64,8 @@ - (instancetype)init - (void)dealloc { - [self removeObserver:self forKeyPath:NSStringFromSelector(@selector(online))]; + [self.reachabilityManager removeObserver:self + forKeyPath:NSStringFromSelector(@selector(isReachable))]; [NSNotificationCenter.defaultCenter removeObserver:self]; } @@ -70,9 +73,9 @@ - (void)dealloc #pragma mark - KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if ([object isKindOfClass:LibratoClient.class]) + if ([object isKindOfClass:self.reachabilityManager.class]) { - if ([keyPath isEqualToString:NSStringFromSelector(@selector(online))]) + if ([keyPath isEqualToString:NSStringFromSelector(@selector(isReachable))]) { if ([object isOnline]) { @@ -168,23 +171,19 @@ - (void)getMetric:(NSString *)name options:(NSDictionary *)options { query[@"resolution"] = query[@"resolution"] ?: @(1); } - - NSURLRequest *request = [self requestWithMethod:@"GET" path:[NSString stringWithFormat:@"metrics/%@", name] parameters:query]; - - AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { - if (success) - { - success(JSON, response.statusCode); - } - } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { - if (failure) { - failure(error, JSON); - } - }]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [op start]; - }); + + NSString *path = [NSString stringWithFormat:@"metrics/%@", name]; + [self GET:path parameters:query + success:^(NSURLSessionDataTask *task, id JSON) { + if (success) + { + success(JSON, ((NSHTTPURLResponse *)task.response).statusCode); + } + } failure:^(NSURLSessionDataTask *task, NSError *error) { + if (failure) { + failure(error, nil); + } + }]; } @@ -214,8 +213,8 @@ - (void)getMeasurements:(NSString *)named options:(NSDictionary *)options - (void)setUser:(NSString *)user andToken:(NSString *)token { - [self clearAuthorizationHeader]; - [self setAuthorizationHeaderWithUsername:user password:token]; + [self.requestSerializer clearAuthorizationHeader]; + [self.requestSerializer setAuthorizationHeaderFieldWithUsername:user password:token]; } @@ -228,23 +227,20 @@ - (void)sendPayload:(NSDictionary *)payload - (void)sendPayload:(NSDictionary *)payload withSuccess:(ClientSuccessBlock)success orFailure:(ClientFailureBlock)failure { [self setUser:email andToken:APIKey]; - NSURLRequest *request = [self requestWithMethod:@"POST" path:@"metrics" parameters:payload]; - // TODO: Move the queue into a local var that can be resotred if the submit fails + [self.queue clear]; - AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { - if (success) - { - success(JSON, response.statusCode); - } - } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { - if (failure) { - failure(error, JSON); - } - }]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [op start]; - }); + + [self POST:@"metrics" parameters:payload + success:^(NSURLSessionDataTask *task, id JSON) { + if (success) + { + success(JSON, ((NSHTTPURLResponse *)task.response).statusCode); + } + } failure:^(NSURLSessionDataTask *task, NSError *error) { + if (failure) { + failure(error, nil); + } + }]; } @@ -309,7 +305,6 @@ - (void)submit:(id)metrics - (void)updateMetricsNamed:(NSString *)name options:(NSDictionary *)options { NSMutableDictionary *query = options.mutableCopy; - NSURLRequest *request = [self requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"metrics/%@", name] parameters:options]; __block ClientSuccessBlock success; if (query[@"success"]) @@ -324,21 +319,19 @@ - (void)updateMetricsNamed:(NSString *)name options:(NSDictionary *)options failure = [query[@"failure"] copy]; [query removeObjectForKey:@"failure"]; } - - AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { + + NSString *path = [NSString stringWithFormat:@"metrics/%@", name]; + [self PUT:path parameters:options + success:^(NSURLSessionDataTask *task, id JSON) { if (success) { - success(JSON, response.statusCode); + success(JSON, ((NSHTTPURLResponse *)task.response).statusCode); } - } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { + } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { - failure(error, JSON); + failure(error, nil); } }]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [op start]; - }); }