Statistics
| Branch: | Tag: | Revision:

root / api / resource.py @ 6130effc

History | View | Annotate | Download (1.3 kB)

1 5c812d1b Faidon Liambotis
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
2 5c812d1b Faidon Liambotis
#
3 5c812d1b Faidon Liambotis
# Copyright © 2010 Greek Research and Technology Network
4 5c812d1b Faidon Liambotis
#
5 5c812d1b Faidon Liambotis
6 5c812d1b Faidon Liambotis
from piston.resource import Resource as BaseResource
7 5c812d1b Faidon Liambotis
from synnefo.helpers import parse_accept_header
8 93efd593 Faidon Liambotis
import synnefo.api.emitter # load our own Emitter
9 5c812d1b Faidon Liambotis
10 5c812d1b Faidon Liambotis
class Resource(BaseResource):
11 5c812d1b Faidon Liambotis
    def determine_emitter(self, request, *args, **kwargs):
12 5c812d1b Faidon Liambotis
        """
13 5c812d1b Faidon Liambotis
        Override default emitter policy to account for Accept header
14 5c812d1b Faidon Liambotis

15 5c812d1b Faidon Liambotis
        emitter_format (.json or .xml suffix in URL) always takes precedence.
16 5c812d1b Faidon Liambotis

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

20 5c812d1b Faidon Liambotis
        If none of the two were provided, then use JSON as per the
21 5c812d1b Faidon Liambotis
        specification.
22 5c812d1b Faidon Liambotis
        """
23 5c812d1b Faidon Liambotis
24 5c812d1b Faidon Liambotis
        em = request.GET.get('format', 'json')
25 5c812d1b Faidon Liambotis
        if 'emitter_format' in kwargs and \
26 5c812d1b Faidon Liambotis
           kwargs["emitter_format"] is not None:
27 5c812d1b Faidon Liambotis
            em = kwargs.pop('emitter_format')
28 5c812d1b Faidon Liambotis
        elif 'HTTP_ACCEPT' in request.META:
29 5c812d1b Faidon Liambotis
            accepts = parse_accept_header(request.META['HTTP_ACCEPT'])
30 5c812d1b Faidon Liambotis
            for content_type, quality in accepts:
31 5c812d1b Faidon Liambotis
                if content_type == 'application/json':
32 5c812d1b Faidon Liambotis
                    break
33 5c812d1b Faidon Liambotis
                elif content_type == 'application/xml':
34 5c812d1b Faidon Liambotis
                    em = request.GET.get('format', 'xml')
35 5c812d1b Faidon Liambotis
                    break
36 5c812d1b Faidon Liambotis
37 5c812d1b Faidon Liambotis
        return em