Exemplo n.º 1
0
Arquivo: helpers.py Projeto: orviz/ooi
    def _get_req(self,
                 req,
                 method,
                 path=None,
                 content_type="application/json",
                 body=None,
                 query_string=""):
        """Return a new Request object to interact with OpenStack.

        This method will create a new request starting with the same WSGI
        environment as the original request, prepared to interact with
        OpenStack. Namely, it will override the script name to match the
        OpenStack version. It will also override the path, content_type and
        body of the request, if any of those keyword arguments are passed.

        :param req: the original request
        :param path: new path for the request
        :param content_type: new content type for the request, defaults to
                             "application/json" if not specified
        :param body: new body for the request
        :param query_string: query string for the request, defaults to an empty
                             query if not specified
        :returns: a Request object
        """
        new_req = webob.Request(copy.copy(req.environ))
        new_req.script_name = self.openstack_version
        new_req.query_string = query_string
        new_req.method = method
        if path is not None:
            new_req.path_info = path
        if content_type is not None:
            new_req.content_type = content_type
        if body is not None:
            new_req.body = utils.utf8(body)
        return new_req
Exemplo n.º 2
0
    def serialize(self, request, content_type, default_serializers=None):
        if self.serializer:
            serializer = self.serializer
        else:
            _mtype, _serializer = self.get_serializer(content_type,
                                                      default_serializers)
            env = {"application_url": request.application_url + "/"}
            serializer = _serializer(env)

        response = webob.Response()
        response.status_int = self.code
        for hdr, value in self._headers.items():
            response.headers[hdr] = utils.utf8(value)
        response.headers['Content-Type'] = content_type
        response.charset = 'utf8'
        if self.obj is not None:
            headers, body = serializer.serialize(self.obj)
            if headers is not None:
                for hdr in headers:
                    response.headers.add(*hdr)
            if body:
                response.body = body

            # 204 should be used if there is no content
            if (not (headers or body)
                    and response.status_int in [200, 201, 202]):
                response.status_int = 204

        return response
Exemplo n.º 3
0
    def serialize(self, request, content_type, default_serializers=None):
        if self.serializer:
            serializer = self.serializer
        else:
            _mtype, _serializer = self.get_serializer(content_type,
                                                      default_serializers)
            env = {"application_url": request.application_url + "/"}
            serializer = _serializer(env)

        response = webob.Response()
        response.status_int = self.code
        for hdr, value in self._headers.items():
            response.headers[hdr] = utils.utf8(value)
        response.headers['Content-Type'] = content_type
        response.charset = 'utf8'
        if self.obj is not None:
            headers, body = serializer.serialize(self.obj)
            if headers is not None:
                for hdr in headers:
                    response.headers.add(*hdr)
            if body:
                response.body = body

            # 204 should be used if there is no content
            if (not (headers or body) and
                    response.status_int in [200, 201, 202]):
                response.status_int = 204

        return response
Exemplo n.º 4
0
Arquivo: helpers.py Projeto: orviz/ooi
    def _get_req(self, req, method,
                 path=None,
                 content_type="application/json",
                 body=None,
                 query_string=""):
        """Return a new Request object to interact with OpenStack.

        This method will create a new request starting with the same WSGI
        environment as the original request, prepared to interact with
        OpenStack. Namely, it will override the script name to match the
        OpenStack version. It will also override the path, content_type and
        body of the request, if any of those keyword arguments are passed.

        :param req: the original request
        :param path: new path for the request
        :param content_type: new content type for the request, defaults to
                             "application/json" if not specified
        :param body: new body for the request
        :param query_string: query string for the request, defaults to an empty
                             query if not specified
        :returns: a Request object
        """
        new_req = webob.Request(copy.copy(req.environ))
        new_req.script_name = self.openstack_version
        new_req.query_string = query_string
        new_req.method = method
        if path is not None:
            new_req.path_info = path
        if content_type is not None:
            new_req.content_type = content_type
        if body is not None:
            new_req.body = utils.utf8(body)
        return new_req
Exemplo n.º 5
0
    def serialize(self, data):
        if not isinstance(data, list):
            data = [data]

        renderers = []
        for d in data:
            renderers.append(urilist_rendering.get_renderer(d))

        ret = "\n".join([r.render(env=self.env) for r in renderers])
        return None, utils.utf8(ret)
Exemplo n.º 6
0
    def serialize(self, data):
        if not isinstance(data, list):
            data = [data]

        renderers = []
        for d in data:
            renderers.append(urilist_rendering.get_renderer(d))

        ret = "\n".join([r.render(env=self.env) for r in renderers])
        return None, utils.utf8(ret)
Exemplo n.º 7
0
 def __call__(self, req):
     code = self.wrapped_exc.status_int
     fault_name = self._fault_names.get(code)
     explanation = self.wrapped_exc.explanation
     fault_data = {
         fault_name: {
             'code': code,
             'message': explanation}}
     self.wrapped_exc.body = utils.utf8(json.dumps(fault_data))
     self.wrapped_exc.content_type = "application/json"
     return self.wrapped_exc
Exemplo n.º 8
0
 def __call__(self, req):
     code = self.wrapped_exc.status_int
     fault_name = self._fault_names.get(code)
     explanation = self.wrapped_exc.explanation
     fault_data = {
         fault_name: {
             'code': code,
             'message': explanation}}
     self.wrapped_exc.body = utils.utf8(json.dumps(fault_data))
     self.wrapped_exc.content_type = "application/json"
     return self.wrapped_exc
Exemplo n.º 9
0
    def serialize(self, data):
        if not isinstance(data, list):
            data = [data]

        renderers = []
        for d in data:
            renderers.append(header_rendering.get_renderer(d))

        # Header renderers will return a list, so we must flatten the results
        # before returning them
        headers = [i for r in renderers for i in r.render(env=self.env)]
        return headers, utils.utf8("")
