def create_response(body, status, headers): if body is not None: headers['Content-Type'] = packers.get_content_type(headers) packer = self.get_packer(headers['Content-Type']) body = packer.pack(body) return webob.Response( body=body, status=status, content_type=headers.get('Content-Type', None), headerlist=[(k, v) for k, v in headers.items()])
def do_resource(self, uuid, parent_resource=None): method = self._req.method kwargs = self._make_kwargs(parent_resource) if method == 'GET': return self.process_result(self.get(uuid=uuid, **kwargs)) elif method == 'PUT': content_type = packers.get_content_type(self._req.headers) packer = self.get_packer(content_type) kwargs.update(packer.unpack(self._req.body)) return self.process_result(self.update(uuid=uuid, **kwargs)) elif method == 'DELETE': result = self.delete(uuid=uuid, **kwargs) return self.process_result(result, 200 if result else 204) else: raise exc.UnsupportedHttpMethod(method=method)
def do_collection(self, parent_resource=None): method = self._req.method if method == 'GET': # TODO(Eugene Frolov): Method returns NestedMultiDict which it # includes multiple identical keys. It is problem. One must # writes a correct translation NestedMultiDict to a type of dict. kwargs = self._make_kwargs(parent_resource, **dict(self._req.params)) return self.process_result(self.filter(**kwargs)) elif method == 'POST': content_type = packers.get_content_type(self._req.headers) packer = self.get_packer(content_type) kwargs = self._make_kwargs(parent_resource, **packer.unpack(self._req.body)) return self.process_result(self.create(**kwargs), 201, add_location=True) else: raise exc.UnsupportedHttpMethod(method=method)