Statistics
| Branch: | Tag: | Revision:

root / api / resource.py @ b016b476

History | View | Annotate | Download (1.3 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
import synnefo.api.emitter # load our own Emitter
9

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

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

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

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

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

    
37
        return em
38