Exemplo n.º 10
0
    def serialize(self, data):
        if not isinstance(data, list):
            data = [data]

        renderers = []
        for d in data:
            renderers.append(header_rendering.get_renderer(d))

        # Header renderers will return a list, so we must flatten the results
        # before returning them
        headers = [i for r in renderers for i in r.render(env=self.env)]
        return headers, utils.utf8("")
Exemplo n.º 11
0
    def _get_req(self,
                 req,
                 method,
                 path=None,
                 content_type="application/json",
                 body=None,
                 query_string=""):
        """Return a new Request object to interact with OpenStack.

        This method will create a new request starting with the same WSGI
        environment as the original request, prepared to interact with
        OpenStack. Namely, it will override the script name to match the
        OpenStack version. It will also override the path, content_type and
        body of the request, if any of those keyword arguments are passed.

        :param req: the original request
        :param path: new path for the request
        :param content_type: new content type for the request, defaults to
                             "application/json" if not specified
        :param body: new body for the request
        :param query_string: query string for the request, defaults to an empty
                             query if not specified
        :returns: a Request object
        """
        if hasattr(self, 'neutron_endpoint'):
            server = self.neutron_endpoint
            environ = copy.copy(req.environ)
            try:
                if "HTTP_X-Auth-Token" not in environ:
                    env_token = req.environ["keystone.token_auth"]
                    token = env_token.get_auth_ref(None)['auth_token']
                    environ = {"HTTP_X-Auth-Token": token}
            except Exception:
                raise webob.exc.HTTPUnauthorized

            new_req = webob.Request.blank(path=path,
                                          environ=environ,
                                          base_url=server)
        else:
            new_req = webob.Request(copy.copy(req.environ))
            new_req.script_name = self.openstack_version
        new_req.query_string = query_string
        new_req.method = method
        if path is not None:
            new_req.path_info = path
        if content_type is not None:
            new_req.content_type = content_type
        if body is not None:
            new_req.body = utils.utf8(body)
        return new_req
Exemplo n.º 12
0
    def _get_req(self, req, method,
                 path=None,
                 content_type="application/json",
                 body=None,
                 query_string=""):
        """Return a new Request object to interact with OpenStack.

        This method will create a new request starting with the same WSGI
        environment as the original request, prepared to interact with
        OpenStack. Namely, it will override the script name to match the
        OpenStack version. It will also override the path, content_type and
        body of the request, if any of those keyword arguments are passed.

        :param req: the original request
        :param path: new path for the request
        :param content_type: new content type for the request, defaults to
                             "application/json" if not specified
        :param body: new body for the request
        :param query_string: query string for the request, defaults to an empty
                             query if not specified
        :returns: a Request object
        """
        if hasattr(self, 'neutron_endpoint'):
            server = self.neutron_endpoint
            environ = copy.copy(req.environ)
            # NOTE(aloga): remove things from environment that will cause the
            # resquest not to be build properly, see
            # https://github.com/Pylons/webob/blob/master/src/webob/request.py
            for k in ("PATH_INFO", "SCRIPT_NAME", "SERVER_NAME", "SERVER_PORT",
                      "HTTP_HOST"):
                environ.pop(k, None)

            new_req = webob.Request.blank(path=path,
                                          environ=environ,
                                          base_url=server)
        else:
            new_req = webob.Request(copy.copy(req.environ))
            new_req.script_name = self.openstack_version
        new_req.query_string = query_string
        new_req.method = method
        if path is not None:
            new_req.path_info = path
        if content_type is not None:
            new_req.content_type = content_type
        if body is not None:
            new_req.body = utils.utf8(body)
        return new_req
Exemplo n.º 13
0
    def _get_req(self, req, method,
                 path=None,
                 content_type="application/json",
                 body=None,
                 query_string=""):
        """Return a new Request object to interact with OpenStack.

        This method will create a new request starting with the same WSGI
        environment as the original request, prepared to interact with
        OpenStack. Namely, it will override the script name to match the
        OpenStack version. It will also override the path, content_type and
        body of the request, if any of those keyword arguments are passed.

        :param req: the original request
        :param path: new path for the request
        :param content_type: new content type for the request, defaults to
                             "application/json" if not specified
        :param body: new body for the request
        :param query_string: query string for the request, defaults to an empty
                             query if not specified
        :returns: a Request object
        """
        if hasattr(self, 'neutron_endpoint'):
            server = self.neutron_endpoint
            environ = copy.copy(req.environ)
            try:
                if "HTTP_X-Auth-Token" not in environ:
                    env_token = req.environ["keystone.token_auth"]
                    token = env_token.get_auth_ref(None)['auth_token']
                    environ = {"HTTP_X-Auth-Token": token}
            except Exception:
                raise webob.exc.HTTPUnauthorized

            new_req = webob.Request.blank(path=path,
                                          environ=environ, base_url=server)
        else:
            new_req = webob.Request(copy.copy(req.environ))
            new_req.script_name = self.openstack_version
        new_req.query_string = query_string
        new_req.method = method
        if path is not None:
            new_req.path_info = path
        if content_type is not None:
            new_req.content_type = content_type
        if body is not None:
            new_req.body = utils.utf8(body)
        return new_req
Exemplo n.º 14
0
 def serialize(self, data):
     if not data:
         return None, utils.utf8("")
     r = json_rendering.get_renderer(data)
     return None, utils.utf8(r.render(env=self.env))
Exemplo n.º 15
0
 def serialize(self, data):
     if not data:
         return None, utils.utf8("")
     r = json_rendering.get_renderer(data)
     return None, utils.utf8(r.render(env=self.env))