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

Building RESTful API with Django

In order to demonstrate how RESTframework for Cocoa works, I’m going to create a small RESTful API demo in Django/Python and use that as a demo web service we’re gonna query from our soon-to-be demo iOS app. Hopefully, you can also pick up a thing or two from this tutorial as well since Django seems to be a very powerful framework for building RESTful APIs with. If you don’t want to bother with Django, you can simply download the project (see the end of this post) and run it as a demo web service. Next post will explain how to use my RESTframework to consume this web service.

Requirements

You’ll need Python, of course, and I’ve tested this on v2.7 although I’m pretty sure it should also work on v2.6 and possibly v2.5. Then you’ll need django and djangorestframework both available for install with pip or easy_install.

Django project

Create a new django project and the first django app.

$ django-admin.py startproject DjangoTest
$ cd DjangoTest
$ django-admin.py startapp api

Now we need to modify our initial project settings. For the sake of simplicity, I’ll be using SQLite3 database since it doesn’t require any installation or setup. To do this, edit settings.py in your project dir.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'database.db',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

Another thing we need to do in settings.py is to add your app and djangorestframework to the installed apps section.

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'DjangoTest.api',
    'djangorestframework',
)

Okay, now let’s run and test if it worked.

$ python manage.py runserver

Now go to http://localhost:8000/ and make sure you see “It worked” message.

Next step is creating some resources for our RESTful API. I’ve created a simple model to hold a name and creation date. Looks like this:

class Object(models.Model):
	name = models.CharField(max_length=50)
	date = models.DateTimeField()

We’re almost there. We need to sync our database now after we’ve created the model (do this every time you change the model, see more info here)

$ python manage.py syncdb

RESTful API using django-rest-framework

Now our django project is ready. Using djangorestframework we will add RESTful API interface for our ‘api’ app. Assuming you’ve installed it correctly, all we need to do is to create another python file where we will store our resources. Name it resources.py and put this inside:

from djangorestframework.resources import ModelResource
from api.models import Object
 
class ObjectResource(ModelResource):
	model = Object
	fields = ('name', 'date')

When this is done, we need to add some URLs where our resources will be located. I’m using nifty views provided by djangorestframework which work with your models without a single line of code. You get 2 URLs for viewing a list of objects (or creating a new one) and one for viewing your object details. urls.py looks like this:

from django.conf.urls.defaults import patterns, include, url
 
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
from api.resources import ObjectResource
 
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'DjangoTest.views.home', name='home'),
    # url(r'^DjangoTest/', include('DjangoTest.foo.urls')),
 
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
 
    url(r'^objects/$', ListOrCreateModelView.as_view(resource=ObjectResource), name='object-root'),
    url(r'^objects/(?P[^/]+)/$', InstanceModelView.as_view(resource=ObjectResource), name='object'),
)

That’s it. Save and run your project and you’ll be able to access your resources at http://localhost:8000/objects/
For example, querying an URL like this one (providing you added an object with id=1) http://localhost:8000/objects/1/?_accept=application/json would result in this:

{"date": "2011-11-28 09:38:21", "name": "Test"}

Download sample project

Help yourself with the sources for this project ready to unzip & run. You can download the project here – DjangoRESTfulAPI.

REST framework for iOS is now Open Source

I have (finally) open sourced my RESTframework project on GitHub. It aims for simplicity of usage primarily. Querying RESTful web services is really easy now. Supported are iOS versions 4.0 and above and Mac OS 10.6 and above.

Feel free to fork & contribute!

RESTframework on GitHub

XCode 4 themes

I was searching for some cool XCode 4 themes I could use cause the default ones are pretty boring so I thought I’d share what I found with my readers.

My favorite (for now) is the Spacedust theme by m5h

Some other really interesting themes:

If you have an ‘old’ XCode 3 theme you’d like to use with XCode 4, I’m sure this will help.

Cleaning up XCode 4 user data from VCS (git)

If you use a version control system for your XCode 4 projects (and if you don’t I suggest you start) you’d probably want to keep your VCS state clean and omit committing user specific settings/files that XCode automatically creates. With XCode 3 you were probably adding *.mode1v3 and *.pbxuser files and now the .xcodeproj structure has changed so you need to modify your source control ignore files to include these new files. This simple how-to will use Git as an example but the logic applies to almost any other version control system.
XCode 4 project file now stores a special folder called “xcuserdata” amongst other folders (“xcshareddata” to name one). While xcuserdata contains user specific files that you probably want to ignore, xcshareddata contains project settings that are project specific and does not relate to users so it’s a good idea to keep that one. If you decide to delete that one, the worst thing that will happen is that you will need to recreate your project schemes. So, to keep your current build schemes pay attention to the order of the things I’m going to do.

