Test task. Simple API.
The API allows you to create, read, update, and delete data in the Apps model.
“Apps” is a model that stores external sources that have access to the API.
The Apps model has the following fields: id, title, description, api_key, created_date. Using API you can set the only title, description fields, new api_key, id and current data will be generated automatically.
- python 3.6
- django 2.2
- djangorestframework
- djangorestframework-api-key
Create a virtual environment and install dependencies with pipenv:
pipenv install
pipenv shellApply migrations and create a user:
cd apps_api_project
python manage.py migrateThere are two ways to create your first API key:
- Create user
and use django admin to add new API key
python manage.py createsuperuser - Or you can add API key in django shell:
python manage.py shell >>> from rest_framework_api_key.models import APIKey >>> api_key, key = APIKey.objects.create_key(name="my_first_api_key") >>> key 'aby3suNl.SZ2iq5vPdI441VOjhz23CqCVbYqKXg0n'
python manage.py runserver Clients must pass their API key via the Authorization header. It must be formatted as follows:
Authorization: Api-Key ********
where ******** refers to the generated API key.
http://127.0.0.1:8000/api/ - list of the apps
http://127.0.0.1:8000/api/app_name - data of the 'app_name' app
You can use different HTTP clients for working with API, for instance, it can be curl, HTTPie or Postman.
I've provided the examples with HTTPie.
http http://127.0.0.1:8000/api/ 'Authorization: Api-Key ********'
http POST http://127.0.0.1:8000/api/ 'Authorization: Api-Key ********' title=new_app description='new app description'
http http://127.0.0.1:8000/api/new_app 'Authorization: Api-Key ********'
http PUT http://127.0.0.1:8000/api/new_app 'Authorization: Api-Key ********' title='new_app' description='updated app description
http DELETE http://127.0.0.1:8000/api/new_app 'Authorization: Api-Key ********'