OTRest Classes

This commit is contained in:
Jeremy Ellison
2009-08-08 11:47:50 -04:00
parent bad6024d0f
commit 846d114c82
31 changed files with 1200 additions and 1402 deletions

View File

@@ -0,0 +1,18 @@
//
// NSDictionary+OTRestRequestSerialization.h
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OTRestRequestSerializable.h"
@interface NSDictionary (OTRestRequestSerialization) <OTRestRequestSerializable>
- (NSString*)URLEncodedString;
- (NSString*)ContentTypeHTTPHeader;
- (NSData*)HTTPBody;
@end

View File

@@ -0,0 +1,45 @@
//
// NSDictionary+OTRestRequestSerialization.m
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "NSDictionary+OTRestRequestSerialization.h"
// private helper function to convert any object to its string representation
static NSString *toString(id object) {
return [NSString stringWithFormat: @"%@", object];
}
// private helper function to convert string to UTF-8 and URL encode it
static NSString *urlEncode(id object) {
NSString *string = toString(object);
return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}
@implementation NSDictionary (OTRestRequestSerialization)
- (NSString*)URLEncodedString {
NSMutableArray *parts = [NSMutableArray array];
for (id key in self) {
id value = [self objectForKey: key];
NSString *part = [NSString stringWithFormat: @"%@=%@",
urlEncode(key), urlEncode(value)];
[parts addObject: part];
}
return [parts componentsJoinedByString: @"&"];
}
- (NSString*)ContentTypeHTTPHeader {
return @"application/x-www-form-urlencoded";
}
- (NSData*)HTTPBody {
return [[self URLEncodedString] dataUsingEncoding:NSUTF8StringEncoding];
}
@end

87
OTRestClient.h Normal file
View File

@@ -0,0 +1,87 @@
//
// OTRestClient.h
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestRequest.h"
#import "OTRestParams.h"
#import "OTRestResponse.h"
#import "NSDictionary+OTRestRequestSerialization.h"
#import "Element+OTRestAdditions.h"
@interface OTRestClient : NSObject {
NSString* _baseURL;
NSString* _username;
NSString* _password;
NSMutableDictionary* _HTTPHeaders;
}
/**
* The base URL all resources are nested underneath
*/
@property(nonatomic, retain) NSString* baseURL;
/**
* The username to use for authentication via HTTP AUTH
*/
@property(nonatomic, retain) NSString* username;
/**
* The password to use for authentication via HTTP AUTH
*/
@property(nonatomic, retain) NSString* password;
/**
* A dictionary of headers to be sent with each request
*/
@property(nonatomic, readonly) NSDictionary* HTTPHeaders;
/**
* Return the configured singleton instance of the Rest client
*/
+ (OTRestClient*)client;
/**
* Set the shared singleton issue of the Rest client
*/
+ (void)setClient:(OTRestClient*)client;
/**
* Return a Rest client scoped to a particular base URL. If the singleton client is nil, the return client is set as the singleton
*/
+ (OTRestClient*)clientWithBaseURL:(NSString*)baseURL;
/**
* Return a Rest client scoped to a particular base URL with a set of HTTP AUTH credentials. If the singleton client is nil, the return client is set as the singleton
*/
+ (OTRestClient*)clientWithBaseURL:(NSString*)baseURL username:(NSString*)username password:(NSString*)password;
/**
* Fetch a resource via an HTTP GET and invoke a callback with the resulting payload
*/
- (OTRestRequest*)get:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
/**
* Create a resource via an HTTP POST with a set of form parameters and invoke a callback with the resulting payload
*/
- (OTRestRequest*)post:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback;
/**
* Update a resource via an HTTP PUT and invoke a callback with the resulting payload
*/
- (OTRestRequest*)put:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback;
/**
* Destroy a resource via an HTTP DELETE and invoke a callback with the resulting payload
*/
- (OTRestRequest*)delete:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback;
/**
* Adds an HTTP header to each request dispatched through the Rest client
*/
- (void)setValue:(NSString*)value forHTTPHeaderField:(NSString*)header;
@end

102
OTRestClient.m Normal file
View File

@@ -0,0 +1,102 @@
//
// OTRestClient.m
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestClient.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// global
static OTRestClient* sharedClient = nil;
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation OTRestClient
@synthesize baseURL = _baseURL, username = _username, password = _password, HTTPHeaders = _HTTPHeaders;
+ (OTRestClient*)client {
return sharedClient;
}
+ (void)setClient:(OTRestClient*)client {
[sharedClient release];
sharedClient = [client retain];
}
+ (OTRestClient*)clientWithBaseURL:(NSString*)baseURL {
OTRestClient* client = [[[OTRestClient alloc] init] autorelease];
client.baseURL = baseURL;
if (sharedClient == nil) {
[OTRestClient setClient:client];
}
return client;
}
+ (OTRestClient*)clientWithBaseURL:(NSString*)baseURL username:(NSString*)username password:(NSString*)password {
OTRestClient* client = [OTRestClient clientWithBaseURL:baseURL];
client.username = username;
client.password = password;
return client;
}
- (id)init {
if (self = [super init]) {
_HTTPHeaders = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[_baseURL release];
[_username release];
[_password release];
[_HTTPHeaders release];
[super dealloc];
}
- (NSURL*)URLForResourcePath:(NSString*)resourcePath {
NSString* urlString = [NSString stringWithFormat:@"%@%@", self.baseURL, resourcePath];
return [NSURL URLWithString:urlString];
}
- (OTRestRequest*)get:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[request get];
return request;
}
- (OTRestRequest*)post:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[request postParams:params];
return request;
}
- (OTRestRequest*)put:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[request putParams:params];
return request;
}
- (OTRestRequest*)delete:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[request delete];
return request;
}
- (void)setValue:(NSString*)value forHTTPHeaderField:(NSString*)header {
[_HTTPHeaders setValue:value forKey:header];
}
@end

