From 11143036bd9ec7e1b25fdf050972299f9185dd6c Mon Sep 17 00:00:00 2001 From: Sam Dean Date: Tue, 24 Jun 2014 22:49:07 +0100 Subject: [PATCH 1/2] 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]; - }); } From 67c2d2ba3ff6264565588b49c538abf29502807e Mon Sep 17 00:00:00 2001 From: Sam Dean Date: Tue, 24 Jun 2014 22:50:13 +0100 Subject: [PATCH 2/2] Update the pod to version 2.0.0, also tweak README and CHANGELOG --- CHANGELOG.md | 4 ++++ Demo/Podfile | 2 +- README.md | 2 +- librato-iOS.podspec | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39d35c1..270bd98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### Version 2.0.0 + +Upgrade to AFNetworking ~> 2.0 (iOS 7 only) + ### Version 1.0.2 * Removed the `init` override in `LibratoMetric` to fix an issue where offline cached metrics could not be rehydrated diff --git a/Demo/Podfile b/Demo/Podfile index 15c32fe..60233ee 100644 --- a/Demo/Podfile +++ b/Demo/Podfile @@ -1,3 +1,3 @@ -platform :ios, '6.0' +platform :ios, '7.0' pod 'librato-iOS', :path => '../librato-iOS.podspec' diff --git a/README.md b/README.md index 3d017b6..a244f2f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ librato-iOS Metrics are automatically cached while the network is unavailable and saved if the app closes before they're submitted. Don't worry about submitting metrics, we make sure they don't go missing before they can be handed off to Librato's service. -Currently iOS versions 6 and 7 are supported and the wonderful [AFNetworking](https://github.com/AFNetworking/AFNetworking) is used to handle network duties. +Currently iOS version 7 and above is supported and the wonderful [AFNetworking 2](https://github.com/AFNetworking/AFNetworking) is used to handle network duties. # Quick Start diff --git a/librato-iOS.podspec b/librato-iOS.podspec index 0d14aa9..cafa794 100644 --- a/librato-iOS.podspec +++ b/librato-iOS.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "librato-iOS" - s.version = "1.0.3" + s.version = "2.0.0" s.summary = "Librato library for iOS" s.description = <<-DESC A simple, delightful wrapper for the Librato API with conveniences for common use cases