A minimal implementation of REST API using Django REST framework. This is a demonstrative project to understand the implementation of following features in RESTFull web services:
- Django project configuration for installed apps, database, authentication, testing etc.
- Creating MSV (Models, Serializers, Views) as per DRF API guidelines.
- Common customization for django admin interface and urls.
- User request Authentication using Token Authentication and Permission Policy.
- Customizable Pagination which allows you to split large result sets into individual pages.
- Test Driven Development.
- Python==3.6.9
- Django==3.0.4
- djangorestframework==3.11.0
- django-nose==1.4.6
pip install -r requirements.txt
Endpoint | HTTP Method | CRUD Operation |
---|---|---|
api/review/institute/ |
POST, GET | Create, Retrieve ALL |
api/review/institute/{id}/ |
GET, PUT, DELETE | Retrieve, Update, Destroy |
api/user/ |
POST | Create User |
api/user/login/ |
POST | Login User |
get-token/ |
POST | Request a token |
We need to do following changes in reviewapp/settings.py
in order to enable token authentication.
First add restframework.authtoken
to your INSTALLED_APPS
. Second add the TokenAuthentication
to REST_FRAMEWORK
INSTALLED_APPS = [
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Third party Apps
'rest_framework',
'rest_framework.authtoken', # here
'django_nose',
# Local Apps
'review',
'user',
]
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
...
}
The simplest ways we can get a token are -
python manage.py drf_create_token tyrian
Or by using httpie
http post http://127.0.0.1:8000/get-token/ username=kailas password=kailas123
Pagination allows you to control how many objects per page to be returned. To enable it add the following lines to REST_FRAMEWORK dictionary in reviewapp/settings.py
REST_FRAMEWORK = {
...
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
...
}
- Verification of user registration with invalid password.
- Verification of user registration with valid data.
- Verification of user registration with unique username validation.
- User login without password authentication.
- User logn with password authentication.
- User login with valid credentials.
- Endpoint validation
- Create institute.
- List institutes.
- Retrieve an institutes with given id.
- Update an institute.
- Partial update of an institute.
- Delete an institute with given id.
You can set the test request default format by adding a TEST_REQUEST_DEFAULT_FORMAT
key in REST_FRAMEWORK
cofig. dictionary in your reviewapp/settings.py
REST_FRAMEWORK = {
...
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
...
}
python manage.py test
There are multiple ways to call the API, such as postman, curl, httpie.
- Install the postman
sudo snap install postman
- Start the django server
python manage.py runserver
- Select request type, Set headers and send request
- Install httpie using pip
pip install httpie
- Start the django server
python manage.py runserver
- Access API -
http http://127.0.0.1:8000/api/review/institute/2/ "Authorization: Token 39643b8bec57a7288ff4b68ce7199c9398ae7699"
- Response:
{
"id": 2,
"institute_name": "COEP",
"address": "shivaji nagar pune",
"pin_code": 411005,
"office_mail": "[email protected]",
"phone_number": null,
"website": null,
"institute_type": "GO",
"founded_in": null,
"affiliated_to": null,
"approved_by": null,
"owner": 1
}
- Install curl
sudo apt install curl
- Start the django server
python manage.py runserver
- Access API using curl
curl http://127.0.0.1:8000/api/review/institute/ -H 'Authorization: Token 39643b8bec57a7288ff4b68ce7199c9398ae7699'
- Response
{"count":2,"next":null,"previous":null,"results":[{"id":2,"institute_name":"AISSMSIOIT","address":"shivaji nagar pune","pin_code":411005,"office_mail":"[email protected]","phone_number":null,"website":null,"institute_type":"GO","founded_in":null,"affiliated_to":null,"approved_by":null,"owner":1},{"id":4,"institute_name":"PICT","address":"pune","pin_code":null,"office_mail":null,"phone_number":null,"website":null,"institute_type":"GO","founded_in":null,"affiliated_to":null,"approved_by":null,"owner":1}]}
Note - Authorization token is required as we are using token based authentication.