github.com/dustinrc/deis@v1.10.1-0.20150917223407-0894a5fb979e/controller/api/middleware.py (about) 1 """ 2 HTTP middleware for the Deis REST API. 3 4 See https://docs.djangoproject.com/en/1.6/topics/http/middleware/ 5 """ 6 7 import json 8 9 from django.http import HttpResponse 10 from rest_framework import status 11 12 from api import __version__ 13 14 15 class APIVersionMiddleware(object): 16 """ 17 Return an error if a client request is incompatible with this REST API 18 version, and include that REST API version with each response. 19 """ 20 21 def process_request(self, request): 22 """ 23 Return a 405 "Not Allowed" if the request's client major version 24 doesn't match this controller's REST API major version (currently "1"). 25 """ 26 try: 27 client_version = request.META['HTTP_DEIS_VERSION'] 28 server_version = __version__.rsplit('.', 2)[0] 29 if client_version != server_version: 30 message = { 31 'error': 'Client and server versions do not match. ' + 32 'Client version: {} '.format(client_version) + 33 'Server version: {}'.format(server_version) 34 } 35 return HttpResponse( 36 json.dumps(message), 37 content_type='application/json', 38 status=status.HTTP_405_METHOD_NOT_ALLOWED 39 ) 40 except KeyError: 41 pass 42 43 def process_response(self, request, response): 44 """ 45 Include the controller's REST API major and minor version in 46 a response header. 47 """ 48 # clients shouldn't care about the patch release 49 response['DEIS_API_VERSION'] = __version__.rsplit('.', 1)[0] 50 response['X_DEIS_API_VERSION'] = response['DEIS_API_VERSION'] # DEPRECATED 51 return response