[snowy] Add support for Root resource in REST API.



commit 1cb344b70f4e667224793fc1ff9d380f59b1573a
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Sat Jun 27 10:18:22 2009 -0700

    Add support for Root resource in REST API.
    
    Also add 'user-name' to User resource, since most users will
    not directly provide it.

 api/handlers.py |   14 ++++++++++++++
 api/urls.py     |    2 ++
 2 files changed, 16 insertions(+), 0 deletions(-)
---
diff --git a/api/handlers.py b/api/handlers.py
index 360aeaa..b4ac23e 100644
--- a/api/handlers.py
+++ b/api/handlers.py
@@ -46,6 +46,19 @@ class catch_and_return(object):
                 return self.response
         return wrapper
 
+# http://domain/api/1.0
+class RootHandler(BaseHandler):
+    allow_methods = ('GET')
+
+    def read(self, request):
+        return {
+            'user-ref': {
+                'api-ref' : 'http://%s%s' % (settings.DOMAIN_NAME, '/api/1.0/' + request.user.username),
+                'href' : 'http://%s%s' % (settings.DOMAIN_NAME, '/' + request.user.username)
+            },
+            'api-version': '1.0'
+        }
+
 # http://domain/api/1.0/user
 class UserHandler(AnonymousBaseHandler):
     allow_methods = ('GET',)
@@ -56,6 +69,7 @@ class UserHandler(AnonymousBaseHandler):
         profile = user.get_profile()
         reverse_args = {'username': username}
         return {
+            'user-name': user.username,
             'first-name': user.first_name,
             'last-name': user.last_name,
             'notes-ref': {
diff --git a/api/urls.py b/api/urls.py
index 02df555..40c9cf7 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -26,6 +26,7 @@ auth = HttpBasicAuthentication(realm='Snowy')
 authoauth = OAuthAuthentication(realm='Snowy')
 ad = {'authentication': authoauth}
 
+root_handler = Resource(handler=RootHandler, **ad)
 user_handler = Resource(UserHandler)
 notes_handler = Resource(handler=NotesHandler, **ad)
 note_handler = Resource(handler=NoteHandler, **ad)
@@ -35,4 +36,5 @@ urlpatterns = patterns('',
     url(r'1.0/(?P<username>\w+)/notes/(?P<note_id>\d+)/$', note_handler, name='note_api_detail'),
     url(r'1.0/(?P<username>\w+)/notes/$', notes_handler, name='note_api_index'),
     url(r'1.0/(?P<username>\w+)/$', user_handler),
+    url(r'1.0/$', root_handler),
 )



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]