File diff suppressed because it is too large Load Diff

View File

@@ -1,51 +0,0 @@
// !$*UTF8*$!
{
0867D690FE84028FC02AAC07 /* Project object */ = {
activeBuildConfigurationName = Debug;
activeTarget = D2AAC07D0554694100DB518D /* OTRestFramework */;
codeSenseManager = 3F4E187E102DD18F00320118 /* Code sense */;
perUserDictionary = {
PBXConfiguration.PBXFileTableDataSource3.PBXFileTableDataSource = {
PBXFileTableDataSourceColumnSortingDirectionKey = "-1";
PBXFileTableDataSourceColumnSortingKey = PBXFileDataSource_Filename_ColumnID;
PBXFileTableDataSourceColumnWidthsKey = (
20,
341,
20,
48.16259765625,
43,
43,
20,
);
PBXFileTableDataSourceColumnsKey = (
PBXFileDataSource_FiletypeID,
PBXFileDataSource_Filename_ColumnID,
PBXFileDataSource_Built_ColumnID,
PBXFileDataSource_ObjectSize_ColumnID,
PBXFileDataSource_Errors_ColumnID,
PBXFileDataSource_Warnings_ColumnID,
PBXFileDataSource_Target_ColumnID,
);
};
PBXPerProjectTemplateStateSaveDate = 271438212;
PBXWorkspaceStateSaveDate = 271438212;
};
sourceControlManager = 3F4E187D102DD18F00320118 /* Source Control */;
userBuildSettings = {
};
};
3F4E187D102DD18F00320118 /* Source Control */ = {
isa = PBXSourceControlManager;
fallbackIsa = XCSourceControlManager;
isSCMEnabled = 0;
scmConfiguration = {
};
};
3F4E187E102DD18F00320118 /* Code sense */ = {
isa = PBXCodeSenseManager;
indexTemplatePath = "";
};
D2AAC07D0554694100DB518D /* OTRestFramework */ = {
activeExec = 0;
};
}

View File

