Changing AFRestClient +baseURL to a @property
This commit is contained in:
parent
76d9b2bfc8
commit
2b9a66d7a9
3 changed files with 15 additions and 23 deletions
|
|
@ -26,16 +26,15 @@
|
|||
#import "NSMutableURLRequest+AFNetworking.h"
|
||||
#import "NSString+AFNetworking.h"
|
||||
|
||||
@protocol AFRestClient <NSObject>
|
||||
+ (NSURL *)baseURL;
|
||||
@end
|
||||
|
||||
@interface AFRestClient : NSObject <AFRestClient> {
|
||||
@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;
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue