In the last post, we discussed the Django REST Framework, or DRF for short. The DRF lets you create really simple and powerful APIs with very little coding. Using Models and Model Serializers with generic List and Update Views, you can have a working API in no time at all.
Now that we have a solid foundation for our project and a RESTful API design pattern, we can begin documenting our API as well as provide a cool testing environment with Swagger UI.
To enable Swagger UI in your API project, for Django and DRF...
(.env) $ pip3 install django-swagger
Add this to your root URLs conf.
From the Gist above, we set
schema_view to our Swagger definition.
schema_view = get_swagger_view(title='DRF by Example')
Then wire up the route:
Next, in your browser, go to: http://localhost:8888/api/docs/
Now, start testing your API...
In my example from the last post, we are going to GeoLocate an IP Address. Select locations and expand the list. Select geolocate and click
Try It Out. Enter your IP address and click
You can see in the image above, the nicely formatted response body, reponse headers, response status code.
The DRF makes it ridiculously simple to create killer APIs with little effort using a set of very specific design patterns.
Swagger UI extends the API development toolset by helping you test and document your API while you are building. When testing, use Swagger to check the response body data and response codes the server sends back to the test client. This really helps speed up development and ensures the API is responding correctly.
Learn more about Swagger UI.