Skip to content

Commit

Permalink
Update to use AFNetworking 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nap-sam-dean committed Jun 24, 2014
1 parent 2811477 commit 1114303
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 55 deletions.
4 changes: 2 additions & 2 deletions librato-iOS.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 1114303

Please sign in to comment.