github.com/amrnt/deis@v1.3.1/controller/api/middleware.py (about) 1 import json 2 3 from django.http import HttpResponse 4 from rest_framework import status 5 6 from api import __version__ 7 8 9 class APIVersionMiddleware: 10 11 def process_request(self, request): 12 try: 13 # server and client version must match the major release point 14 client_version = request.META['HTTP_X_DEIS_VERSION'] 15 server_version = __version__.rsplit('.', 2)[0] 16 if client_version != server_version: 17 message = { 18 'error': 'Client and server versions do not match. ' + 19 'Client version: {} '.format(client_version) + 20 'Server version: {}'.format(server_version) 21 } 22 return HttpResponse( 23 json.dumps(message), 24 content_type='application/json', 25 status=status.HTTP_405_METHOD_NOT_ALLOWED 26 ) 27 except KeyError: 28 pass 29 30 def process_response(self, request, response): 31 # clients shouldn't care about the patch release 32 response['X_DEIS_API_VERSION'] = __version__.rsplit('.', 1)[0] 33 return response