Make your schemes “shared”

If you don’t want to lose your schemes, first you need to make them shared so you can safely ignore/delete your personal settings. To do this, go to Editor->Edit Schemes… and from the opened dialog choose “Manage Schemes”. >

XCode Edit schemes

Edit Schemes

 

Shared Scheme

Shared Scheme

Add user specific filename patterns to ignore files

Now you can ignore all those files. To do this, you simply add patterns to the .gitignore file (or your vcs of choice alternative). With Git we can do this:

echo *.mode1v3 > .gitignore
echo *.pbxuser >> .gitignore
echo *.xcuserdata >> .gitignore
echo .DS_store >> .gitignore

The first two are XCode 3 specific so if you don’t plan on using it – you can ignore these 2. The next file is XCode 4 specific and lastly I added OSX noise files as well. And that’s about it for this simple post, hope someone finds it useful.

Looking for a partner to promote a math iPhone game

Having lots of other projects gives me really little time to work on some of my own projects, specially when it comes to promoting already built apps. Thus I decided to partner up with someone to help promote an existing iPhone game. The project is really interesting and fun, so far we have 100% positive responses but the game just won’t take off without help which I don’t have time to do.

So, if you’re good in promoting iOS games, drop me a note and I’ll provide more details.

How to use Dropbox as a Git repository

We all know the Git is a very powerful distributed source control system and it’s a pleasure to use it. I sometimes have the need to work on some projects locally only (no servers, no collaborators, nothing too funky) so in order to keep track of my code changes I was using a local git repo which works just flawless. But having multiple computers this sometimes can be a pain, so you need to copy the latest version around which is not so cool. Dropbox (or any similar cloud solution) comes to the rescue. I’ll show you how easy it is to have Dropbox act as a Git server making it your personal code repo sync point.

First we need to create a bare git repo inside your Dropbox folder. I have created a subdirectory specially for my git repos named, well, “git” :)

$cd ~/Dropbox/Git
$mkdir Example.git
$cd Example.git
$git init --bare

And the first part is finished. We now have a “remote” bare git repo ready to be cloned locally.

$cd ~/Projects
$git clone ~/Dropbox/Git/Example.git

And you’re done. Your local git is ready for the initial commit and when you want to sync the repo, you just do a “git push” as you’d normally do. All other computers are now able to pull the changes so you can work on multiple machines without a hassle.

Warning

Everything is not so pink, though. You need to be extra careful when pushing the changes. I’m using this approach on my own, so no collaborators. If you plan to have multiple people pushing to the repo at the same time (or you are pushing from multiple computers, in some crazy scenario) Dropbox might mix your changes and you can end up with the garbage data in the git repo. So, I guess this is really useful for 1 person only or for a really, really careful team of people.

There are also some discussions that are advising against this saying that the Dropbox can make a mess out of your git data. I haven’t heard of any case where this did happen but keep this in mind for sensitive projects. Also, feel free to let me know if anyone runs into issues.

RESTful web service, API with ASP MVC 2

In this very simple tutorial, I’ll show how to build a basic RESTful API with Microsoft ASP MVC 2 later to be used with various clients (e.g. iOS application). This is a very basic level tutorial but it will show how easy it is to build powerful REST services in the matter of minutes.

The first thing to do is to create a new MVC 2 project, as simple as shown on the screenshot.

Create new MVC 2 project

Create new MVC 2 project

Now, I have deleted the Account controller and all the views cause I don’t need it for this example, but you don’t have to. The key concept to understand here is pretty simple – instead of returning back the Views as action results, we return JSON. MVC already provides HTTP verbs such as GET, POST, PUT and DELETE so all we really need to do is prepare our data and (de)encode it as JSON and we’re done. Fortunately enough, MVC also provides built-in JsonResult which inherits ActionResult which makes our job even easier. A simple GET action that returns a “Hello World” might look like this:

[HttpGet]
public ActionResult Index()
{
	return Json("Hello World!", JsonRequestBehavior.AllowGet);
}

There you go, a simple GET method is created. Now if you want to return something more complex, you can pass any serializable object to the Json() method and return that back. Here’s an array/dictionary combination.

[HttpGet]
public ActionResult Array()
{
	List<object> objects = new List<object>();						
	Dictionary dict = new Dictionary();
	dict.Add("key1", "value1");
	dict.Add("key2", "value2");
	objects.Add("item1");
	objects.Add("item2");
	objects.Add(dict);
	return Json(objects, JsonRequestBehavior.AllowGet);
}

This produces a JSON like this:

["item1","item2",{"key1":"value1","key2":"value2"}]

Ok, so how about other REST concepts like PUT, POST or DELETE? Easy as well. Define your action and just place a HTTP verb attribute like this example:

[HttpPut]
[ActionName("Index")]
public ActionResult IndexPut(string key, string value)
{
	Dictionary dict = new Dictionary();
	System.Web.Caching.Cache c = HttpContext.Cache;
	if (key == null || value == null)
	{
		dict.Add("result", "failed");
		dict.Add("message", string.Format("No key or no value provided"));
	}
	else
	{
		if (c.Get(key) == null)
		{
			c.Add(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
			dict.Add("result", "ok");
			dict.Add("message", string.Format("You have inserted {0} for key {1}", c[key], key));
		}
		else
		{
			dict.Add("result", "failed");
			dict.Add("message", string.Format("Value for key {0} already exists. Use POST to modify the value", key));
		}
	}
	return Json(dict);
}

The example above takes 2 input parameters named key and value (both strings but can be any object) and puts them into HTTP Cache. It also prevents to PUT another value with the same key. The project source code has other methods as well (POST and DELETE) used to modify the values and delete values for keys. Take a look on how that is implemented. This is very basic stuff so I won’t cover those methods here but feel free to ask in comments if something is unclear.

This REST example will be used for future posts covering REST integration in iOS, so you can keep it somewhere for testing. The project source code can be downloaded here: ASP MVC 2 REST service example

One post a day at Dizzey.com. Promise.

Hello everyone!

I’ve decided to do at least one post a day on Dizzey.com. That’s a promise! It’s what’s going to happen! So stay tuned for great new posts here in the future!

Enjoy April the 1st!

 

PS: In all seriousness, I’m planning to cover a bit bigger topic about RESTful webservices and their integration with iOS. I developed a small RESTful framework to help me with some projects that I decided to post on GitHub as an open source project so you can enjoy and use it as well. It’s based on simplicity and communicating with the services in 1-2 lines of code. Stay tuned!

Some XCode 4 tips

Apple kind of surprised me with releasing the XCode 4 along with the iOS 4.3. Honestly, I wasn’t expecting it until later this year or at least until iOS 5 was announced. But I’m glad it’s out, of course, and I already switched to using it vs XCode 3. I held back from trying out the beta versions so the experience was really exciting to me when I first tried it. Completely different experience compared to version 3.x and finally a single, fullscreen window. Pretty cool.

Anyway, I’m not going to review it as there are lots of reviews, I’m just going to point out a few things that I ran into while testing the last few days.

How to enable LLVM in XCode 4

By default, XCode uses a GCC kind of wrapper for LLVM so you’ll probably want to enable LLVM compiler and you can do that easily enough in build settings like so.

Now that you enabled that naturally you want to enable LLDB too. LLDB is a debugger, it’s to LLVM what GDB is to GCC. To enable LLDB you need to choose “Edit Scheme” from the “Product” menu and for the “Run” phase you’ll see a debugger chooser dropdown. However, at this point for iOS projects you’re stuck with GDB as the LLDB is only available for Mac apps and not available for iOS, yet.

How do I add existing frameworks to the XCode 4 projects?

“Add the existing framework…” right click menu option is gone. So how do you add existing frameworks?! The option is there of course, it’s just a bit hidden. The trick is to open target settings and navigate to the “Build Phases” tab. There’s a section that says “Link binary with Libraries” and that’s our X. See below.

Add existing frameworks

Add existing frameworks

So, should I switch to XCode 4?

Yup. I was in doubt and thought it might be a good idea to hold off of it for a while, at least until the next version but I tried it and found nothing that stops me from using it. In fact, I’m so pleased I’m having a hard time coding some legacy projects in XCode 3. I would suggest, though, to keep the version 3.x and install XCode 4 in a separate directory just in case…

UPDATE:

Where do I set NSZombieEnabled?

Got another tip here, it’s moved to a different place now. You need to choose “Edit Scheme” from the “Product” menu and you’ll find it there.

Edit Scheme Xcode 4

Edit Scheme Xcode 4