From 2b9a66d7a95f4b48cb1132bbd574427fe3515e64 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Fri, 16 Sep 2011 10:02:51 -0500 Subject: [PATCH] Changing AFRestClient +baseURL to a @property --- AFNetworking/AFRestClient.h | 11 +++++------ AFNetworking/AFRestClient.m | 17 +++++++---------- Example/Classes/AFGowallaAPIClient.m | 10 +++------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/AFNetworking/AFRestClient.h b/AFNetworking/AFRestClient.h index 7470810..28f8f21 100644 --- a/AFNetworking/AFRestClient.h +++ b/AFNetworking/AFRestClient.h @@ -26,16 +26,15 @@ #import "NSMutableURLRequest+AFNetworking.h" #import "NSString+AFNetworking.h" -@protocol AFRestClient -+ (NSURL *)baseURL; -@end - -@interface AFRestClient : NSObject { -@protected +@interface AFRestClient : NSObject { +@private + NSURL *_baseURL; NSMutableDictionary *_defaultHeaders; NSOperationQueue *_operationQueue; } +- (id)initWithBaseURL:(NSURL *)url; + - (NSString *)defaultValueForHeader:(NSString *)header; - (void)setDefaultHeader:(NSString *)header value:(NSString *)value; - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password; diff --git a/AFNetworking/AFRestClient.m b/AFNetworking/AFRestClient.m index 7b94409..b382e0c 100644 --- a/AFNetworking/AFRestClient.m +++ b/AFNetworking/AFRestClient.m @@ -28,20 +28,24 @@ static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding; @interface AFRestClient () +@property (readwrite, nonatomic, retain) NSURL *baseURL; @property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders; @property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue; @end @implementation AFRestClient +@synthesize baseURL = _baseURL; @synthesize defaultHeaders = _defaultHeaders; @synthesize operationQueue = _operationQueue; -- (id)init { +- (id)initWithBaseURL:(NSURL *)url { self = [super init]; if (!self) { return nil; } + self.baseURL = url; + self.operationQueue = [[[NSOperationQueue alloc] init] autorelease]; [self.operationQueue setMaxConcurrentOperationCount:2]; @@ -64,19 +68,12 @@ static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding } - (void)dealloc { + [_baseURL release]; [_defaultHeaders release]; [_operationQueue release]; [super dealloc]; } -+ (NSURL *)baseURL { - if ([self class] == [AFRestClient class]) { - [NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; - } - - return nil; -} - - (NSString *)defaultValueForHeader:(NSString *)header { return [self.defaultHeaders valueForKey:header]; } @@ -104,7 +101,7 @@ static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters { NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders]; - NSURL *url = [NSURL URLWithString:path relativeToURL:[[self class] baseURL]]; + NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL]; if (parameters) { NSMutableArray *mutableParameterComponents = [NSMutableArray array]; diff --git a/Example/Classes/AFGowallaAPIClient.m b/Example/Classes/AFGowallaAPIClient.m index d8ee311..21cab53 100644 --- a/Example/Classes/AFGowallaAPIClient.m +++ b/Example/Classes/AFGowallaAPIClient.m @@ -34,14 +34,14 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/"; + (id)sharedClient { static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ - _sharedClient = [[self alloc] init]; + _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAFGowallaBaseURLString]]; }); return _sharedClient; } -- (id)init { - self = [super init]; +- (id)initWithBaseURL:(NSURL *)url { + self = [super initWithBaseURL:url]; if (!self) { return nil; } @@ -58,8 +58,4 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/"; return self; } -+ (NSURL *)baseURL { - return [NSURL URLWithString:kAFGowallaBaseURLString]; -} - @end