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 "NSMutableURLRequest+AFNetworking.h"
|
||||||
#import "NSString+AFNetworking.h"
|
#import "NSString+AFNetworking.h"
|
||||||
|
|
||||||
@protocol AFRestClient <NSObject>
|
@interface AFRestClient : NSObject {
|
||||||
+ (NSURL *)baseURL;
|
@private
|
||||||
@end
|
NSURL *_baseURL;
|
||||||
|
|
||||||
@interface AFRestClient : NSObject <AFRestClient> {
|
|
||||||
@protected
|
|
||||||
NSMutableDictionary *_defaultHeaders;
|
NSMutableDictionary *_defaultHeaders;
|
||||||
NSOperationQueue *_operationQueue;
|
NSOperationQueue *_operationQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (id)initWithBaseURL:(NSURL *)url;
|
||||||
|
|
||||||
- (NSString *)defaultValueForHeader:(NSString *)header;
|
- (NSString *)defaultValueForHeader:(NSString *)header;
|
||||||
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
|
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
|
||||||
- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password;
|
- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password;
|
||||||
|
|
|
||||||
|
|
@ -28,20 +28,24 @@
|
||||||
static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding;
|
static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding;
|
||||||
|
|
||||||
@interface AFRestClient ()
|
@interface AFRestClient ()
|
||||||
|
@property (readwrite, nonatomic, retain) NSURL *baseURL;
|
||||||
@property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders;
|
@property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders;
|
||||||
@property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;
|
@property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation AFRestClient
|
@implementation AFRestClient
|
||||||
|
@synthesize baseURL = _baseURL;
|
||||||
@synthesize defaultHeaders = _defaultHeaders;
|
@synthesize defaultHeaders = _defaultHeaders;
|
||||||
@synthesize operationQueue = _operationQueue;
|
@synthesize operationQueue = _operationQueue;
|
||||||
|
|
||||||
- (id)init {
|
- (id)initWithBaseURL:(NSURL *)url {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.baseURL = url;
|
||||||
|
|
||||||
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
|
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
|
||||||
[self.operationQueue setMaxConcurrentOperationCount:2];
|
[self.operationQueue setMaxConcurrentOperationCount:2];
|
||||||
|
|
||||||
|
|
@ -64,19 +68,12 @@ static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc {
|
- (void)dealloc {
|
||||||
|
[_baseURL release];
|
||||||
[_defaultHeaders release];
|
[_defaultHeaders release];
|
||||||
[_operationQueue release];
|
[_operationQueue release];
|
||||||
[super dealloc];
|
[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 {
|
- (NSString *)defaultValueForHeader:(NSString *)header {
|
||||||
return [self.defaultHeaders valueForKey: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 *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
|
||||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
||||||
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:self.defaultHeaders];
|
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) {
|
if (parameters) {
|
||||||
NSMutableArray *mutableParameterComponents = [NSMutableArray array];
|
NSMutableArray *mutableParameterComponents = [NSMutableArray array];
|
||||||
|
|
|
||||||
|
|
@ -34,14 +34,14 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/";
|
||||||
+ (id)sharedClient {
|
+ (id)sharedClient {
|
||||||
static dispatch_once_t oncePredicate;
|
static dispatch_once_t oncePredicate;
|
||||||
dispatch_once(&oncePredicate, ^{
|
dispatch_once(&oncePredicate, ^{
|
||||||
_sharedClient = [[self alloc] init];
|
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAFGowallaBaseURLString]];
|
||||||
});
|
});
|
||||||
|
|
||||||
return _sharedClient;
|
return _sharedClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)init {
|
- (id)initWithBaseURL:(NSURL *)url {
|
||||||
self = [super init];
|
self = [super initWithBaseURL:url];
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
@ -58,8 +58,4 @@ NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/";
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (NSURL *)baseURL {
|
|
||||||
return [NSURL URLWithString:kAFGowallaBaseURLString];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue