[Grok-dev] Re: Is there an easy way to customize URL used for grok.REST (use suffix instead of prefix) ?

Craeg Strong cstrong at arielpartners.com
Sun Jun 8 18:31:06 EDT 2008


We finally got a chance to try this out, and thanks to your excellent 
advice, it is working perfectly.

>> I admit I have not yet grasped the full zen of IRESTLayer, REST, 
>> RESTProtocol, etc.
> Yeah, it's a bit involved. It's similar to skins.
>
> A skin is a particular layer that is published. It has a name 
> ++skin++foo. Views are associated with layers. Layers can extend other 
> layers and thus you can compose bigger layers out of smaller ones if 
> you want.
>
> A rest protocol is a particular rest layer that's published. It has a 
> name ++rest++foo. REST views are associated with rest layers.
We ended up creating an adapter that knows how to retrieve XML from any 
object that has an xml() method.
We put the boilerplate code in a separate "traversers.py" module:

import grok
from zope.publisher.http import applySkin

class MyLayer(grok.IRESTLayer):
    pass

class MyProtocol(grok.RESTProtocol):
    grok.layer(MyLayer)
    grok.name('xml')
   
class BaseTraverser(grok.Traverser):
    def traverse(self, name):
        if name == 'xml':
            if not MyLayer.providedBy(self.request):
                applySkin(self.request, MyLayer, grok.IRESTSkinType)
            return XmlAdapter(self.context)

class XmlAdapter(grok.Model):  
    def __init__(self, context):
        self.context = context
       
    def xml(self):
        return self.context.xml()
           
class RestAdapter(grok.REST):
    grok.context(XmlAdapter)
    grok.layer(MyLayer)
   
    def GET(self):
        self.response.setHeader('Content-Type', 'text/xml; 
charset=UTF-8')       
        return self.context.xml()

Then each class in the application that needs to have a REST interface 
simply needs to do this:

class MyApp(grok.Application):
   
    def xml():
       return '<app/>'

class MyAppTraverser(BaseTraverser):
    grok.context(MyApp)
>
> [snip]
>> Yes, something like that which also incorporates my other use case 
>> described above would be fantastic!
>> I am way too much of a newbie to contribute code, but can contribute 
>> the writing and executing of tests and documentation.
>
> I suggest you get some experience getting this pattern going with 
> REST, and then start a new thread based on your experiences. It looks 
> like we want something that's like a model and a view at the same 
> time. This can be accomplished with views on views too, by the way. 
> Perhaps that is the cleaner solution; but I'm not sure.

The above code is not bad, but I am still intrigued by your idea of 
adding something like RelativeRestView
Thanks so much for your help!

--Craeg
>
> Regards,
>
> Martijn
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev


More information about the Grok-dev mailing list