@@ -7,11 +7,101 @@
objects = {
/* Begin PBXBuildFile section */
3F4E18F2102DD38800320118 /* OTRestClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18E3102DD38700320118 /* OTRestClient.h */; };
3F4E18F3102DD38800320118 /* OTRestClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18E4102DD38700320118 /* OTRestClient.m */; };
3F4E18F4102DD38800320118 /* OTRestParams.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18E5102DD38700320118 /* OTRestParams.h */; };
3F4E18F5102DD38800320118 /* OTRestParams.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18E6102DD38700320118 /* OTRestParams.m */; };
3F4E18F6102DD38800320118 /* OTRestParamsAttachment.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18E7102DD38700320118 /* OTRestParamsAttachment.h */; };
3F4E18F7102DD38800320118 /* OTRestParamsAttachment.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18E8102DD38700320118 /* OTRestParamsAttachment.m */; };
3F4E18F8102DD38800320118 /* OTRestParamsDataAttachment.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18E9102DD38700320118 /* OTRestParamsDataAttachment.h */; };
3F4E18F9102DD38800320118 /* OTRestParamsDataAttachment.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18EA102DD38700320118 /* OTRestParamsDataAttachment.m */; };
3F4E18FA102DD38800320118 /* OTRestParamsFileAttachment.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18EB102DD38700320118 /* OTRestParamsFileAttachment.h */; };
3F4E18FB102DD38800320118 /* OTRestParamsFileAttachment.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18EC102DD38700320118 /* OTRestParamsFileAttachment.m */; };
3F4E18FC102DD38800320118 /* OTRestRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18ED102DD38700320118 /* OTRestRequest.h */; };
3F4E18FD102DD38800320118 /* OTRestRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18EE102DD38700320118 /* OTRestRequest.m */; };
3F4E18FE102DD38800320118 /* OTRestRequestSerializable.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18EF102DD38700320118 /* OTRestRequestSerializable.h */; };
3F4E18FF102DD38800320118 /* OTRestResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E18F0102DD38700320118 /* OTRestResponse.h */; };
3F4E1900102DD38800320118 /* OTRestResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E18F1102DD38800320118 /* OTRestResponse.m */; };
3F4E191A102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1918102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.h */; };
3F4E191B102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E1919102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.m */; };
3F4E1936102DD4B300320118 /* CDataChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1925102DD4B300320118 /* CDataChunk.h */; };
3F4E1937102DD4B300320118 /* Chunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1926102DD4B300320118 /* Chunk.h */; };
3F4E1938102DD4B300320118 /* CommentChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1927102DD4B300320118 /* CommentChunk.h */; };
3F4E1939102DD4B300320118 /* CSSPartMatcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1928102DD4B300320118 /* CSSPartMatcher.h */; };
3F4E193A102DD4B300320118 /* CSSSelector.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1929102DD4B300320118 /* CSSSelector.h */; };
3F4E193B102DD4B300320118 /* CSSSelectorMatcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192A102DD4B300320118 /* CSSSelectorMatcher.h */; };
3F4E193C102DD4B300320118 /* CSSSelectorPart.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192B102DD4B300320118 /* CSSSelectorPart.h */; };
3F4E193D102DD4B300320118 /* DoctypeChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192C102DD4B300320118 /* DoctypeChunk.h */; };
3F4E193E102DD4B300320118 /* DocumentRoot.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192D102DD4B300320118 /* DocumentRoot.h */; };
3F4E193F102DD4B300320118 /* Element.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192E102DD4B300320118 /* Element.h */; };
3F4E1940102DD4B300320118 /* ElementParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E192F102DD4B300320118 /* ElementParser.h */; };
3F4E1941102DD4B300320118 /* EntityChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1930102DD4B300320118 /* EntityChunk.h */; };
3F4E1942102DD4B300320118 /* NSString_HTML.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1931102DD4B300320118 /* NSString_HTML.h */; };
3F4E1943102DD4B300320118 /* ProcessingInstructionChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1932102DD4B300320118 /* ProcessingInstructionChunk.h */; };
3F4E1944102DD4B300320118 /* TagChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1933102DD4B300320118 /* TagChunk.h */; };
3F4E1945102DD4B300320118 /* TxtChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1934102DD4B300320118 /* TxtChunk.h */; };
3F4E1946102DD4B300320118 /* URLParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1935102DD4B300320118 /* URLParser.h */; };
3F4E194A102DD4C900320118 /* Element+OTRestAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F4E1948102DD4C900320118 /* Element+OTRestAdditions.h */; };
3F4E194B102DD4C900320118 /* Element+OTRestAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F4E1949102DD4C900320118 /* Element+OTRestAdditions.m */; };
AA747D9F0F9514B9006C5449 /* OTRestFramework_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* OTRestFramework_Prefix.pch */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
3F4E18DA102DD31E00320118 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 3F4E18D6102DD31E00320118 /* ElementParser.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = D2AAC07E0554694100DB518D /* libElementParser.a */;
remoteInfo = ElementParser;
};
3F4E18DC102DD32A00320118 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 3F4E18D6102DD31E00320118 /* ElementParser.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = D2AAC07D0554694100DB518D /* ElementParser */;
remoteInfo = ElementParser;
};
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
3F4E18D6102DD31E00320118 /* ElementParser.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ElementParser.xcodeproj; path = ../ElementParser/ElementParser.xcodeproj; sourceTree = SOURCE_ROOT; };
3F4E18E3102DD38700320118 /* OTRestClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestClient.h; sourceTree = SOURCE_ROOT; };
3F4E18E4102DD38700320118 /* OTRestClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestClient.m; sourceTree = SOURCE_ROOT; };
3F4E18E5102DD38700320118 /* OTRestParams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestParams.h; sourceTree = SOURCE_ROOT; };
3F4E18E6102DD38700320118 /* OTRestParams.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestParams.m; sourceTree = SOURCE_ROOT; };
3F4E18E7102DD38700320118 /* OTRestParamsAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestParamsAttachment.h; sourceTree = SOURCE_ROOT; };
3F4E18E8102DD38700320118 /* OTRestParamsAttachment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestParamsAttachment.m; sourceTree = SOURCE_ROOT; };
3F4E18E9102DD38700320118 /* OTRestParamsDataAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestParamsDataAttachment.h; sourceTree = SOURCE_ROOT; };
3F4E18EA102DD38700320118 /* OTRestParamsDataAttachment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestParamsDataAttachment.m; sourceTree = SOURCE_ROOT; };
3F4E18EB102DD38700320118 /* OTRestParamsFileAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestParamsFileAttachment.h; sourceTree = SOURCE_ROOT; };
3F4E18EC102DD38700320118 /* OTRestParamsFileAttachment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestParamsFileAttachment.m; sourceTree = SOURCE_ROOT; };
3F4E18ED102DD38700320118 /* OTRestRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestRequest.h; sourceTree = SOURCE_ROOT; };
3F4E18EE102DD38700320118 /* OTRestRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestRequest.m; sourceTree = SOURCE_ROOT; };
3F4E18EF102DD38700320118 /* OTRestRequestSerializable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestRequestSerializable.h; sourceTree = SOURCE_ROOT; };
3F4E18F0102DD38700320118 /* OTRestResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestResponse.h; sourceTree = SOURCE_ROOT; };
3F4E18F1102DD38800320118 /* OTRestResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRestResponse.m; sourceTree = SOURCE_ROOT; };
3F4E1918102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+OTRestRequestSerialization.h"; sourceTree = SOURCE_ROOT; };
3F4E1919102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+OTRestRequestSerialization.m"; sourceTree = SOURCE_ROOT; };
3F4E1925102DD4B300320118 /* CDataChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CDataChunk.h; path = ../ElementParser/Classes/CDataChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1926102DD4B300320118 /* Chunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Chunk.h; path = ../ElementParser/Classes/Chunk.h; sourceTree = SOURCE_ROOT; };
3F4E1927102DD4B300320118 /* CommentChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommentChunk.h; path = ../ElementParser/Classes/CommentChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1928102DD4B300320118 /* CSSPartMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSSPartMatcher.h; path = ../ElementParser/Classes/CSSPartMatcher.h; sourceTree = SOURCE_ROOT; };
3F4E1929102DD4B300320118 /* CSSSelector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSSSelector.h; path = ../ElementParser/Classes/CSSSelector.h; sourceTree = SOURCE_ROOT; };
3F4E192A102DD4B300320118 /* CSSSelectorMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSSSelectorMatcher.h; path = ../ElementParser/Classes/CSSSelectorMatcher.h; sourceTree = SOURCE_ROOT; };
3F4E192B102DD4B300320118 /* CSSSelectorPart.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSSSelectorPart.h; path = ../ElementParser/Classes/CSSSelectorPart.h; sourceTree = SOURCE_ROOT; };
3F4E192C102DD4B300320118 /* DoctypeChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DoctypeChunk.h; path = ../ElementParser/Classes/DoctypeChunk.h; sourceTree = SOURCE_ROOT; };
3F4E192D102DD4B300320118 /* DocumentRoot.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DocumentRoot.h; path = ../ElementParser/Classes/DocumentRoot.h; sourceTree = SOURCE_ROOT; };
3F4E192E102DD4B300320118 /* Element.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Element.h; path = ../ElementParser/Classes/Element.h; sourceTree = SOURCE_ROOT; };
3F4E192F102DD4B300320118 /* ElementParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ElementParser.h; path = ../ElementParser/Classes/ElementParser.h; sourceTree = SOURCE_ROOT; };
3F4E1930102DD4B300320118 /* EntityChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EntityChunk.h; path = ../ElementParser/Classes/EntityChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1931102DD4B300320118 /* NSString_HTML.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSString_HTML.h; path = ../ElementParser/Classes/NSString_HTML.h; sourceTree = SOURCE_ROOT; };
3F4E1932102DD4B300320118 /* ProcessingInstructionChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ProcessingInstructionChunk.h; path = ../ElementParser/Classes/ProcessingInstructionChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1933102DD4B300320118 /* TagChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TagChunk.h; path = ../ElementParser/Classes/TagChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1934102DD4B300320118 /* TxtChunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TxtChunk.h; path = ../ElementParser/Classes/TxtChunk.h; sourceTree = SOURCE_ROOT; };
3F4E1935102DD4B300320118 /* URLParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = URLParser.h; path = ../ElementParser/Classes/URLParser.h; sourceTree = SOURCE_ROOT; };
3F4E1948102DD4C900320118 /* Element+OTRestAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "Element+OTRestAdditions.h"; path = "../gateguru-iphone/Element+OTRestAdditions.h"; sourceTree = SOURCE_ROOT; };
3F4E1949102DD4C900320118 /* Element+OTRestAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "Element+OTRestAdditions.m"; path = "../gateguru-iphone/Element+OTRestAdditions.m"; sourceTree = SOURCE_ROOT; };
AA747D9E0F9514B9006C5449 /* OTRestFramework_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRestFramework_Prefix.pch; sourceTree = SOURCE_ROOT; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D2AAC07E0554694100DB518D /* libOTRestFramework.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libOTRestFramework.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -40,6 +130,8 @@
0867D691FE84028FC02AAC07 /* OTRestFramework */ = {
isa = PBXGroup;
children = (
3F4E1924102DD4B300320118 /* ElementParserHeaders */,
3F4E18D6102DD31E00320118 /* ElementParser.xcodeproj */,
08FB77AEFE84172EC02AAC07 /* Classes */,
32C88DFF0371C24200C91783 /* Other Sources */,
0867D69AFE84028FC02AAC07 /* Frameworks */,
@@ -59,6 +151,25 @@
08FB77AEFE84172EC02AAC07 /* Classes */ = {
isa = PBXGroup;
children = (
3F4E1948102DD4C900320118 /* Element+OTRestAdditions.h */,
3F4E1949102DD4C900320118 /* Element+OTRestAdditions.m */,
3F4E1918102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.h */,
3F4E1919102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.m */,
3F4E18E3102DD38700320118 /* OTRestClient.h */,
3F4E18E4102DD38700320118 /* OTRestClient.m */,
3F4E18E5102DD38700320118 /* OTRestParams.h */,
3F4E18E6102DD38700320118 /* OTRestParams.m */,
3F4E18E7102DD38700320118 /* OTRestParamsAttachment.h */,
3F4E18E8102DD38700320118 /* OTRestParamsAttachment.m */,
3F4E18E9102DD38700320118 /* OTRestParamsDataAttachment.h */,
3F4E18EA102DD38700320118 /* OTRestParamsDataAttachment.m */,
3F4E18EB102DD38700320118 /* OTRestParamsFileAttachment.h */,
3F4E18EC102DD38700320118 /* OTRestParamsFileAttachment.m */,
3F4E18ED102DD38700320118 /* OTRestRequest.h */,
3F4E18EE102DD38700320118 /* OTRestRequest.m */,
3F4E18EF102DD38700320118 /* OTRestRequestSerializable.h */,
3F4E18F0102DD38700320118 /* OTRestResponse.h */,
3F4E18F1102DD38800320118 /* OTRestResponse.m */,
);
name = Classes;
sourceTree = "<group>";
@@ -71,6 +182,38 @@
name = "Other Sources";
sourceTree = "<group>";
};
3F4E18D7102DD31E00320118 /* Products */ = {
isa = PBXGroup;
children = (
3F4E18DB102DD31E00320118 /* libElementParser.a */,
);
name = Products;
sourceTree = "<group>";
};
3F4E1924102DD4B300320118 /* ElementParserHeaders */ = {
isa = PBXGroup;
children = (
3F4E1925102DD4B300320118 /* CDataChunk.h */,
3F4E1926102DD4B300320118 /* Chunk.h */,
3F4E1927102DD4B300320118 /* CommentChunk.h */,
3F4E1928102DD4B300320118 /* CSSPartMatcher.h */,
3F4E1929102DD4B300320118 /* CSSSelector.h */,
3F4E192A102DD4B300320118 /* CSSSelectorMatcher.h */,
3F4E192B102DD4B300320118 /* CSSSelectorPart.h */,
3F4E192C102DD4B300320118 /* DoctypeChunk.h */,
3F4E192D102DD4B300320118 /* DocumentRoot.h */,
3F4E192E102DD4B300320118 /* Element.h */,
3F4E192F102DD4B300320118 /* ElementParser.h */,
3F4E1930102DD4B300320118 /* EntityChunk.h */,
3F4E1931102DD4B300320118 /* NSString_HTML.h */,
3F4E1932102DD4B300320118 /* ProcessingInstructionChunk.h */,
3F4E1933102DD4B300320118 /* TagChunk.h */,
3F4E1934102DD4B300320118 /* TxtChunk.h */,
3F4E1935102DD4B300320118 /* URLParser.h */,
);
name = ElementParserHeaders;
sourceTree = SOURCE_ROOT;
};
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
@@ -79,6 +222,33 @@
buildActionMask = 2147483647;
files = (
AA747D9F0F9514B9006C5449 /* OTRestFramework_Prefix.pch in Headers */,
3F4E18F2102DD38800320118 /* OTRestClient.h in Headers */,
3F4E18F4102DD38800320118 /* OTRestParams.h in Headers */,
3F4E18F6102DD38800320118 /* OTRestParamsAttachment.h in Headers */,
3F4E18F8102DD38800320118 /* OTRestParamsDataAttachment.h in Headers */,
3F4E18FA102DD38800320118 /* OTRestParamsFileAttachment.h in Headers */,
3F4E18FC102DD38800320118 /* OTRestRequest.h in Headers */,
3F4E18FE102DD38800320118 /* OTRestRequestSerializable.h in Headers */,
3F4E18FF102DD38800320118 /* OTRestResponse.h in Headers */,
3F4E191A102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.h in Headers */,
3F4E1936102DD4B300320118 /* CDataChunk.h in Headers */,
3F4E1937102DD4B300320118 /* Chunk.h in Headers */,
3F4E1938102DD4B300320118 /* CommentChunk.h in Headers */,
3F4E1939102DD4B300320118 /* CSSPartMatcher.h in Headers */,
3F4E193A102DD4B300320118 /* CSSSelector.h in Headers */,
3F4E193B102DD4B300320118 /* CSSSelectorMatcher.h in Headers */,
3F4E193C102DD4B300320118 /* CSSSelectorPart.h in Headers */,
3F4E193D102DD4B300320118 /* DoctypeChunk.h in Headers */,
3F4E193E102DD4B300320118 /* DocumentRoot.h in Headers */,
3F4E193F102DD4B300320118 /* Element.h in Headers */,
3F4E1940102DD4B300320118 /* ElementParser.h in Headers */,
3F4E1941102DD4B300320118 /* EntityChunk.h in Headers */,
3F4E1942102DD4B300320118 /* NSString_HTML.h in Headers */,
3F4E1943102DD4B300320118 /* ProcessingInstructionChunk.h in Headers */,
3F4E1944102DD4B300320118 /* TagChunk.h in Headers */,
3F4E1945102DD4B300320118 /* TxtChunk.h in Headers */,
3F4E1946102DD4B300320118 /* URLParser.h in Headers */,
3F4E194A102DD4C900320118 /* Element+OTRestAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -96,6 +266,7 @@
buildRules = (
);
dependencies = (
3F4E18DD102DD32A00320118 /* PBXTargetDependency */,
);
name = OTRestFramework;
productName = OTRestFramework;
@@ -113,6 +284,12 @@
mainGroup = 0867D691FE84028FC02AAC07 /* OTRestFramework */;
productRefGroup = 034768DFFF38A50411DB9C8B /* Products */;
projectDirPath = "";
projectReferences = (
{
ProductGroup = 3F4E18D7102DD31E00320118 /* Products */;
ProjectRef = 3F4E18D6102DD31E00320118 /* ElementParser.xcodeproj */;
},
);
projectRoot = "";
targets = (
D2AAC07D0554694100DB518D /* OTRestFramework */,
@@ -120,16 +297,43 @@
};
/* End PBXProject section */
/* Begin PBXReferenceProxy section */
3F4E18DB102DD31E00320118 /* libElementParser.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = libElementParser.a;
remoteRef = 3F4E18DA102DD31E00320118 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
/* End PBXReferenceProxy section */
/* Begin PBXSourcesBuildPhase section */
D2AAC07B0554694100DB518D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3F4E18F3102DD38800320118 /* OTRestClient.m in Sources */,
3F4E18F5102DD38800320118 /* OTRestParams.m in Sources */,
3F4E18F7102DD38800320118 /* OTRestParamsAttachment.m in Sources */,
3F4E18F9102DD38800320118 /* OTRestParamsDataAttachment.m in Sources */,
3F4E18FB102DD38800320118 /* OTRestParamsFileAttachment.m in Sources */,
3F4E18FD102DD38800320118 /* OTRestRequest.m in Sources */,
3F4E1900102DD38800320118 /* OTRestResponse.m in Sources */,
3F4E191B102DD42F00320118 /* NSDictionary+OTRestRequestSerialization.m in Sources */,
3F4E194B102DD4C900320118 /* Element+OTRestAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXTargetDependency section */
3F4E18DD102DD32A00320118 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
name = ElementParser;
targetProxy = 3F4E18DC102DD32A00320118 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin XCBuildConfiguration section */
1DEB921F08733DC00010E9CD /* Debug */ = {
isa = XCBuildConfiguration;

69
OTRestParams.h Normal file
View File

@@ -0,0 +1,69 @@
//
// OTRestParams.h
// gateguru
//
// Created by Blake Watters on 8/3/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OTRestRequestSerializable.h"
#import "OTRestParamsFileAttachment.h"
#import "OTRestParamsDataAttachment.h"
@interface OTRestParams : NSObject <OTRestRequestSerializable> {
NSMutableDictionary* _valueData;
NSMutableDictionary* _fileData;
}
/**
* Returns an empty params object ready for population
*/
+ (OTRestParams*)params;
/**
* Initialize a params object from a dictionary of key/value pairs
*/
+ (OTRestParams*)paramsWithDictionary:(NSDictionary*)dictionary;
/**
* Initalize a params object from a dictionary of key/value pairs
*/
- (OTRestParams*)initWithDictionary:(NSDictionary*)dictionary;
/**
* Sets the value for a named parameter
*/
- (void)setValue:(id <NSObject>)value forParam:(NSString*)param;
/**
* Sets the value for a named parameter to the data contained in a file at the given path
*/
- (OTRestParamsFileAttachment*)setFile:(NSString*)filePath forParam:(NSString*)param;
/**
* Sets the value for a named parameter to the data contained in a file at the given path with the specified MIME Type and attachment file name
*/
- (OTRestParamsFileAttachment*)setFile:(NSString*)filePath MIMEType:(NSString*)MIMEType fileName:(NSString*)fileName forParam:(NSString*)param;
/**
* Sets the value to the data object for a named parameter
*/
- (OTRestParamsDataAttachment*)setData:(NSData*)data forParam:(NSString*)param;
/**
* Sets the value for a named parameter to a data object with the specified MIME Type and attachment file name
*/
- (OTRestParamsDataAttachment*)setData:(NSData*)data MIMEType:(NSString*)MIMEType fileName:(NSString*)fileName forParam:(NSString*)param;
/**
* Returns the value for the Content-Type header for these params
*/
- (NSString*)ContentTypeHTTPHeader;
/**
* Returns the data contained in this params object as a MIME string
*/
- (NSData*)HTTPBody;
@end

139
OTRestParams.m Normal file
View File

@@ -0,0 +1,139 @@
//
// OTRestParams.m
// gateguru
//
// Created by Blake Watters on 8/3/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParams.h"
static NSString* kOTRestStringBoundary = @"0xKhTmLbOuNdArY";
@implementation OTRestParams
+ (OTRestParams*)params {
OTRestParams* params = [[[OTRestParams alloc] init] autorelease];
return params;
}
+ (OTRestParams*)paramsWithDictionary:(NSDictionary*)dictionary {
OTRestParams* params = [[[OTRestParams alloc] initWithDictionary:dictionary] autorelease];
return params;
}
- (id)init {
if (self = [super init]) {
_valueData = [[NSMutableDictionary alloc] init];
_fileData = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[_valueData release];
[_fileData release];
[super dealloc];
}
- (OTRestParams*)initWithDictionary:(NSDictionary*)dictionary {
if (self = [super init]) {
_valueData = [[NSMutableDictionary dictionaryWithDictionary:dictionary] retain];
_fileData = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)setValue:(id <NSObject>)value forParam:(NSString*)param {
[_valueData setObject:value forKey:param];
}
- (OTRestParamsFileAttachment*)setFile:(NSString*)filePath forParam:(NSString*)param {
OTRestParamsFileAttachment* attachment = [OTRestParamsFileAttachment attachment];
attachment.filePath = filePath;
[_fileData setObject:attachment forKey:param];
return attachment;
}
- (OTRestParamsFileAttachment*)setFile:(NSString*)filePath MIMEType:(NSString*)MIMEType fileName:(NSString*)fileName forParam:(NSString*)param {
OTRestParamsFileAttachment* attachment = [self setFile:filePath forParam:param];
if (MIMEType != nil) {
attachment.MIMEType = MIMEType;
}
if (fileName != nil) {
attachment.fileName = fileName;
}
return attachment;
}
- (OTRestParamsDataAttachment*)setData:(NSData*)data forParam:(NSString*)param {
OTRestParamsDataAttachment* attachment = [OTRestParamsDataAttachment attachment];
attachment.data = data;
[_fileData setObject:attachment forKey:param];
return attachment;
}
- (OTRestParamsDataAttachment*)setData:(NSData*)data MIMEType:(NSString*)MIMEType fileName:(NSString*)fileName forParam:(NSString*)param {
OTRestParamsDataAttachment* attachment = [self setData:data forParam:param];
if (MIMEType != nil) {
attachment.MIMEType = MIMEType;
}
if (fileName != nil) {
attachment.fileName = fileName;
}
return attachment;
}
- (NSData*)endItemBoundary {
return [[NSString stringWithFormat:@"\r\n--%@\r\n", kOTRestStringBoundary] dataUsingEncoding:NSUTF8StringEncoding];
}
- (void)addValuesToHTTPBody:(NSMutableData*)HTTPBody {
int i = 0;
for (id key in _valueData) {
id value = [_valueData valueForKey:key];
[HTTPBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[HTTPBody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
i++;
// Only add the boundary if this is not the last item in the post body
if (i != [_valueData count] || [_fileData count] > 0) {
[HTTPBody appendData:[self endItemBoundary]];
}
}
}
- (void)addAttachmentsToHTTPBody:(NSMutableData*)HTTPBody {
int i = 0;
for (id key in _fileData) {
OTRestParamsAttachment* attachment = (OTRestParamsAttachment*) [_fileData valueForKey:key];
[HTTPBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, attachment.fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[HTTPBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", attachment.MIMEType] dataUsingEncoding:NSUTF8StringEncoding]];
[attachment writeAttachmentToHTTPBody:HTTPBody];
i++;
if (i != [_fileData count]) {
[HTTPBody appendData:[self endItemBoundary]];
}
}
}
- (NSData*)HTTPBody {
NSMutableData* HTTPBody = [NSMutableData data];
[HTTPBody appendData:[[NSString stringWithFormat:@"--%@\r\n", kOTRestStringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[self addValuesToHTTPBody:HTTPBody];
[self addAttachmentsToHTTPBody:HTTPBody];
[HTTPBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", kOTRestStringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
return HTTPBody;
}
- (NSString*)ContentTypeHTTPHeader {
return [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kOTRestStringBoundary];
}
@end

38
OTRestParamsAttachment.h Normal file
View File

@@ -0,0 +1,38 @@
//
// OTRestParamsAttachment.h
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface OTRestParamsAttachment : NSObject {
NSString* _fileName;
NSString* _MIMEType;
}
/**
* The name of the attached file in the MIME stream
* Defaults to 'file' if not specified
*/
@property (nonatomic, retain) NSString* fileName;
/**
* The MIME type of the attached file in the MIME stream
* Defaults to 'application/octet-stream' if not specified
*/
@property (nonatomic, retain) NSString* MIMEType;
/**
* Returns an autoreleased attachment object
*/
+ (id)attachment;
/**
* Abstract method implementing writing this attachment into an HTTP stream
*/
- (void)writeAttachmentToHTTPBody:(NSMutableData*)HTTPBody;
@end

38
OTRestParamsAttachment.m Normal file
View File

@@ -0,0 +1,38 @@
//
// OTRestAttachment.m
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParamsAttachment.h"
@implementation OTRestParamsAttachment
@synthesize fileName = _fileName, MIMEType = _MIMEType;
+ (id)attachment {
return [[[self alloc] init] autorelease];
}
- (id)init {
if (self = [super init]) {
self.fileName = @"file";
self.MIMEType = @"application/octet-stream";
}
return self;
}
- (void)dealloc {
[_fileName release];
[_MIMEType release];
[super dealloc];
}
- (void)writeAttachmentToHTTPBody:(NSMutableData*)HTTPBody {
[self doesNotRecognizeSelector:_cmd];
}
@end

View File

@@ -0,0 +1,20 @@
//
// OTRestParamsDataAttachment.h
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParamsAttachment.h"
@interface OTRestParamsDataAttachment : OTRestParamsAttachment {
NSData* _data;
}
/**
* The data being attached to the MIME stream
*/
@property (nonatomic, retain) NSData* data;
@end

View File

@@ -0,0 +1,27 @@
//
// OTRestParamsDataAttachment.m
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParamsDataAttachment.h"
@implementation OTRestParamsDataAttachment
@synthesize data = _data;
- (void)dealloc {
[_data release];
[super dealloc];
}
- (void)writeAttachmentToHTTPBody:(NSMutableData*)HTTPBody {
if ([_data length] == 0) {
return;
}
[HTTPBody appendData:_data];
}
@end

View File

@@ -0,0 +1,20 @@
//
// OTRestParamsFileAttachment.h
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParamsAttachment.h"
@interface OTRestParamsFileAttachment : OTRestParamsAttachment {
NSString* _filePath;
}
/**
* The path to this file attachment on disk
*/
@property (nonatomic, retain) NSString* filePath;
@end

View File

@@ -0,0 +1,35 @@
//
// OTRestParamsFileAttachment.m
// gateguru
//
// Created by Blake Watters on 8/6/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestParamsFileAttachment.h"
@implementation OTRestParamsFileAttachment
@synthesize filePath = _filePath;
- (void)dealloc {
[_filePath release];
[super dealloc];
}
- (void)writeAttachmentToHTTPBody:(NSMutableData*)HTTPBody {
NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:_filePath] autorelease];
[stream open];
int bytesRead;
while ([stream hasBytesAvailable]) {
unsigned char buffer[1024*256];
bytesRead = [stream read:buffer maxLength:sizeof(buffer)];
if (bytesRead == 0) {
break;
}
[HTTPBody appendData:[NSData dataWithBytes:buffer length:bytesRead]];
}
[stream close];
}
@end

88
OTRestRequest.h Normal file
View File

@@ -0,0 +1,88 @@
//
// OTRestRequest.h
// gateguru
//
// Created by Jeremy Ellison on 7/27/09.
// Copyright 2009 Objective3. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "DocumentRoot.h"
#import "OTRestRequestSerializable.h"
@interface OTRestRequest : NSObject {
NSURL* _URL;
NSMutableURLRequest* _URLRequest;
NSDictionary* _additionalHTTPHeaders;
NSObject<OTRestRequestSerializable>* _params;
id _delegate;
SEL _callback;
}
@property(nonatomic, readonly) NSURL* URL;
/**
* The NSMutableURLRequest being sent for the Restful request
*/
@property(nonatomic, readonly) NSMutableURLRequest* URLRequest;
/**
* The HTTP Method used for this request
*/
@property(nonatomic, readonly) NSString* HTTPMethod;
/**
* The delegate to inform when the request is completed
*/
@property(nonatomic, retain) id delegate;
/**
* The selector to invoke when the request is completed
*/
@property(nonatomic, assign) SEL callback;
/**
* A Dictionary of additional HTTP Headers to send with the request
*/
@property(nonatomic, retain) NSDictionary* additionalHTTPHeaders;
/**
* A serializable collection of parameters sent as the HTTP Body of the request
*/
@property(nonatomic, readonly) NSObject<OTRestRequestSerializable>* params;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Return a REST request that is ready for dispatching
*/
+ (OTRestRequest*)requestWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback;
/**
* Initialize a REST request and prepare it for dispatching
*/
- (id)initWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback;
/**
* GET the resource and invoke the callback with the response payload
*/
- (void)get;
/**
* POST a collection of params to the resource and invoke the callback with the response payload
*/
- (void)postParams:(NSObject<OTRestRequestSerializable>*)params;
/**
* PUT a collection of params to the resource and invoke the callback with the response payload
*/
- (void)putParams:(NSObject<OTRestRequestSerializable>*)params;
/**
* DELETE the resource and invoke the callback with the response payload
*/
- (void)delete;
@end

96
OTRestRequest.m Normal file
View File

@@ -0,0 +1,96 @@
//
// OTRestRequest.m
// gateguru
//
// Created by Jeremy Ellison on 7/27/09.
// Copyright 2009 Objective3. All rights reserved.
//
#import "OTRestRequest.h"
#import "OTRestResponse.h"
#import "NSDictionary+OTRestRequestSerialization.h"
@implementation OTRestRequest
@synthesize URL = _URL, URLRequest = _URLRequest, delegate = _delegate, callback = _callback, additionalHTTPHeaders = _additionalHTTPHeaders,
params = _params;
+ (OTRestRequest*)requestWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:URL delegate:delegate callback:callback];
[request autorelease];
return request;
}
- (id)initWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback {
if (self = [self init]) {
_URL = [URL retain];
_URLRequest = [[NSMutableURLRequest alloc] initWithURL:_URL];
_delegate = [delegate retain];
_callback = callback;
}
return self;
}
- (void)dealloc {
[_URL release];
[_URLRequest release];
[_delegate release];
[_params release];
[_additionalHTTPHeaders release];
[super dealloc];
}
- (NSString*)HTTPMethod {
return [_URLRequest HTTPMethod];
}
- (void)addHeadersToRequest {
NSString* header;
for (header in _additionalHTTPHeaders) {
[_URLRequest setValue:[_additionalHTTPHeaders valueForKey:header] forHTTPHeaderField:header];
}
if (_params != nil) {
[_URLRequest setValue:[_params ContentTypeHTTPHeader] forHTTPHeaderField:@"Content-Type"];
}
NSLog(@"Headers: %@", [_URLRequest allHTTPHeaderFields]);
}
- (void)send {
NSString* body = [[NSString alloc] initWithData:[_URLRequest HTTPBody] encoding:NSUTF8StringEncoding];
NSLog(@"Sending %@ request to URL %@. HTTP Body: %@", [self HTTPMethod], [[self URL] absoluteString], body);
[body release];
OTRestResponse* response = [[OTRestResponse alloc] initWithRestRequest:self];
[[NSURLConnection connectionWithRequest:_URLRequest delegate:response] retain];
}
- (void)get {
[_URLRequest setHTTPMethod:@"GET"];
[self addHeadersToRequest];
[self send];
}
- (void)postParams:(NSObject<OTRestRequestSerializable>*)params {
[_URLRequest setHTTPMethod:@"POST"];
_params = [params retain];
[_URLRequest setHTTPBody:[_params HTTPBody]];
[self addHeadersToRequest];
[self send];
}
- (void)putParams:(NSObject<OTRestRequestSerializable>*)params {
[_URLRequest setHTTPMethod:@"PUT"];
_params = [params retain];
[_URLRequest setHTTPBody:[_params HTTPBody]];
[self addHeadersToRequest];
[self send];
}
- (void)delete {
[_URLRequest setHTTPMethod:@"DELETE"];
[self addHeadersToRequest];
[self send];
}
@end

View File

@@ -0,0 +1,26 @@
//
// OTRestRequestSerializable.h
// gateguru
//
// Created by Blake Watters on 8/3/09.
// Copyright 2009 Objective 3. All rights reserved.
//
/*
* This protocol is implemented by objects that can be serialized into a representation suitable
* for transmission over a REST request. Suitable serializations are x-www-form-urlencoded and
* multipart/form-data.
*/
@protocol OTRestRequestSerializable
/**
* The value of the Content-Type header for the HTTP Body representation of the serialization
*/
- (NSString*)ContentTypeHTTPHeader;
/**
* An NSData representing the HTTP Body serialization of the object implementing the protocol
*/
- (NSData*)HTTPBody;
@end

69
OTRestResponse.h Normal file
View File

@@ -0,0 +1,69 @@
//
// OTRestResponse.h
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OTRestRequest.h"
#import "DocumentRoot.h"
@interface OTRestResponse : NSObject {
OTRestRequest* _request;
NSHTTPURLResponse* _httpURLResponse;
NSMutableData* _payload;
}
/**
* The request that generated this response
*/
@property(nonatomic, readonly) OTRestRequest* request;
/**
* The URL the response was loaded from
*/
@property(nonatomic, readonly) NSURL* URL;
/**
* The MIME Type of the response payload
*/
@property(nonatomic, readonly) NSString* MIMEType;
/**
* The status code of the HTTP response
*/
@property(nonatomic, readonly) NSInteger statusCode;
/**
* Return a dictionary of headers sent with the HTTP response
*/
@property(nonatomic, readonly) NSDictionary* allHeaderFields;
/**
* The data returned as the response payload
*/
@property(nonatomic, readonly) NSData* payload;
/**
* Initialize a new response object for a REST request
*/
- (OTRestResponse*)initWithRestRequest:(OTRestRequest*)request;
/**
* Return the localized human readable representation of the HTTP Status Code returned
*/
- (NSString*)localizedStatusCodeString;
/**
* Return the response payload as an NSString
*/
- (NSString*)payloadString;
/**
* Parse the response payload into an XML Document via ElementParser
*/
- (DocumentRoot*)payloadXMLDocument;
@end

79
OTRestResponse.m Normal file
View File

@@ -0,0 +1,79 @@
//
// OTRestResponse.m
// gateguru
//
// Created by Blake Watters on 7/28/09.
// Copyright 2009 Objective 3. All rights reserved.
//
#import "OTRestResponse.h"
@implementation OTRestResponse
@synthesize payload = _payload, request = _request;
- (id)init {
if (self = [super init]) {
_payload = [[NSMutableData alloc] init];
}
return self;
}
- (id)initWithRestRequest:(OTRestRequest*)request {
if (self = [self init]) {
_request = [request retain];
}
return self;
}
- (void)dealloc {
[_httpURLResponse release];
[_payload release];
[_request release];
[super dealloc];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_payload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
_httpURLResponse = [response retain];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
[[_request delegate] performSelector:[_request callback] withObject:self];
}
- (NSString*)localizedStatusCodeString {
return [NSHTTPURLResponse localizedStringForStatusCode:[self statusCode]];
}
- (NSString*)payloadString {
return [[[NSString alloc] initWithData:_payload encoding:NSUTF8StringEncoding] autorelease];
}
- (DocumentRoot*)payloadXMLDocument {
return [DocumentRoot parseXML:[self payloadString]];
}
- (NSURL*)URL {
return [_httpURLResponse URL];
}
- (NSString*)MIMEType {
return [_httpURLResponse MIMEType];
}
- (NSInteger)statusCode {
return [_httpURLResponse statusCode];
}
- (NSDictionary*)allHeaderFields {
return [_httpURLResponse allHeaderFields];
}
@end