HTTPRiot is a simple REST library designed to make interacting with REST services much easier. It supports GET, POST, PUSH and DELETE requests and HTTP Basic Authentication. HTTPRiot was inspired by John Nunemaker's excellent httparty Ruby library.
[HRRestModel getPath:@"/person.json" withOptions:nil object:nil];
NSDictionary *opts = [NSDictionary dictionaroyWithObject:[person JSONRepresentation] forKey:@"body"]; [HRRestModel postPath:@"/person" withOptions:opts object:nil];
NSDictionary *opts = [NSDictionary dictionaroyWithObject:[updatedPerson JSONRepresentation] forKey:@"body"]; [HRRestModel putPath:@"/person" withOptions:opts object:nil];
[HRRestModel deletePath:@"/person/1" withOptions:nil object:nil];
Although you can use HTTPRiot straight out of the box by itself, this approach has some pitfals. Every request will share the same configuation options. By subclassing HRRestModel you can have per-class configuation options meaning that all requests generating from your subclass share a local set of configuation options and will not affect other requests not originating from your subclass.
@implementation Tweet @synthesize screenName; @synthesize name; @synthesize text; @synthesize location; // Set default options here + (void)initialize { [self setDelegate:self]; [self setBaseURL:[NSURL URLWithString:@"http://twitter.com"]]; } - (void)initWithDictionary:(NSDictionary *)dict { if(self = [super init]) { [self setScreenName:[dict valueForKeyPath:@"user.screen_name"]]; [self setName:[dict valueForKeyPath:@"user.name"]]; [self setLocation:[dict valueForKeyPath:@"user.location"]]; [self setText:[dict valueForKey:@"text"]]; } return self; } // NOTE: The `delegate` object isn't neccissary here, but there is an important reason why // I'm using this approach. I want to give my controller fully initialized models instead of // raw NSDIctionary objects. On that note I pass the controller as the <tt>object</tt> to the // request so I can call back to it in the delegate methods handling the responses. + (id)timelineForUser:(NSString *)user delegate:(id)delegate { NSDictionary *params = [NSDictionary dictionaryWithObject:user forKey:@"screen_name"]; NSDictionary *opts = [NSDictionary dictionaryWithObject:params forKey:@"params"]; [self getPath:@"/statuses/user_timeline.json" withOptions:opts object:delegate]; } + (id)publicTimelineWithDelegate:(id)delegate { [self getPath:@"/statuses/public_timeline.json" withOptions:nil object:delegate]; } #pragma mark - HRRequestOperation Delegates + (void)restConnection:(NSURLConnection *)connection didFailWithError:(NSError *)error object:(id)object { // Handle connection errors. Failures to connect to the server, etc. } + (void)restConnection:(NSURLConnection *)connection didReceiveError:(NSError *)error response:(NSHTTPURLResponse *)response object:(id)object { // Handle invalid responses, 404, 500, etc. } + (void)restConnection:(NSURLConnection *)connection didReceiveParseError:(NSError *)error responseBody:(NSString *)string { // Request was successful, but couldn't parse the data returned by the server. } // Given I've passed the controller as the <tt>object</tt> here, I can call any method I want to on it // giving it a collection of models I've initialized. + (void)restConnection:(NSURLConnection *)connection didReturnResource:(id)resource object:(id)object { NSMutableArray *tweets = [[[NSMutableArray alloc] init] autorelease]; for(id item in resource) { [tweets addItem:[[Tweet alloc] initWithDictionary:item]]; } [object performSelector:@selector(tweetsLoaded:) withObject:tweets]; } @end