Statistics
| Branch: | Tag: | Revision:

root / api / resource.py @ 5c812d1b

History | View | Annotate | Download (1.2 kB)

1
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
2
#
3
# Copyright © 2010 Greek Research and Technology Network
4
#
5

    
6
from piston.resource import Resource as BaseResource
7
from synnefo.helpers import parse_accept_header
8

    
9
class Resource(BaseResource):
10
    def determine_emitter(self, request, *args, **kwargs):
11
        """
12
        Override default emitter policy to account for Accept header
13

14
        emitter_format (.json or .xml suffix in URL) always takes precedence.
15

16
        After that, the Accept header is checked; if both JSON and XML are
17
        equally preferred, use JSON.
18

19
        If none of the two were provided, then use JSON as per the
20
        specification.
21
        """
22

    
23
        em = request.GET.get('format', 'json')
24
        if 'emitter_format' in kwargs and \
25
           kwargs["emitter_format"] is not None:
26
            em = kwargs.pop('emitter_format')
27
        elif 'HTTP_ACCEPT' in request.META:
28
            accepts = parse_accept_header(request.META['HTTP_ACCEPT'])
29
            for content_type, quality in accepts:
30
                if content_type == 'application/json':
31
                    break
32
                elif content_type == 'application/xml':
33
                    em = request.GET.get('format', 'xml')
34
                    break
35

    
36
        return em
37