Wednesday, August 31, 2011

Example #13 - Simple queries using GET

Going a bit deeper into the API.  Let's look at code a few simple queries.  Some of this will look familiar.  We'll combine some of the queries we did in Example #10 with the code from Example #12 to create code snippets that could be used by a simple app.

The SODA API is a RESTful API.  You can read more about that on Wikipedia or one of many simple tutorials.

The SODA reference page for the Views API shows that all uses of this service are done with a GET.  When we open a URL without any parameters, that is a GET.  This is what we've done in all examples so far and will do in this example.

This talk about REST and GET is just background.  You may want to dig deeper and if you do, you'll start to care about these terms.

So, on with the example.  We're going to progressively add parameters to do different queries and show how the returned information differs.  The output from each request shows the number of views and the name of the view.

Example Code
A few overall comments:
  • I'm using a different style of importing.  When you import from a module, you don't have to put in the name of the module when you use a function.  In past examples, I would import urllib2 and then call urllib2.urlopen().  This example uses from urllib2 import urlopen and then calls urlopen().  It makes the code look a little cleaner.
  • If you are wondering about the actions of the code, look back at Example 12 and your favorite Python book or website.

from urllib2 import urlopen
from json import load


# 1 - Ask for all views
url = 'http://data.cityofchicago.org/api/views.json'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for all views.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']


# 2 - Ask for views with 'police' in the name
url = 'http://data.cityofchicago.org/api/views.json?name=police'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police\" in name.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']


# 3 - Ask for views with 'police station' in the name
url = 'http://data.cityofchicago.org/api/views.json?name=police+station'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police station\" in name.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']


# 4 - Ask for views with 'police' in the name but set a limit of just returning two views
url = 'http://data.cityofchicago.org/api/views.json?name=police&limit=2'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police\" in name and limit to two views.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']

Explanations of each snippet
For each of the four snippets, here's the few lines of code, an explanation, and the portion of the output generated by that snippet.

API request with no parameters
# 1 - Ask for all views
url = 'http://data.cityofchicago.org/api/views.json'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for all views.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']
This is a straightforward request from the views service to return all the views (aka datasets) in the city of Chicago data portal.  The output shows we get back the default of 50 views and lists their names.


API request with one parameter
# 2 - Ask for views with 'police' in the name
url = 'http://data.cityofchicago.org/api/views.json?name=police'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police\" in name.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']
This shows how to add one parameter by adding on the ? along with a name/value pair.  This time we get back the eight views that have the word police in their name.


API request with a multi-word parameter
# 3 - Ask for views with 'police station' in the name
url = 'http://data.cityofchicago.org/api/views.json?name=police+station'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police station\" in name.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']
The + character is treated as a space.  This is a request to search for police station in the name.  We get back three datasets.


API request with multiple parameters
# 4 - Ask for views with 'police' in the name but set a limit of just returning two views
url = 'http://data.cityofchicago.org/api/views.json?name=police&limit=2'
u = urlopen(url)
views = load(u)
numViews = len(views)
print '\nAsk for \"police\" in name and limit to two views.  Number of views returned = %s' % numViews
for view in views:
   print '   ', view['name']
Using the & character between name/value pairs, you can string together multiple parameters for your service request.  Here we ask for the views with police in the name but with a limit of returning just two views.  Instead of getting the eight views that have police in the name, we get just the first two.


Generalizing
You can use this type of code to make all the requests detailed on the reference card in Example #10.  Just plug in the URL you wish to use.  You can use the ? to string together name/value parameters shown on the reference card.  If any of your parameters have multiple words in the value, use the +.

No comments:

Post a Comment