Webapp2:使用值初始化响应对象

时间:2014-10-31 23:15:50

标签: python http-headers webapp2

有没有办法用特定值初始化response对象,以便使用response对象的其他方法不必重新声明这些值?

E.g。 这是我目前要做的事情:

class TodoHandler(webapp2.RequestHandler):
    """Handle all the actions of the API"""

    def options(self):
        """Need to re-declare common headers in each method"""
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
        self.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS'


    def post(self):
        """Need to re-declare common headers in each method"""
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
        self.response.headers['Access-Control-Allow-Methods'] = 'POST'

        new_title = json.loads(self.request.body).get('title')
        self.response.write('Successfully added new todo')

这就是我要创建的内容:

class TodoHandler(webapp2.RequestHandler):
    """Handle all the actions of the API"""

    def initialize():
        # Set response headers to handle CORS
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'

    def options(self):
        """Response already initialized, so I only have to declare what I need for this method"""

        self.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS'


    def post(self):
        """Response already initialized, so I only have to declare what I need for this method"""

        self.response.headers['Access-Control-Allow-Methods'] = 'POST'

        new_title = json.loads(self.request.body).get('title')
        self.response.write('Successfully added new todo')

1 个答案:

答案 0 :(得分:0)

通常,多个类处理程序需要共享一些常用方法和响应值,在这种情况下,您将实现继承自RequestHandler的自己的webapp2.RequestHandler类。然后,所有后续路由/请求处理程序将从新创建的自定义RequestHandler继承:

class CustomApiRequestHandler(webapp2.RequestHandler):
    def render_api_json(self, obj):
        self.response.content_type = 'application/json'
        self.response.headers.add_header("Access-Control-Allow-Origin", "*")
        self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
        self.response.write(json.encode((self.to_json(obj))))


class TodoHandler(CustomApiRequestHandler):
    def options(self):
        """Response already initialized, so I only have to declare what I need for this method"""
        self.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS'
        return self.render_api_json(...)

    def post(self):
        """Response already initialized, so I only have to declare what I need for this method"""
        self.response.headers['Access-Control-Allow-Methods'] = 'POST'
        return self.render_api_json(...)

请注意,如果所有或多个options / post方法都需要公共标题/任务/等,您也可以在自定义RequestHandler中实现它们,然后调用super()在方法的开头。