html, rss, atom, Oh My!

View: New views
7 Messages — Rating Filter:   Alert me  

html, rss, atom, Oh My!

by Jeff H-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@...
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: html, rss, atom, Oh My!

by Gerold Penz-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@...
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: html, rss, atom, Oh My!

by Gerold Penz-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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
Wissen hat eine wunderbare Eigenschaft:
Es verdoppelt sich, wenn man es teilt.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@...
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: html, rss, atom, Oh My!

by Jeff H-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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...@...> 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
> Wissen hat eine wunderbare Eigenschaft:
> Es verdoppelt sich, wenn man es teilt.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@...
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: html, rss, atom, Oh My!

by Jeff H-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On Nov 9, 5:43 pm, dundeemt <dunde...@...> wrote:

> 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...@...> 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
> > Wissen hat eine wunderbare Eigenschaft:
> > Es verdoppelt sich, wenn man es teilt.

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@....
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=.



RE: Re: html, rss, atom, Oh My!

by Robert Brewer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@....
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=.



Re: html, rss, atom, Oh My!

by Jeff H-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Nov 12, 10:25 am, "Robert Brewer" <fuman...@...> wrote:

> 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

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

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users@....
To unsubscribe from this group, send email to cherrypy-users+unsubscribe@....
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.