UserPreferences

CarrotVsOrangeExample


Both sets of code implement the basic AtomAPI in Python. Both implementations are just CGI glue that maps the CGI interface to a lower level API in the Python module myLowLevelBloggingAPI.py. That module has four functions: create(content), get(id), update(id, content), delete(id).

Notes on the first two examples:

HTTP using GET/POST to a single URI

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
#!/usr/bin/python

import cgitb; cgitb.enable()
import os, cgi, urlparse, sys
import myLowLevelBloggingAPI
import xmltramp


method = os.environ['REQUEST_METHOD']
id = None

if method == 'POST':
   content = sys.stdin.read()
   d = xmltramp.parse(content)
   if d._name == 'delete':
      myLowLevelBloggingAPI.delete(d('id'))
   try:
      id = d.entry.id
   except:
      pass
   if id == None:
      id = myLowLevelBloggingAPI.create(sys.stdin.read())
      print "Status: 201 Created"
      print "Location: " + urlparse.urlunparse(('http', os.environ['SERVER_NAME'], os.environ['REQUEST_URI'] + '/%d', '', '', '')) + "\n" % (id, )
   else:
      myLowLevelBloggingAPI.update(id, content)
      print "Status: 205 Reset Content\n"
elif method == 'GET':
   id = os.environ.get('PATH_INFO', '')
   print "Status: 200 Ok\n"
   print myLowLevelBloggingAPI.get(id)

HTTP in the REST Architectural Style

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
#!/usr/bin/python

import cgitb; cgitb.enable()
import os, urlparse, sys
import myLowLevelBloggingAPI

method = os.environ['REQUEST_METHOD']
id = os.environ.get('PATH_INFO', '')

if method == 'POST':
   id = myLowLevelBloggingAPI.create(sys.stdin.read())
   print "Status: 201 Created"
   print "Location: " + urlparse.urlunparse(('http', os.environ['SERVER_NAME'], os.environ['REQUEST_URI'] + '/%d', '', '', '')) + "\n" % (id, )
elif method == 'GET':
   print "Status: 200 Ok\n"
   print myLowLevelBloggingAPI.get(id)
elif method == 'PUT':
   myLowLevelBloggingAPI.update(id, sys.stdin.read())
   print "Status: 205 Reset Content\n"
elif method == 'DELETE':
   myLowLevelBloggingAPI.delete(id)
   print "Status: 200 Ok\n"

Complete RESTful Server

[WWW]atom.cgi is a shell script that implements a RESTful server. CommonGatewayInterface, like most web server interfaces, does all the real work in providing the method (REQUEST_METHOD), the parsed and relatively safe portion of the URI (PATH_INFO), and any query parameters (QUERY_STRING).

atom.cgi supports entries, comments and static files using PUT/GET/DELETE, and queries for comments and other references to an entry, via a '?references' query parameter to entries. New comments are created using POST to the resource (typically an entry) being commented-on.

If your browser supports XSLT rendering of XML content (Moz/5 does), you can view the [WWW]home page, [WWW]an entry, and what currently passes for [WWW]a comment. Also manageable thru atom.cgi are the stylesheets, a [WWW]FOAF file, and its icon, http://bitsko.slc.ut.us/~ken/atom.cgi/static/foafTiny.gif .


CategoryApi