Open xogeny opened 11 years ago
In case anybody is interested and following this ticket, I've got patches that processes the "Accept" header on the request and can match it against different output formats, pick the correct one and provides the correct content type on the response.
For example, with this code you can say "If I get a request with Accept: application/json", then use this code to generate and render the JSON and return a Content-Type: application/json.
But "If I get a request with Accept: application/hal+json", use different code to formulate the response (dict can be organized differently, which would be required for HAL), render the JSON and return Content-Type: application/hal+json.
Now, you may not care about HAL, but the point is that you can have an API where the clients can request different formats (various JSON serializations, XML, YAML, etc) and you can plug-in code to deal with these requests without having to change your Resource definitions.
I'm trying to put together a pull request, but the changes are involved. :-(
@xogeny Do you know what the general steps might be to support CSV with content-type application/csv ?
@pezon Sorry, I've been away from flask-mongorest
for a long time. I really don't have any suggestions. Sorry.
This blog post has a great discussion about hypermedia formats like HAL and Siren.
Personally, I'm quite interested in exploring these formats and supporting one or more of them in my API. The issue I see is that the current approach expects each
dispatch_request
method to return adict
and then it marshals that throughmimerender
into either JSON or XML.But the issue I see in supporting things like HAL or Siren is that these are effectively different content types (
application/hal+json
andapplication/vnd.siren+json
, respectively). In both cases, you could get by with returning different dictionaries, but the structure of the dictionaries wouldn't be the same and it depends on the contents of the underlying Document. Somimerender
can't make this call, it has to come from something resource specific.You can make something like this:
But this links the Resource with the content type in an ugly way. What if you want to support both formats? It seems to me that what you need is to have the
serialize
method handle this somehow. Ideally, it would be nice to have renderers for different formats, e.g.and then the
Resource.serialize
method could simply look at the requested content type and call the appropriate renderer. The default value forrenderers
should include at least[DefaultJSONRenderer, DefaultXMLRenderer]
(which would capture the current approach) but could also be extended to include other supported content types without any backward compatibility issues.Does this seem like a reasonable approach? If so, I could take a shot at making a backward compatible pull request to add this functionality.