(self, callback, route)
| 1991 | " apply.") |
| 1992 | |
| 1993 | def apply(self, callback, route): |
| 1994 | dumps = self.json_dumps |
| 1995 | if not self.json_dumps: return callback |
| 1996 | |
| 1997 | @functools.wraps(callback) |
| 1998 | def wrapper(*a, **ka): |
| 1999 | try: |
| 2000 | rv = callback(*a, **ka) |
| 2001 | except HTTPResponse as resp: |
| 2002 | rv = resp |
| 2003 | |
| 2004 | if isinstance(rv, dict): |
| 2005 | #Attempt to serialize, raises exception on failure |
| 2006 | json_response = dumps(rv) |
| 2007 | #Set content type only if serialization successful |
| 2008 | response.content_type = 'application/json' |
| 2009 | return json_response |
| 2010 | elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict): |
| 2011 | rv.body = dumps(rv.body) |
| 2012 | rv.content_type = 'application/json' |
| 2013 | return rv |
| 2014 | |
| 2015 | return wrapper |
| 2016 | |
| 2017 | |
| 2018 | class TemplatePlugin(object): |
no outgoing calls