Bug 1145720 - Setup api versioning

This commit is contained in:
Mauro Doglio 2015-07-22 20:14:21 +02:00
Родитель 21ca58c3d2
Коммит e3d4c3162d
3 изменённых файлов: 49 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,39 @@
from rest_framework.decorators import APIView
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from rest_framework.exceptions import NotAcceptable
from django.conf import settings
class RequestVersionView(APIView):
def get(self, request, *args, **kwargs):
return Response({'version': request.version})
factory = APIRequestFactory()
def test_unsupported_version():
view = RequestVersionView.as_view()
request = factory.get('/endpoint/', HTTP_ACCEPT='application/json; version=foo.bar')
try:
response = view(request)
except NotAcceptable:
pass
assert response.data == {u'detail': u'Invalid version in "Accept" header.'}
def test_correct_version():
view = RequestVersionView.as_view()
version = settings.REST_FRAMEWORK['ALLOWED_VERSIONS'][0]
request = factory.get('/endpoint/',
HTTP_ACCEPT='application/json; version={0}'.format(version))
response = view(request)
assert response.data == {'version': version}
def test_default_version():
view = RequestVersionView.as_view()
request = factory.get('/endpoint/', HTTP_ACCEPT='application/json')
response = view(request)
version = settings.REST_FRAMEWORK['DEFAULT_VERSION']
assert response.data == {'version': version}

Просмотреть файл

@ -600,6 +600,8 @@ class TreeherderClient(object):
"""
PROTOCOLS = {'http', 'https'} # supported protocols
API_VERSION = '1.0'
ACCEPT_HEADER = 'application/json; version={0}'.format(API_VERSION)
UPDATE_ENDPOINT = 'job-log-url/{}/update_parse_status'
RESULTSET_ENDPOINT = 'resultset'
@ -651,7 +653,7 @@ class TreeherderClient(object):
uri = self._get_project_uri(project, endpoint)
resp = requests.get(uri, timeout=timeout, params=params,
headers={'Accept': 'application/json'})
headers={'Accept': self.ACCEPT_HEADER})
resp.raise_for_status()
return resp.json()
@ -665,8 +667,10 @@ class TreeherderClient(object):
uri = self._get_project_uri(project, endpoint)
resp = requests.post(uri, json=data,
headers={'Content-Type': 'application/json'},
headers={'Content-Type': 'application/json',
'Accept': self.ACCEPT_HEADER},
timeout=timeout, auth=auth)
resp.raise_for_status()
def get_option_collection_hash(self):

Просмотреть файл

@ -283,7 +283,10 @@ REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES': {
'jobs': '220/minute',
'resultset': '220/minute'
}
},
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
'DEFAULT_VERSION': '1.0',
'ALLOWED_VERSIONS': ('1.0',)
}
SITE_URL = os.environ.get("TREEHERDER_SITE_URL", "http://local.treeherder.mozilla.org")