Back
Mar 2, 2017

API versioning with django rest framework?

Igor Tokarev

What the versioning is used for?

We often handling API server updates including backwards-incompatible changes when upgrading web applications. At the same time we update the client part, therefore, we did not experience any particular difficulties.

Though, in the case of handling API updates for a mobile app, the process of upgrading applications in the user's phone does not occur simultaneously. Moreover, users may have various versions of the application, and we have to keep all API versions up and running.

An app should have a nice versioned API: changes and new functionalities have to be implemented in the new API versions, not just in one same version. Existing clients can continue to use the old compatible API version with the new one. New or upgraded clients can use a new version.

Versioning schemes supported in drf

DRF supports multiple versioning schemes:

AcceptHeaderVersioning – transfer of the version number through the Accept request header:

false

URLPathVersioning – adding the version to a resource address of a variable (the path is specified in the DRF through the VERSION_PARAM parameter):

false

NamespaceVersioning – the version is given through the url namespace:

false

HostNameVersioning – the version is set by the domain name:

false

QueryParameterVersioning – transfer of the version through the GET parameter:

false

Versioning the code

The first versioning method is described in the Django REST Framework documentation.

Creating Serializer and ViewSet:

false

If we need to change/delete/add a field, we create a new serializer and change fields in it.

false

And then we redefine the get_serializer_class method in AccountViewSet:

false

That’s one way to redefine the serializer, permission class and other methods in ViewSet.

As well, I found a small application for versioning.

I did not use it, though, we got from the description that we can set the Serializer and Parser and use them to set the transform’s base class.

false

Setting the basic version:

false

And we do so for every new version:

false

The backwards methods would apply from the end to the beginning when receiving data from client, i.e. 0004, 0003, 0002. When sending data to the client, the forwards would apply in direct order 0002, 0003, 0004.

How we handled versioning

The basic idea was to split the API into the modules and use the class inheritance.

The following directory structure is:

false

base - the first version of our API.

Further, in the versioned folder, we created a folder for each version. In this project we had two external clients: iOS and Android + our WEB client. The WEB client has always used the latest version of the API.

Each successive API version was prepared this way: we made changes in the existing API v2; after the iOS and Android client release (they released them at the same time), we’ve created v3 and stopped making changes to v2. We could change Version 3 before the next release of the external clients.

DRF uses classes to create ViewSet, Serializer, Permission. We used inheritance between the API versions to not fully copy ViewSets and Serializers.

false
false
false

Further, we generally connected urls.py to the first API version:

false

We removed the first_name, last_name fields and added the full_name fields. Then we created v2 keeping the backward compatibility and addde the serializers.py, views.py, router.py directories and files:

false

inheriting the basic version:

false
false
false

Updating the urls.py root:

false

You may notice that we’ve inherited UserViewSet, and we did not have to update BookViewSet, that’s why we used the first version of the view.

Pros and cons of the approach

Pros:

  • Simple implementation
  • Versions of relevant classes are classified by the module base, v1, v2, etc.
  • Easy to navigate the code
  • No need to copy the view source code and serializers
  • Less IF nesting

Cons:

  • Deep level of nesting occurs when there are a large number of API versions
  • Slightly harder to debug code due to nest

Read Also: SaaS Development Trends

CONCLUSION

It can be quite difficult to manage the API versions, especially if you want to do it properly. You can find pros and cons in each versioning method. And the inheritance method proved to be good due to small number of versions in our project.

More thoughts