[Grok-dev] Multiple RESTful Views

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Sep 24 03:39:31 EDT 2009


Paul Wilson wrote:
> It appears that a REST view doesn't work quite like other views; each
> method (GET/POST/PUT/DELETE) is a view itself.
> 
> This is a problem for me because i'd like to provide many different
> RESTful interfaces for the same content type. Is this pattern
> discouraged or is there a workaround that you guys know of?
> 
> I cannot seem to do this:
> 
> ---------------------%<----------------------
> class DefaultREST(grok.REST):
>     grok.layer(IRestLayer)
>     grok.name('default')
>     grok.context(IFooContext)
> 
>     def GET(self):
>         ....
> 
> class AlternateREST(grok.REST):
>     grok.layer(IRestLayer)
>     grok.name('alternate')
>     grok.context(IFooContext)
> 
>     def GET(self):
>        .....
> 
> ConfigurationConflictError: Conflicting configuration actions
> ---------------------%<----------------------
> 
> Any hints appreciated!

The key is the grok.layer(IRestLayer). If you want to have two REST APIs 
available for you objects, you should register each for a separate 
layer, derived from the IRestLayer.

class IRestAPIOne(IRestLayer):
   grok.restskin('default')


class IRestAPITwo(IRestLayer):
     grok.restskin('alternate')


class DefaultREST(grok.REST):
     grok.layer(IRestAPIOne)
     grok.name('default')
     grok.context(IFooContext)

     def GET(self):
         ....

class AlternateREST(grok.REST):
     grok.layer(IRestAPITwo)
     grok.name('alternate')
     grok.context(IFooContext)

     def GET(self):
        .....

See also the ftests of Grok itself for more hints:

http://svn.zope.org/grok/trunk/src/grok/ftests/rest/rest.py?view=markup

HTH.
regards,
jw



More information about the Grok-dev mailing list