I use decorators for authentication, in your case how do you handle when all application controllers methods need the same functionality? Having a lot of decorators over every method would look a bit bloated.
On Tuesday, October 30, 2012 10:19:06 PM UTC+4, Kevin Houlihan wrote: > > I'm working on a REST API for which I implement common functionality using > decorators that I can add to the request methods, and part of that common > functionality is setting headers. It's a work in progress but this is the > general idea: > > def serializecontent(method): > > @wraps(method) > > def wrapper(self, *args, **keys): > > > > # Retrieve the data from the wrapped method > > content = method(self, *args, **keys) > > > > if not content is None: > > # Serialize the data as requested > > serializeddata = self.serialize(content) > > # Set the content headers > > # This may not be the best place to do this, particularly the >> content length. > > self.setcontentlengthheader(serializeddata) > > self.setcontenttypeheader() > > return serializeddata > > else: > > return None > > >> return wrapper > > > class UserCollection(Collection): >> > > > @handleexceptions >> @requestmethod >> @setetag() >> @serializecontent >> @setlastmodified(determinelastmodified=_lastmodified) >> @conditionalresponse(determinelastmodified=_lastmodified) >> def GET(self): >> return Collection.GET(self) > > > > # ... > > > Is that more or less hacky? :P It needs a lot of tidying up as it is at > the moment, and I'm probably sidestepping a lot of great functionality that > web.py provides, but the thing I was trying to avoid like yourself was > recreating the same standard functionality in each request method. > > Regards, > Kevin > > On Wed, Oct 24, 2012 at 9:48 PM, Cameron <[email protected] > <javascript:>>wrote: > >> I'm curious to know what solution everyone has come up with for setting >> the Content-Length headers for larger web applications. I'd rather not set >> it for every single app / subapp response so I came up with this but it >> seems pretty hacky. Thoughts? >> >> def headersProcessor(h): >> """ Set the content length and and content type of each request. >> h : the handler for the requested url >> return the response from the handler """ >> res = h() >> web.header('Content-type', 'text/html; charset=utf-8') >> web.header('Content-Length', len(res)) >> return res >> >> app.add_processor(headersProcessor) >> >> -- >> You received this message because you are subscribed to the Google Groups >> "web.py" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/webpy/-/brb4RSGGqZMJ. >> To post to this group, send email to [email protected] <javascript:> >> . >> To unsubscribe from this group, send email to >> [email protected] <javascript:>. >> For more options, visit this group at >> http://groups.google.com/group/webpy?hl=en. >> > > -- You received this message because you are subscribed to the Google Groups "web.py" group. To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/jVou_kd7i5UJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/webpy?hl=en.
