Permalink
Browse files

Remove the dependencies on ASIHTTPRequest and YAJL JSON.

  • Loading branch information...
1 parent 1413c0a commit 8ab22638aab4940b3886e1ef1c7c3b067757f4ea @lukeredpath committed Apr 1, 2012
View
@@ -3,8 +3,22 @@ build/*
*.pbxuser
*.mode1v3
*.mode*
+*.perspectivev*
# osx noise
.DS_Store
profile
-*.perspectivev3
+
+Constants.h
+*.xcodeproj/xcuserdata
+*.xcodeproj/*.xcworkspace/xcuserdata
+*.xcworkspace/xcuserdata
+
+log
+Scripts/*.pid
+Docs
+Pods
+xcodebuild
+dist
+.bundle
+xcodebuild.log
View
@@ -7,12 +7,11 @@
//
#import <UIKit/UIKit.h>
-#import "ASIHTTPRequestDelegate.h"
#import "LROAuth2ClientDelegate.h"
@class LROAuth2AccessToken;
-@interface LROAuth2Client : NSObject <ASIHTTPRequestDelegate> {
+@interface LROAuth2Client : NSObject {
NSString *clientID;
NSString *clientSecret;
NSURL *redirectURL;
@@ -25,7 +24,7 @@
BOOL debug;
@private
- BOOL isVerifying;
+ BOOL isVerifying;
}
@property (nonatomic, copy) NSString *clientID;
@property (nonatomic, copy) NSString *clientSecret;
View
@@ -8,14 +8,16 @@
#import <YAJLIOS/NSObject+YAJL.h>
#import "LROAuth2Client.h"
-#import "ASIHTTPRequest.h"
#import "NSURL+QueryInspector.h"
#import "LROAuth2AccessToken.h"
+#import "LRURLRequestOperation.h"
#import "NSDictionary+QueryString.h"
#pragma mark -
-@implementation LROAuth2Client
+@implementation LROAuth2Client {
+ NSOperationQueue *_networkQueue;
+}
@synthesize clientID;
@synthesize clientSecret;
@@ -37,17 +39,14 @@ - (id)initWithClientID:(NSString *)_clientID
redirectURL = [url copy];
requests = [[NSMutableArray alloc] init];
debug = NO;
+ _networkQueue = [[NSOperationQueue alloc] init];
}
return self;
}
- (void)dealloc;
{
- for (ASIHTTPRequest *request in requests) {
- [request setDelegate:nil];
- [request cancel];
- }
- [requests release];
+ [_networkQueue cancelAllOperations];
[accessToken release];
[clientID release];
[clientSecret release];
@@ -94,13 +93,18 @@ - (void)verifyAuthorizationWithAccessCode:(NSString *)accessCode;
[params setValue:clientSecret forKey:@"client_secret"];
[params setValue:accessCode forKey:@"code"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.tokenURL];
- [request setRequestMethod:@"POST"];
- [request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
- [request appendPostData:[[params stringWithFormEncodedComponents] dataUsingEncoding:NSUTF8StringEncoding]];
- [request setDelegate:self];
- [requests addObject:request];
- [request startAsynchronous];
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.tokenURL];
+ [request setHTTPMethod:@"POST"];
+ [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+ [request setHTTPBody:[[params stringWithFormEncodedComponents] dataUsingEncoding:NSUTF8StringEncoding]];
+
+ __block LRURLRequestOperation *operation = [[LRURLRequestOperation alloc] initWithURLRequest:request];
+
+ [operation setCompletionBlock:^{
+ [self handleCompletionForAuthorizationRequestOperation:operation];
+ }];
+
+ [_networkQueue addOperation:operation];
}
}
@@ -115,71 +119,51 @@ - (void)refreshAccessToken:(LROAuth2AccessToken *)_accessToken;
[params setValue:clientSecret forKey:@"client_secret"];
[params setValue:_accessToken.refreshToken forKey:@"refresh_token"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.tokenURL];
- [request setRequestMethod:@"POST"];
- [request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
- [request appendPostData:[[params stringWithFormEncodedComponents] dataUsingEncoding:NSUTF8StringEncoding]];
- [request setDelegate:self];
- [requests addObject:request];
- [request startAsynchronous];
-}
-
-#pragma mark -
-#pragma mark ASIHTTPRequestDelegate methods
-
-- (void)requestStarted:(ASIHTTPRequest *)request
-{
- if (self.debug) {
- NSLog(@"[oauth] starting verification request");
- }
-}
-
-- (void)requestFinished:(ASIHTTPRequest *)request
-{
- if (self.debug) {
- NSLog(@"[oauth] finished verification request, %@ (%d)", [request responseString], [request responseStatusCode]);
- }
- isVerifying = NO;
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:self.tokenURL];
+ [request setHTTPMethod:@"POST"];
+ [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+ [request setHTTPBody:[[params stringWithFormEncodedComponents] dataUsingEncoding:NSUTF8StringEncoding]];
- [requests removeObject:request];
-}
-
-- (void)requestFailed:(ASIHTTPRequest *)request
-{
- if (self.debug) {
- NSLog(@"[oauth] request failed with code %d, %@", [request responseStatusCode], [request responseString]);
- }
+ __block LRURLRequestOperation *operation = [[LRURLRequestOperation alloc] initWithURLRequest:request];
+
+ [operation setCompletionBlock:^{
+ [self handleCompletionForAuthorizationRequestOperation:operation];
+ }];
+
+ [_networkQueue addOperation:operation];
}
-- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)rawData
+- (void)handleCompletionForAuthorizationRequestOperation:(LRURLRequestOperation *)operation
{
- NSData* data = rawData;
- if( [request isResponseCompressed]) {
- data = [ASIHTTPRequest uncompressZippedData:rawData];
- }
-
- NSError *parseError = nil;
- NSDictionary *authorizationData = [data yajl_JSON:&parseError];
+ NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.URLResponse;
- if (parseError) {
- // try and decode the response body as a query string instead
- NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- authorizationData = [NSDictionary dictionaryWithFormEncodedString:responseString];
- [responseString release];
- if ([authorizationData valueForKey:@"access_token"] == nil) {
- // TODO handle complete parsing failure
+ if (response.statusCode == 200) {
+ NSError *parserError;
+ NSDictionary *authData = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:&parserError];
+
+ if (authData == nil) {
+ // try and decode the response body as a query string instead
+ NSString *responseString = [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding];
+ authData = [NSDictionary dictionaryWithFormEncodedString:responseString];
+ }
+ if ([authData objectForKey:@"access_token"] == nil) {
NSAssert(NO, @"Unhandled parsing failure");
}
- }
- if (accessToken == nil) {
- accessToken = [[LROAuth2AccessToken alloc] initWithAuthorizationResponse:authorizationData];
- if ([self.delegate respondsToSelector:@selector(oauthClientDidReceiveAccessToken:)]) {
- [self.delegate oauthClientDidReceiveAccessToken:self];
- }
- } else {
- [accessToken refreshFromAuthorizationResponse:authorizationData];
- if ([self.delegate respondsToSelector:@selector(oauthClientDidRefreshAccessToken:)]) {
- [self.delegate oauthClientDidRefreshAccessToken:self];
+ if (accessToken == nil) {
+ accessToken = [[LROAuth2AccessToken alloc] initWithAuthorizationResponse:authData];
+ if ([self.delegate respondsToSelector:@selector(oauthClientDidReceiveAccessToken:)]) {
+ [self.delegate oauthClientDidReceiveAccessToken:self];
+ }
+ } else {
+ [accessToken refreshFromAuthorizationResponse:authData];
+ if ([self.delegate respondsToSelector:@selector(oauthClientDidRefreshAccessToken:)]) {
+ [self.delegate oauthClientDidRefreshAccessToken:self];
+ }
+ }
+ }
+ else {
+ if (operation.connectionError) {
+ NSLog(@"Connection error: %@", operation.connectionError);
}
}
}
@@ -0,0 +1,36 @@
+//
+// LRURLConnectionOperation.h
+// LRResty
+//
+// Created by Luke Redpath on 04/10/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface LRURLRequestOperation : NSOperation {
+ BOOL _isExecuting;
+ BOOL _isFinished;
+ NSURLRequest *URLRequest;
+ NSURLResponse *URLResponse;
+ NSURLConnection *URLConnection;
+ NSError *connectionError;
+ NSMutableData *responseData;
+}
+@property (nonatomic, strong) NSURLRequest *URLRequest;
+@property (nonatomic, strong, readonly) NSURLResponse *URLResponse;
+@property (nonatomic, strong, readonly) NSError *connectionError;
+@property (nonatomic, strong, readonly) NSData *responseData;
+
+- (id)initWithURLRequest:(NSURLRequest *)request;
+- (void)finish;
+- (void)cancelImmediately;
+@end
+
+@interface LRURLRequestOperation (NSURLConnectionDelegate)
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)theResponse;
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
+@end
Oops, something went wrong.

0 comments on commit 8ab2263

Please sign in to comment.