Extracting core aspects of AFGowallaAPI into AFRestClient
This commit is contained in:
parent
87a90fe169
commit
5b27bf6511
12 changed files with 8762 additions and 5552 deletions
|
|
@ -32,13 +32,6 @@ extern NSString * const AFHTTPOperationParsedDataErrorKey;
|
|||
|
||||
@class AFHTTPOperationCallback;
|
||||
|
||||
// QHTTPOperation/NSOperation subclass that provides JSON parsing and started/finished notifications.
|
||||
//
|
||||
// Usage:
|
||||
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apple.com"]];
|
||||
// AFHTTPOperation *op = [[[AFHTTPOperation alloc] initWithRequest:request] autorelease];
|
||||
// [[NSOperationQueue mainQueue] addOperation:op];
|
||||
//
|
||||
@interface AFHTTPOperation : QHTTPOperation {
|
||||
@private
|
||||
AFHTTPOperationCallback *_callback;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
/* Begin PBXBuildFile section */
|
||||
F8D25CC41396948D00CF3BD6 /* AFGowallaAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CC21396948D00CF3BD6 /* AFGowallaAPI.m */; };
|
||||
F8D25CC51396948D00CF3BD6 /* AFImageRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CC31396948D00CF3BD6 /* AFImageRequest.m */; };
|
||||
F8D25CC813969DD200CF3BD6 /* AFRestClient.m in Sources */ = {isa = PBXBuildFile; fileRef = F8D25CC713969DD200CF3BD6 /* AFRestClient.m */; };
|
||||
F8E469651395739D00DB05C8 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469641395739D00DB05C8 /* UIKit.framework */; };
|
||||
F8E469671395739D00DB05C8 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469661395739D00DB05C8 /* Foundation.framework */; };
|
||||
F8E469691395739D00DB05C8 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469681395739D00DB05C8 /* CoreGraphics.framework */; };
|
||||
|
|
@ -36,6 +37,8 @@
|
|||
F8D25CC11396948D00CF3BD6 /* AFImageRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFImageRequest.h; path = AFNetworkingExample/Classes/AFImageRequest.h; sourceTree = "<group>"; };
|
||||
F8D25CC21396948D00CF3BD6 /* AFGowallaAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFGowallaAPI.m; path = AFNetworkingExample/Classes/AFGowallaAPI.m; sourceTree = "<group>"; };
|
||||
F8D25CC31396948D00CF3BD6 /* AFImageRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFImageRequest.m; path = AFNetworkingExample/Classes/AFImageRequest.m; sourceTree = "<group>"; };
|
||||
F8D25CC613969DD100CF3BD6 /* AFRestClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFRestClient.h; sourceTree = "<group>"; };
|
||||
F8D25CC713969DD200CF3BD6 /* AFRestClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFRestClient.m; sourceTree = "<group>"; };
|
||||
F8E469601395739C00DB05C8 /* AFNetworkingExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AFNetworkingExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
F8E469641395739D00DB05C8 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
|
||||
F8E469661395739D00DB05C8 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
|
||||
|
|
@ -170,10 +173,12 @@
|
|||
F8E469871395742500DB05C8 /* AFHTTPOperation.m */,
|
||||
F8E469881395742500DB05C8 /* AFImageRequestOperation.h */,
|
||||
F8E469891395742500DB05C8 /* AFImageRequestOperation.m */,
|
||||
F8E4698A1395742500DB05C8 /* AFURLCache.h */,
|
||||
F8E4698B1395742500DB05C8 /* AFURLCache.m */,
|
||||
F8E4698C1395742500DB05C8 /* UIImage+AFNetworking.h */,
|
||||
F8E4698D1395742500DB05C8 /* UIImage+AFNetworking.m */,
|
||||
F8D25CC613969DD100CF3BD6 /* AFRestClient.h */,
|
||||
F8D25CC713969DD200CF3BD6 /* AFRestClient.m */,
|
||||
F8E4698A1395742500DB05C8 /* AFURLCache.h */,
|
||||
F8E4698B1395742500DB05C8 /* AFURLCache.m */,
|
||||
);
|
||||
name = Alamofire;
|
||||
sourceTree = "<group>";
|
||||
|
|
@ -338,6 +343,7 @@
|
|||
F8E469EC13957FC500DB05C8 /* TTTLocationFormatter.m in Sources */,
|
||||
F8D25CC41396948D00CF3BD6 /* AFGowallaAPI.m in Sources */,
|
||||
F8D25CC51396948D00CF3BD6 /* AFImageRequest.m in Sources */,
|
||||
F8D25CC813969DD200CF3BD6 /* AFRestClient.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -21,32 +21,10 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AFHTTPOperation.h"
|
||||
#import "AFRestClient.h"
|
||||
|
||||
extern NSString * const kAFGowallaClientID;
|
||||
extern NSString * const kAFGowallaClientSecret;
|
||||
|
||||
@interface AFGowallaAPI : NSObject
|
||||
|
||||
+ (NSString *)defaultValueForHeader:(NSString *)header;
|
||||
+ (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
|
||||
+ (void)setAuthorizationHeaderWithToken:(NSString *)token;
|
||||
+ (void)clearAuthorizationHeader;
|
||||
|
||||
+ (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters;
|
||||
+ (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request callback:(AFHTTPOperationCallback *)callback;
|
||||
+ (void)enqueueHTTPOperation:(AFHTTPOperation *)operation;
|
||||
|
||||
+ (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
+ (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
+ (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
+ (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
extern NSString * const kAFGowallaBaseURLString;
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark - NSString + AFGowallaAPI
|
||||
|
||||
@interface NSString (AFGowallaAPI)
|
||||
- (NSString *)urlEncodedString;
|
||||
- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding;
|
||||
@interface AFGowallaAPI : AFRestClient
|
||||
@end
|
||||
|
|
@ -21,38 +21,19 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#import "AFGowallaAPI.h"
|
||||
#import "AFHTTPOperation.h"
|
||||
|
||||
// Replace this with your own API Key, available at http://api.gowalla.com/api/keys/
|
||||
NSString * const kAFGowallaClientID = @"e7ccb7d3d2414eb2af4663fc91eb2793";
|
||||
|
||||
static NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/";
|
||||
|
||||
static NSStringEncoding const kAFStringEncoding = NSUTF8StringEncoding;
|
||||
|
||||
static NSMutableDictionary *_defaultHeaders = nil;
|
||||
static NSOperationQueue *_operationQueue = nil;
|
||||
NSString * const kAFGowallaBaseURLString = @"https://api.gowalla.com/";
|
||||
|
||||
@implementation AFGowallaAPI
|
||||
|
||||
+ (void)initialize {
|
||||
_operationQueue = [[NSOperationQueue alloc] init];
|
||||
[_operationQueue setMaxConcurrentOperationCount:2];
|
||||
|
||||
_defaultHeaders = [[NSMutableDictionary alloc] init];
|
||||
|
||||
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
||||
[self setDefaultHeader:@"Accept" value:@"application/json"];
|
||||
|
||||
// Accept-Encoding HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
||||
[self setDefaultHeader:@"Accept-Encoding" value:@"gzip"];
|
||||
|
||||
// Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
||||
NSString *preferredLanguageCodes = [[NSLocale preferredLanguages] componentsJoinedByString:@", "];
|
||||
[self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]];
|
||||
|
||||
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
|
||||
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"AFNetworkingExample/%@ (%@, %@ %@, %@, Scale/%f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], @"unknown", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion], [[UIDevice currentDevice] model], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0)]];
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
// X-Gowalla-API-Key HTTP Header; see http://api.gowalla.com/api/docs
|
||||
[self setDefaultHeader:@"X-Gowalla-API-Key" value:kAFGowallaClientID];
|
||||
|
|
@ -62,110 +43,12 @@ static NSOperationQueue *_operationQueue = nil;
|
|||
|
||||
// X-UDID HTTP Header
|
||||
[self setDefaultHeader:@"X-UDID" value:[[UIDevice currentDevice] uniqueIdentifier]];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (NSString *)defaultValueForHeader:(NSString *)header {
|
||||
return [_defaultHeaders valueForKey:header];
|
||||
}
|
||||
|
||||
+ (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
|
||||
[_defaultHeaders setObject:value forKey:header];
|
||||
}
|
||||
|
||||
+ (void)setAuthorizationHeaderWithToken:(NSString *)token {
|
||||
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
|
||||
}
|
||||
|
||||
+ (void)clearAuthorizationHeader {
|
||||
[_defaultHeaders removeObjectForKey:@"Authorization"];
|
||||
}
|
||||
|
||||
+ (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
|
||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
||||
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:_defaultHeaders];
|
||||
NSURL *url = nil;
|
||||
|
||||
NSMutableArray *mutableParameterComponents = [NSMutableArray array];
|
||||
for (id key in [parameters allKeys]) {
|
||||
NSString *component = [NSString stringWithFormat:@"%@=%@", [key urlEncodedStringWithEncoding:kAFStringEncoding], [[parameters valueForKey:key] urlEncodedStringWithEncoding:kAFStringEncoding]];
|
||||
[mutableParameterComponents addObject:component];
|
||||
}
|
||||
NSString *queryString = [mutableParameterComponents componentsJoinedByString:@"&"];
|
||||
|
||||
if ([method isEqualToString:@"GET"]) {
|
||||
path = [path stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", queryString];
|
||||
url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:kAFGowallaBaseURLString]];
|
||||
} else {
|
||||
url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:kAFGowallaBaseURLString]];
|
||||
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(kAFStringEncoding));
|
||||
[headers setObject:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forKey:@"Content-Type"];
|
||||
[request setHTTPBody:[queryString dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
}
|
||||
|
||||
[request setURL:url];
|
||||
[request setHTTPMethod:method];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
[request setAllHTTPHeaderFields:headers];
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
+ (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request callback:(AFHTTPOperationCallback *)callback {
|
||||
if ([request URL] == nil || [[request URL] isEqual:[NSNull null]]) {
|
||||
return;
|
||||
}
|
||||
|
||||
AFHTTPOperation *operation = [[[AFHTTPOperation alloc] initWithRequest:request callback:callback] autorelease];
|
||||
[self enqueueHTTPOperation:operation];
|
||||
}
|
||||
|
||||
+ (void)enqueueHTTPOperation:(AFHTTPOperation *)operation {
|
||||
[_operationQueue addOperation:operation];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark HTTP Methods
|
||||
|
||||
+ (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
+ (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
+ (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
+ (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark -
|
||||
|
||||
@implementation NSString (AFGowallaAPI)
|
||||
|
||||
// See http://github.com/pokeb/asi-http-request/raw/master/Classes/ASIFormDataRequest.m
|
||||
- (NSString*)urlEncodedString {
|
||||
return [self urlEncodedStringWithEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding {
|
||||
NSString *newString = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];
|
||||
|
||||
if (newString) {
|
||||
return newString;
|
||||
}
|
||||
|
||||
return @"";
|
||||
+ (NSURL *)baseURL {
|
||||
return [NSURL URLWithString:kAFGowallaBaseURLString];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@
|
|||
[mutableParameters setValue:[NSString stringWithFormat:@"%1.7f", location.coordinate.longitude] forKey:@"lng"];
|
||||
}
|
||||
|
||||
[AFGowallaAPI getPath:urlString parameters:mutableParameters callback:[AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
|
||||
[[AFGowallaAPI sharedClient] getPath:urlString parameters:mutableParameters callback:[AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
|
||||
if (block) {
|
||||
NSMutableArray *mutableRecords = [NSMutableArray array];
|
||||
for (NSDictionary *attributes in [data valueForKeyPath:@"spots"]) {
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@
|
|||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
int retVal = UIApplicationMain(argc, argv, @"UIApplication", @"AppDelegate");
|
||||
[pool release];
|
||||
|
|
|
|||
54
AFRestClient.h
Normal file
54
AFRestClient.h
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// AFRestClient.h
|
||||
//
|
||||
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AFHTTPOperation.h"
|
||||
|
||||
@protocol AFRestClient <NSObject>
|
||||
@required
|
||||
+ (NSURL *)baseURL;
|
||||
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters;
|
||||
@end
|
||||
|
||||
@interface AFRestClient : NSObject <AFRestClient>
|
||||
+ (id)sharedClient;
|
||||
|
||||
- (NSString *)defaultValueForHeader:(NSString *)header;
|
||||
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value;
|
||||
- (void)setAuthorizationHeaderWithToken:(NSString *)token;
|
||||
- (void)clearAuthorizationHeader;
|
||||
|
||||
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback;
|
||||
|
||||
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request callback:(AFHTTPOperationCallback *)callback;
|
||||
- (void)enqueueHTTPOperation:(AFHTTPOperation *)operation;
|
||||
@end
|
||||
|
||||
#pragma mark - NSString + AFRestClient
|
||||
|
||||
@interface NSString (AFRestClient)
|
||||
- (NSString *)urlEncodedString;
|
||||
- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding;
|
||||
@end
|
||||
197
AFRestClient.m
Normal file
197
AFRestClient.m
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
// AFRestClient.m
|
||||
//
|
||||
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import "AFRestClient.h"
|
||||
#import "AFHTTPOperation.h"
|
||||
|
||||
static NSStringEncoding const kAFRestClientStringEncoding = NSUTF8StringEncoding;
|
||||
static AFRestClient *_sharedClient = nil;
|
||||
|
||||
@interface AFRestClient ()
|
||||
@property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders;
|
||||
@property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;
|
||||
@end
|
||||
|
||||
@implementation AFRestClient
|
||||
@synthesize defaultHeaders = _defaultHeaders;
|
||||
@synthesize operationQueue = _operationQueue;
|
||||
|
||||
+ (id)sharedClient {
|
||||
if (_sharedClient == nil) {
|
||||
@synchronized(self) {
|
||||
_sharedClient = [[self alloc] init];
|
||||
}
|
||||
}
|
||||
|
||||
return _sharedClient;
|
||||
}
|
||||
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
|
||||
[self.operationQueue setMaxConcurrentOperationCount:2];
|
||||
|
||||
self.defaultHeaders = [NSMutableDictionary dictionary];
|
||||
|
||||
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
||||
[self setDefaultHeader:@"Accept" value:@"application/json"];
|
||||
|
||||
// Accept-Encoding HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
||||
[self setDefaultHeader:@"Accept-Encoding" value:@"gzip"];
|
||||
|
||||
// Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
||||
NSString *preferredLanguageCodes = [[NSLocale preferredLanguages] componentsJoinedByString:@", "];
|
||||
[self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]];
|
||||
|
||||
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
|
||||
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@, %@ %@, %@, Scale/%f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], @"unknown", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion], [[UIDevice currentDevice] model], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0)]];
|
||||
|
||||
// X-UDID HTTP Header
|
||||
[self setDefaultHeader:@"X-UDID" value:[[UIDevice currentDevice] uniqueIdentifier]];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_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;
|
||||
}
|
||||
|
||||
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
|
||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
||||
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithDictionary:_defaultHeaders];
|
||||
NSURL *url = nil;
|
||||
|
||||
NSMutableArray *mutableParameterComponents = [NSMutableArray array];
|
||||
for (id key in [parameters allKeys]) {
|
||||
NSString *component = [NSString stringWithFormat:@"%@=%@", [key urlEncodedStringWithEncoding:kAFRestClientStringEncoding], [[parameters valueForKey:key] urlEncodedStringWithEncoding:kAFRestClientStringEncoding]];
|
||||
[mutableParameterComponents addObject:component];
|
||||
}
|
||||
NSString *queryString = [mutableParameterComponents componentsJoinedByString:@"&"];
|
||||
|
||||
if ([method isEqualToString:@"GET"]) {
|
||||
path = [path stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", queryString];
|
||||
url = [NSURL URLWithString:path relativeToURL:[[self class] baseURL]];
|
||||
} else {
|
||||
url = [NSURL URLWithString:path relativeToURL:[[self class] baseURL]];
|
||||
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(kAFRestClientStringEncoding));
|
||||
[headers setObject:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forKey:@"Content-Type"];
|
||||
[request setHTTPBody:[queryString dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
}
|
||||
|
||||
[request setURL:url];
|
||||
[request setHTTPMethod:method];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
[request setAllHTTPHeaderFields:headers];
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
- (NSString *)defaultValueForHeader:(NSString *)header {
|
||||
return [self.defaultHeaders valueForKey:header];
|
||||
}
|
||||
|
||||
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
|
||||
[self.defaultHeaders setObject:value forKey:header];
|
||||
}
|
||||
|
||||
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
|
||||
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
|
||||
}
|
||||
|
||||
- (void)clearAuthorizationHeader {
|
||||
[self.defaultHeaders removeObjectForKey:@"Authorization"];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters callback:(AFHTTPOperationCallback *)callback {
|
||||
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
||||
[self enqueueHTTPOperationWithRequest:request callback:callback];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request callback:(AFHTTPOperationCallback *)callback {
|
||||
if ([request URL] == nil || [[request URL] isEqual:[NSNull null]]) {
|
||||
return;
|
||||
}
|
||||
|
||||
AFHTTPOperation *operation = [[[AFHTTPOperation alloc] initWithRequest:request callback:callback] autorelease];
|
||||
[self enqueueHTTPOperation:operation];
|
||||
}
|
||||
|
||||
- (void)enqueueHTTPOperation:(AFHTTPOperation *)operation {
|
||||
[self.operationQueue addOperation:operation];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark - NSString + AFRestClient
|
||||
|
||||
@implementation NSString (AFRestClient)
|
||||
|
||||
// See http://github.com/pokeb/asi-http-request/raw/master/Classes/ASIFormDataRequest.m
|
||||
- (NSString*)urlEncodedString {
|
||||
return [self urlEncodedStringWithEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding {
|
||||
NSString *newString = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];
|
||||
|
||||
if (newString) {
|
||||
return newString;
|
||||
}
|
||||
|
||||
return @"";
|
||||
}
|
||||
|
||||
@end
|
||||
Loading…
Add table
Reference in a new issue