python - Flask api representation affects all the views -


i need have 2 logically identical views response same data in different content types. use flask restful.

app = flask.flask(__name__) api = flask_restful.api(app)  @api.representation('text/csv')                                                def output_csv(data, code, headers=none):                                          data_is_list = isinstance(data, types.listtype)                                keys = data[0].keys() if data_is_list else data.keys()                         output = io.bytesio()                                                          writer = csv.dictwriter(output, keys)                                          writer.writeheader()                                                           encode = lambda v: v.encode('utf-8') if isinstance(                                    v, types.unicodetype) else v                                           if data_is_list:                                                                   writer.writerows([{k: encode(v) k, v in i.items()}     in data])     else:                                                                              writer.writerow({k: encode(v) k, v in data.items()})                    resp = flask.make_response(output.getvalue(), code)                            resp.headers.extend(headers or {})                                              return resp                                                                  class baseaction(flask_restful.resource):     def get(self, id=none):         # ...         return actions[0] if id else actions # dict or list of dicts.   class actionascsv(baseaction):     def get(self, id=none):         data = super(actionascsv, self).get(id, closed)         flask.response(data, mimetype='text/csv')               return data                                     

there several problems. after added representation view views return text/csv data apprpriate header. how use first view class return application/json data , second 1 return text/csv data? second issue return value of csv method, if return response object return flask.response(data, mimetype='text/csv') data become malformed - keys without values. how enable different mimetype without data damage?

firstly, please note should not use same resource return either single action or list of actions. should use 2 different resources:

class action(flask_restful.resource):     def get(self, action_id):         return actions[action_id]  class actionlist(flask_restful.resource):     def get(self):         return actions 

then, simplest way return different media types same resource use content negotiation. in case, not need declare dedicated resource actionascsv handle case of returning response in csv format. example, use content negotiation curl:

curl -ih "accept: text/csv" http://url_to_your_view

moreover, flask-restful automatically add right content-type header in returned response: not need define in get method of resource.

also, api configured default return representations in json. however, can modify follow:

api = flask_restful.api(app, default_mediatype="text/csv") 

if absolutly want 2 different resources handle either application/json or text/csv, withous using content negotiation , no fallback media type, possible:

api = flask_restful.api(app, default_mediatype=none)  class actionlistmixin(object):     def get(self):         return actions  class jsonactionlist(actionlistmixin, flask_restful.resource):     representations = {'application/json': output_json}  class csvactionlist(actionlistmixin, flask_restful.resource):     representations = {'text/csv': output_csv} 

anoter similar option define representation transformers when adding resources:

api = flask_restful.api(app, default_mediatype=none)  class actionlist(flask_restful.resource):     def __init__(self, representations=none):         self.representations = representations         super(actionlist, self).__init__()      def get(self):         return actions  api.add_resource(actionlist, '/actions_json', resource_class_kwargs={'representations': {'application/json': output_json}}) api.add_resource(actionlist, '/actions_csv', resource_class_kwargs={'representations': {'text/csv': output_csv}}) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -