html, rss, atom, Oh My!

2 views
Skip to first unread message

dundeemt

unread,
Nov 8, 2009, 10:14:35 AM11/8/09
to cherrypy-users
I am working on a back end controller to serve out data to various
clients. What I'm looking for is a way to return data in a variety of
formats based on the client request. for instance:

/controller/noun/verb returns html (the default)
/controller/noun/verb.html returns html
/controller/noun/verb.rss returns an rss feed
/controller/noun/verb.json returns json
etc, etc.

My patterns may not always be so simple they might include some
/controller/noun/verb/qualifier.rss

so I've been playing with
class MyRouter:
@cherrypy.expose
def default(self, x=None, *args,**kwargs):
if x:
return '%s:[%s][%s]' % (x,args,kwargs)
else:
return "No args found."

where x is the controller and everything after that can vary wildly.

So I would be writing a router of sorts and then handling all of the
mime type in the response myself. Lots of heavy lifting. My question
is, "How much of this is redundant to what CP can already do and I'm
just not looking the right places." My gut tells me that I am most
likely reinventing the wheel and I would rather not if I could build
on top of something that already exists.

The .rss/.xml is not a requirement, I could go with /rss, /xml etc I
just figured that the suffix might be easier to detect given the
variability of the urls.

Best,

Jeff

Gerold Penz

unread,
Nov 8, 2009, 11:38:15 AM11/8/09
to cherryp...@googlegroups.com
dundeemt schrieb:

> /controller/noun/verb returns html (the default)
> /controller/noun/verb.html returns html
> /controller/noun/verb.rss returns an rss feed
> /controller/noun/verb.json returns json

Hello Jeff!

What`s about this?

class Noun(object):

def verb(*args, **kwargs):
...
return html

def verb_html(*args, **kwargs):
return verb(*args, **kwargs)

def verb_rss(*args, **kwargs):
...
return rss

def verb_json(*args, **kwargs):
...
return json

Regards,
Gerold
:-)

--
________________________________________________________________________
Gerold Penz - http://halvar.at
Wissen hat eine wunderbare Eigenschaft:
Es verdoppelt sich, wenn man es teilt.

Gerold Penz

unread,
Nov 8, 2009, 3:46:40 PM11/8/09
to cherryp...@googlegroups.com
Gerold Penz schrieb:
> def verb_html(*args, **kwargs):

...

If you don´t know it -- the underline (_) is a replacement for the dot.

http://localhost/.../verb.html --> verb_html()

Try it out.

dundeemt

unread,
Nov 9, 2009, 6:43:00 PM11/9/09
to cherrypy-users
While I did know it, I had completely forgotten it. Thank You!
I'll give it a go and post back.

Best,
Jeff

On Nov 8, 2:46 pm, Gerold Penz <gerold.p...@aon.at> wrote:
> Gerold Penz schrieb:
>
> >          def verb_html(*args, **kwargs):
>
> ...
>
> If you don´t know it -- the underline (_) is a replacement for the dot.
>
>      http://localhost/.../verb.html--> verb_html()
>
> Try it out.
>
> --
> ________________________________________________________________________
> Gerold Penz -http://halvar.at

dundeemt

unread,
Nov 12, 2009, 12:49:48 AM11/12/09
to cherrypy-users
While Gerold's suggestion was spot on for my question, I realize after
working with it that my question was flawed.

What I really want is to write a data controller, such that it will
handle returning the responses based on the url

/controller/noun/id and then format with either something like .json
or /json giving an url like
/controller/noun/id.json | /controller/noun/id.xml | /controller/noun/
id.html | /controller/noun/id
or
/controller/noun/id/json

I've been looking at selector4cherrypy as a solution.

Best,

Jeff

Robert Brewer

unread,
Nov 12, 2009, 11:25:59 AM11/12/09
to cherryp...@googlegroups.com
dundeemt wrote:
> While Gerold's suggestion was spot on for my question, I realize after
> working with it that my question was flawed.
>
> What I really want is to write a data controller, such that it will
> handle returning the responses based on the url
>
> /controller/noun/id and then format with either something like .json
> or /json giving an url like
> /controller/noun/id.json | /controller/noun/id.xml |
/controller/noun/
> id.html | /controller/noun/id
> or
> /controller/noun/id/json
>
> I've been looking at selector4cherrypy as a solution.

Why?

def noun(self, id, format='json'):
thing = Thing.get(id)
if format == 'json':
cherrypy.response.headers['Content-Type'] =
'application/json'
return simplejson.dumps(thing.dict)
elif format == 'xml':
...
else:
raise cherrypy.NotFound()
noun.exposed = True


Robert Brewer

dundeemt

unread,
Dec 3, 2009, 9:13:10 AM12/3/09
to cherrypy-users
Ok, I still haven't been happy with my attempts. I've run across a
django add-on project django-piston
http://bitbucket.org/jespern/django-piston/wiki/Documentation#piston-documentation
that has an interesting component, namely the Emitters. Cherrypy
already handles dispatch by method but what I am looking for is
something like what the Emitter does for piston. allows one to use
resource/id?format=yaml or resource/id.yaml -- the dispatcher
consumes the format portion of the request so the handler would look
like
myhandler(req, id):
...
return some_iterable

The returning iterable is handed to the desired emitter which takes
care of headers and translation. Which keeps that logic and code out
of the handler.(nice)

What I am looking for is some guidance in what I need to do to get
this same effect with cherrypy, since I am building a REST api that I
want to speak multiple data formats without clouding the logic in my
handlers. (json,xml,yaml,csv, ??)

Best,

Jeff
Reply all
Reply to author
Forward
0 new messages