wangwenchao / django-rest-interface

Automatically exported from code.google.com/p/django-rest-interface
0 stars 0 forks source link

Ehancement proposal #36

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello,

I think that the resource should be aware and responsible for the format
output. 

If I want to do an API I want that my users could use it in all the
available formats. Not just one. It's easier to enable all format by
default. And also in the actual implentation we have to add another tedious
layer of abstraction to attain this goal.

Here is an example of an aware ressource (works only with get request I
guess because of the request.GET):

{{{
class FormatAwareResource(ResourceBase):

    def __call__(self, request, *args, **kwargs):

        self.format = request.GET.get('format', 'xml')
        self.mimetype = 'application/'+self.format
        self.responder = SerializeResponder(format=self.format,
mimetype=self.mimetype)

        # Check permission
        if not self.authentication.is_authenticated(request):
            response = HttpResponse(_('Authorization Required'),
mimetype=self.mimetype)
            challenge_headers = self.authentication.challenge_headers()
            for k,v in challenge_headers.items():
                response[k] = v
            response.status_code = 401
            return response

        try:
            return self.dispatch(request, self, *args, **kwargs)
        except HttpMethodNotAllowed:
            response = HttpResponseNotAllowed(self.permitted_methods)
            response.mimetype = self.mimetype
            return response
}}}

Original issue reported on code.google.com by batiste....@gmail.com on 3 Feb 2009 at 1:14