def make_response(self, data, *args, **kwargs): """Looks up the representation transformer for the requested media type, invoking the transformer to create a response object. This defaults to default_mediatype if no transformer is found for the requested mediatype. If default_mediatype is None, a 406 Not Acceptable response will be sent as per RFC 2616 section 14.1 :param data: Python object containing response data to be transformed """ default_mediatype = kwargs.pop('fallback_mediatype', None) or self.default_mediatype mediatype = request.accept_mimetypes.best_match( self.representations, default=default_mediatype, ) if mediatype is None: raise NotAcceptable() if mediatype in self.representations: resp = self.representations[mediatype](data, *args, **kwargs) resp.headers['Content-Type'] = mediatype return resp elif mediatype == 'text/plain': resp = original_flask_make_response(str(data), *args, **kwargs) resp.headers['Content-Type'] = 'text/plain' return resp else: raise InternalServerError()
def make_response(self, data, *args, **kwargs): """ Looks up the representation transformer for the requested media type, invoking the transformer to create a response object. This defaults to default_mediatype if no transformer is found for the requested mediatype. If default_mediatype is None, a 406 Not Acceptable response will be sent as per RFC 2616 section 14.1 :param data: Python object containing response data to be transformed """ default_mediatype = (kwargs.pop("fallback_mediatype", None) or self.default_mediatype) mediatype = request.accept_mimetypes.best_match( self.representations, default=default_mediatype, ) if mediatype is None: raise NotAcceptable() if mediatype in self.representations: resp = self.representations[mediatype](data, *args, **kwargs) resp.headers["Content-Type"] = mediatype return resp elif mediatype == "text/plain": resp = original_flask_make_response(str(data), *args, **kwargs) resp.headers["Content-Type"] = "text/plain" return resp else: raise InternalServerError()
def post(self, dataset, action): '''direct search into the dataset''' if ((action == "_search")): try: args = parsers.es_parser.parse_args() ds = Dataset(dataset) query = request.get_json() if (ds.connector.type == "elasticsearch"): try: ds.select = {"query": {"function_score": { "query": query["query"], "random_score": {}}}} except: ds.select = query try: size = args['size'] except: size = ds.chunk try: # hack for speed up an minimal rendering on object resp = original_flask_make_response(json.dumps(ds.connector.es.search( body=ds.select, index=ds.table, doc_type=ds.doc_type, size=size))) resp.headers['Content-Type'] = 'application/json' return resp except: return api.abort(403, err()) else: api.abort(403, "not an elasticsearch dataset") except: return {"status": "ko - " + err()} else: api.abort(403)
def post(self, dataset, id, action): '''elasticsearch update api proxy''' if ((action == "_update")): try: args = parsers.es_parser.parse_args() ds = Dataset(dataset) data = request.get_json() except: return {"status": "ko - " + err()} if (ds.connector.type == "elasticsearch"): try: # hack for speed up an minimal rendering on object resp = original_flask_make_response( json.dumps( ds.connector.es.update(index=ds.table, id=id, body=data))) resp.headers['Content-Type'] = 'application/json' return resp except: return api.abort(403, err()) else: api.abort(403, "not an elasticsearch dataset") else: api.abort(403)
def make_response(self, *args, **kwargs): """Looks up the representation transformer for the requested media type, invoking the transformer to create a response object. This defaults to default_mediatype if no transformer is found for the requested mediatype. If default_mediatype is None, a 406 Not Acceptable response will be sent as per RFC 2616 section 14.1 :param data: Python object containing response data to be transformed """ if len(args) > 1 and args[1] == 204: # 204, no content, force text/plain so we don't add any data during transform mediatype = 'text/plain' else: default_mediatype = kwargs.pop('fallback_mediatype', None) or self.default_mediatype mediatype = request.accept_mimetypes.best_match( self.representations, default=default_mediatype, ) if mediatype is None: raise NotAcceptable() if mediatype in self.representations: resp = self.representations[mediatype](*args, **kwargs) resp.headers['Content-Type'] = mediatype return resp elif mediatype == 'text/plain': resp = original_flask_make_response(*args) resp.headers['Content-Type'] = 'text/plain' return resp else: raise InternalServerError()
def make_response(self, data, *args, **kwargs): """Looks up the representation transformer for the requested media type, invoking the transformer to create a response object. This defaults to default_mediatype if no transformer is found for the requested mediatype. If default_mediatype is None, a 406 Not Acceptable response will be sent as per RFC 2616 section 14.1 :param data: Python object containing response data to be transformed """ default_mediatype = kwargs.pop('fallback_mediatype', None) or self.default_mediatype mediatype = request.accept_mimetypes.best_match( self.representations, default=default_mediatype, ) if mediatype is None: raise NotAcceptable() if mediatype in self.representations: resp = self.representations[mediatype](data, *args, **kwargs) resp.headers['Access-Control-Allow-Origin'] = current_app.config['ACCESS_CONTROL_ALLOW_ORIGIN_HEADER'] resp.headers['Access-Control-Allow-Headers'] = current_app.config['ACCESS_CONTROL_ALLOW_HEADERS'] resp.headers['Access-Control-Allow-Methods'] = current_app.config['ACCESS_CONTROL_ALLOW_METHODS'] resp.headers['Content-Type'] = mediatype return resp elif mediatype == 'text/plain': resp = original_flask_make_response(str(data), *args, **kwargs) resp.headers['Access-Control-Allow-Origin'] = current_app.config['ACCESS_CONTROL_ALLOW_ORIGIN_HEADER'] resp.headers['Access-Control-Allow-Headers'] = current_app.config['ACCESS_CONTROL_ALLOW_HEADERS'] resp.headers['Access-Control-Allow-Methods'] = current_app.config['ACCESS_CONTROL_ALLOW_METHODS'] resp.headers['Content-Type'] = 'text/plain' return resp else: raise InternalServerError()
def make_response(self, data, *args, **kwargs): if isinstance(data, requests.models.Response): headers = list(data.headers.items()) response = original_flask_make_response(data.content, data.status_code, headers) for key in response.headers.keys(): if key not in data.headers: response.headers.pop(key) assert response.status_code == data.status_code else: response = super(Api, self).make_response(data, *args, **kwargs) return response