Skip to content

Commit

Permalink
Merge pull request #40 from net-a-porter-mobile/master
Browse files Browse the repository at this point in the history
Upgrade to AFNetworking 2.0
  • Loading branch information
adamyanalunas committed Aug 22, 2014
2 parents 2811477 + 67c2d2b commit e55a9a0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Demo/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
platform :ios, '6.0'
platform :ios, '7.0'

pod 'librato-iOS', :path => '../librato-iOS.podspec'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions librato-iOS.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,14 +9,14 @@ Pod::Spec.new do |s|
s.license = { :type => 'MIT', :file => 'LICENSE.md' }
s.author = { "Adam Yanalunas" => "[email protected]" }
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'
s.resources = 'Librato-iOS/Librato-Localizable.strings'
s.exclude_files = 'Demo'
s.requires_arc = true

s.dependency 'AFNetworking', '~> 1.0'
s.dependency 'AFNetworking', '~> 2.0'
s.dependency 'Mantle', '~> 1.3'
end
2 changes: 1 addition & 1 deletion librato-iOS/Classes/LibratoClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
97 changes: 45 additions & 52 deletions librato-iOS/Classes/LibratoClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand All @@ -62,17 +64,18 @@ - (instancetype)init

- (void)dealloc
{
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(online))];
[self.reachabilityManager removeObserver:self
forKeyPath:NSStringFromSelector(@selector(isReachable))];
[NSNotificationCenter.defaultCenter removeObserver:self];
}


#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])
{
Expand Down Expand Up @@ -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);
}
}];
}


Expand Down Expand Up @@ -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];
}


Expand All @@ -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);
}
}];
}


Expand Down Expand Up @@ -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"])
Expand All @@ -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];
});
}


Expand Down

0 comments on commit e55a9a0

Please sign in to comment.