Consuming RESTful API with RESTframework

As explained in previous post, we’re going to demonstrate how RESTframework handles REST web services. I’ll be consuming a simple Django demo web service created in previous post, so either grab the sources or read the post and build it yourself. Assuming you have this service running on localhost, port 8000, we can continue.

First, download RESTframework from GitHub. With git client, simply type in terminal:

$ git clone git@github.com:ivasic/RESTframework.git

The quickest way to get RESTframework into your project is just including all the files from RFClasses  folder. RF does not have any external dependencies so building shouldn’t be a problem.

RFClasses

RFClasses

Now, we’ll also need JSONKit to parse API response and MBProgressHUD to show some progress. Grab both from GitHub and add to your project.

Fetching objects list

We’ll have 2 view controllers in our demo app. One will be a simple object list and the other one will be, even more simple, view controller for creating a new object. First, we’re going to show how to fetch the object list. We’ll need a GET request to /objects/ URL and we’ll need to parse the JSON array we get in response and show in the table view.

RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/"] type:RFRequestMethodGet resourcePathComponents:@"objects", nil];
[MBProgressHUD showHUDAddedTo:self.view animated:YES].labelText = NSLocalizedString(@"Loading...", @"");
[RFService execRequest:r completion:^(RFResponse* response) {
	[MBProgressHUD hideHUDForView:self.view animated:YES];
 
	if (response.error) {
		UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"") message:response.error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[aiv show];
		return;
	}
 
	self.dataSource = [response.dataValue objectFromJSONData];
	[self.tableView reloadData];
}];

So, what we’re doing in this code snippet is:

  • Creating a GET RFRequest with appropriate URL and resource path
  • Attaching MBProgressHUD to our view to show spinner progress
  • Executing RFRequest and defining the block to execute after RFResponse is received
  • Inside this block, we simply check for errors, parse the response, add to our tableView dataSource and reloading the table
You can see all this in action in the project sample attached to this blog post (at the bottom).

Creating new objects

Our view controller for creating new objects is responsible for creating POST requests to /objects/ URL. Again, very simple, you just need to do this:

RFRequest* r = [RFRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/"] type:RFRequestMethodPost resourcePathComponents:@"objects", @"", nil];
 
[r addParam:self.txtName.text forKey:@"name"];
[r addParam:self.lblDate.text forKey:@"date"];
 
[MBProgressHUD showHUDAddedTo:self.view animated:YES].labelText = NSLocalizedString(@"Submitting...", @"");
[RFService execRequest:r completion:^(RFResponse* response) {
	[MBProgressHUD hideHUDForView:self.view animated:YES];
 
	if (response.error) {
		UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"") message:response.error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[aiv show];
		return;
	}
 
	UIAlertView* aiv = [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
	[aiv show];
 
	[self.navigationController popViewControllerAnimated:YES];
}];

Similar to our previous snippet, but this time we also add some parameters to the request – name and date strings.

This is a brief overview with sample Xcode project of how RESTframework consumes RESTful web APIs. Questions welcomed via comments as usual.

 

Download project: RESTframework demo Xcode project download

Reusing code with Snippets for Mac

I was never a fan of any snippets tools for development, it may be because I never came across any descent app and maybe I didn’t even need one. One thing is sure, while coding, I always have several projects opened and I’m copy/pasting code like crazy all over them. So I guess I do need one after all. Recently I stumbled upon a very nice app called Snippets for Mac and I decided to give it a try.

What I need from this kind of app is:

  1. to integrate nicely with my Mac OSX desktop
  2. have a clean and nice GUI
  3. provide the best user experience

These are 3 most important features I need and the last one is crucial. Snippets app manages to get good ratings here. Snippets app does everything you’d expect from this kind of app: searching, syncing, easy access etc..

Snippets app has a very slick interface as you would expect from a modern Mac OSX app. Main window has a library tree view, along with the highlights and groups on the left and code editor on the right. GUI is pretty much straight-forward and you can easily add new snippets or select existing ones.

Snippets Main Window

Snippets app main window

Editing snippets is just as easy and what I like about it is that you can use different editors launching from within the app. From toolbar, you can choose whether you want to edit the snippet in XCode, Coda, TextEdit or other editors of your choice.

Choose editor for a snippet

Choose editor for a snippet

Now, ‘integration’ with the actual editors/IDEs where one does coding is the most important part. This is where snippets bought me. While it doesn’t integrate with IDEs per se (you can easily copy code from its main window), it’s much easier and quicker to do it from the tray icon either by clicking on it or using a global shortcut which is quicker and you can insert a snippet without loosing focus in your code editor. Either by clicking the tray icon or using a global shortcut,  you quickly get a list of search results and you can add the code momentarily.

Adding snippets

Inserting snippets

Another great feature is that highlighting a search result shows a tooltip with the code that snippet holds. This is very useful because sometimes I name snippets similarly or even forget what it has.

The app does satisfy my needs as a simple snippet manager and provides me simpler way of reusing my code than to browse around my old projects and copy/pasting code. I’m still in my trial period and deciding whether or not this software is worth the money ($39,95 at the moment, comes with 30 day trial) but it is being useful no doubt about it. I suggest you take it for a spin and see if it fits you.

Snippets website / download

Snippets for Mac