You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
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 a dict and then it marshals that through mimerender 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 and application/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. So mimerender can't make this call, it has to come from something resource specific.
You can make something like this:
classHALResource(Resource):
serialize_embedded=Truedefserialize_field(self, obj, **kwargs):
ifself.serialize_embedded:
returnself.serialize(obj, **kwargs)
else:
returnself._url(str(obj.id))
defserialize(self, obj, **kwargs):
# Get the normal serialized versionrep=super(HALResource,self).serialize(obj, **kwargs)
# Pull the database ID out of this and then remove that fieldmyid=rep["id"]
rep.pop("id")
# Compute the URI for this resource (including host name)myuri=self._url(str(myid))
# Populate the initial _links and _embedded fieldlinks= {"self": myuri}
embedded= {}
# Added related resources to embedded if they have# a uri_prefixforkinself.related_resources:
r=self.related_resources[k]()
ifr.uri_prefix!=None:
v=rep[k]
rep.pop(k)
embedded[k] =v# Add special fieldsrep["_links"] =linksiflen(embedded)>0:
rep["_embedded"] =embedded# Return the HAL versionreturnrep
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 for renderers 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.
The text was updated successfully, but these errors were encountered:
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. :-(
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.
The text was updated successfully, but these errors were encountered: