(Summary of a talk at the Amsterdam python meetup)
Ehmad Mokhtar used Django a lot. And he loves web APIs: you can have many intergrations. You build one back-end and it can be used by many other services and front-ends. It is also core for microservices and “service oriented architectures”.
So: APIs = business! Without APIs, you have no intergrations. So often you won’t have as much business.
What is a RESTful API? It is an architectural style that uses HTTP: you work on URLs (“resources”) and you use HTTP verbs (“GET”, “DELETE”, “POST”).
If you build an API, developers are your API customers. So you need, for instance, great documentation. You need to keep that in mind all the time.
If you use django and you want to build an API: use django rest framework (DRF). DRF feels like django itself. It uses the ORM. And it is perhaps even better documented than Django itself.
A tip he has is to use two views per model. In django, you can have a separate list view, a delete view, a regular view, etc. In DRF you are best off having two:
You could use a generic single ModelViewSet, but Emad prefers having control of his URLs.
A tip: don’t go too deep with your hierarchy.
Another tip: version your API from the beginning. Prefix your URLs with
/v2/, for instance. This reduces breakage.
And: always return the coorect HTTP status codes. Look at
https://httpstatuses.com/ . If you create a customer, return a
CREATED. If you request an unknown customer:
404 NOT FOUND.
(Note by Reinout: look at https://http.cat/, too :-) )
django-guardianis a third-party django app.
Handy: there is support for throttling:
AnonRateThrottlefor anonymous users.
UserRateThrottle: throttling per authenticated user.
ScopeRateThrottlefor specific API scopes. You can set that per API endpoint.
When testing, use DRF test classes.
APISimpleTestCase, etcetera. They’re specially made to make your tests
For functional tests, you can use vcrpy to make mocking your web API much faster. You use it to give it a sort of test fixture instead of having it call the real API.
Another tip: use the ‘postman’ app to play with your API and to test it out in detail. He demoed the API: nice!
My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):