[Checkins] SVN: grok/branches/faassen-rest/src/grok/ Added ability to retrieve raw request body, and document it.

Martijn Faassen faassen at infrae.com
Tue Oct 16 08:29:02 EDT 2007


Log message for revision 80890:
  Added ability to retrieve raw request body, and document it.
  

Changed:
  U   grok/branches/faassen-rest/src/grok/components.py
  U   grok/branches/faassen-rest/src/grok/ftests/rest/rest.py
  U   grok/branches/faassen-rest/src/grok/interfaces.py

-=-
Modified: grok/branches/faassen-rest/src/grok/components.py
===================================================================
--- grok/branches/faassen-rest/src/grok/components.py	2007-10-16 11:19:38 UTC (rev 80889)
+++ grok/branches/faassen-rest/src/grok/components.py	2007-10-16 12:29:01 UTC (rev 80890)
@@ -196,6 +196,13 @@
     pass
 
 class REST(object):
+    interface.implements(interfaces.IREST)
+    
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+        self.body = request.bodyStream.getCacheStream().read()
+    
     def GET(self):
         raise GrokMethodNotAllowed(self.context, self.request)
     

Modified: grok/branches/faassen-rest/src/grok/ftests/rest/rest.py
===================================================================
--- grok/branches/faassen-rest/src/grok/ftests/rest/rest.py	2007-10-16 11:19:38 UTC (rev 80889)
+++ grok/branches/faassen-rest/src/grok/ftests/rest/rest.py	2007-10-16 12:29:01 UTC (rev 80890)
@@ -182,6 +182,31 @@
   WWW-Authenticate: basic realm="Zope"
   <BLANKLINE>
 
+Normally when we POST or PUT a request, we expect some content. This
+content is sent along as the request body. In the normal case for POST
+we tend to retrieve this information from a web form (request.form),
+but with REST often the POST body contains a description of an
+entirely new resource, similar to what is contained in a PUT body. We
+therefore need to have some easy way to get to this information. The 'body'
+attribute on the REST view contains the uploaded data::
+
+  >>> print http_call('POST', 'http://localhost/++rest++f/app/alpha',
+  ...                 'this is the POST body')
+  HTTP/1.1 200 Ok
+  Content-Length: 21
+  Content-Type: text/plain
+  <BLANKLINE>
+  this is the POST body
+
+This works with PUT as well::
+
+  >>> print http_call('PUT', 'http://localhost/++rest++f/app/alpha',
+  ...                 'this is the PUT body')
+  HTTP/1.1 200 Ok
+  Content-Length: 20
+  <BLANKLINE>
+  this is the PUT body
+
 Todo:
 
 * Support for OPTIONS, HEAD, other methods?
@@ -209,6 +234,9 @@
 class LayerSecurity(grok.IRESTLayer):
     pass
 
+class LayerContent(grok.IRESTLayer):
+    pass
+
 class A(grok.RESTProtocol):
     grok.layer(LayerA)
 
@@ -223,6 +251,9 @@
 
 class E(grok.RESTProtocol):
     grok.layer(LayerSecurity)
+
+class F(grok.RESTProtocol):
+    grok.layer(LayerContent)
     
 class ARest(grok.REST):
     grok.layer(LayerA)
@@ -280,3 +311,13 @@
     def DELETE(self):
         return "DELETE3"
     
+class BodyTest(grok.REST):
+    grok.context(MyContent)
+    grok.layer(LayerContent)
+
+    def POST(self):
+        return self.body
+
+    def PUT(self):
+        return self.body
+    

Modified: grok/branches/faassen-rest/src/grok/interfaces.py
===================================================================
--- grok/branches/faassen-rest/src/grok/interfaces.py	2007-10-16 11:19:38 UTC (rev 80889)
+++ grok/branches/faassen-rest/src/grok/interfaces.py	2007-10-16 12:29:01 UTC (rev 80890)
@@ -413,7 +413,15 @@
         doesn't have to update an object, the dictionary is empty.
         """
 
+class IREST(interface.Interface):
+    context = interface.Attribute("Object that the REST handler presents.")
 
+    request = interface.Attribute("Request that REST handler was looked"
+                                  "up with.")
+    
+    body = interface.Attribute(
+        """The text of the request body.""")
+
 class IApplication(interface.Interface):
     """Marker-interface for grok application factories.
 



More information about the Checkins mailing list