~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/views/api.py

  • Committer: Tarmac
  • Author(s): Abel Deuring
  • Date: 2013-08-07 13:31:20 UTC
  • mfrom: (334.1.5 etags)
  • Revision ID: tarmac-20130807133120-euu4igaar08e89iy
[r=abentley][bug=][author=adeuring] Add an Etag header to the API response for charm details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
)
39
39
 
40
40
 
41
 
def json_response(status, params):
 
41
def json_response(status, params, headers=[]):
42
42
    """Return a JSON API response.
43
43
 
44
44
    :param status: The HTTP status code to use.
54
54
                        ('Content-Type', 'application/json'),
55
55
                        ("Access-Control-Allow-Origin", "*"),
56
56
                        ("Access-Control-Allow-Headers", "X-Requested-With"),
57
 
                    ],
 
57
                    ] + headers,
58
58
                    status_code=status)
59
59
 
60
60
 
334
334
                404, {'type': 'no_such_charm', 'charm_id': charm_id})
335
335
        charm = Charm(charm_data)
336
336
        if trailing is None:
337
 
            return self._charm_results([charm_data])[0]
 
337
            return self._charm_details(charm_data)
338
338
        elif trailing.startswith('file/'):
339
339
            return self._charm_file(charm, trailing)
340
340
        elif trailing == ('icon.svg'):
377
377
            headerlist.append(('Content-Type', content_type))
378
378
        return headerlist
379
379
 
 
380
    def _charm_details(self, charm_data):
 
381
        h = charm_data.get('hash')
 
382
        if h is not None:
 
383
            if_none_match = getattr(self.request, 'if_none_match', None)
 
384
            if if_none_match is not None and h in if_none_match:
 
385
                return Response('', status_code=304)
 
386
            result = self._charm_result(Charm(charm_data))
 
387
            return json_response(200, result, [('Etag', h)])
 
388
        return self._charm_result(Charm(charm_data))
 
389
 
380
390
    def _charm_file(self, charm, trailing):
381
391
        path = trailing.split('/', 1)[1]
382
392
        file_data = charm.files.get(quote_key(path.split('/')[-1]))