22[ Django REST framework] ( http://www.django-rest-framework.org/ ) is a powerful and flexible toolkit for building Web APIs.
33
44## Requirements
5- - Pythton 3.6
5+ - Python 3.6
66- Django (1.10, 1.11, 2.0)
77- Django REST Framework
8+ - Django Rest Auth
89
910## Installation
1011```
1112 pip install django
1213 pip install djangorestframework
14+ pip install django-rest-auth
15+ pip install django-allauth
1316```
1417
1518## Structure
@@ -47,12 +50,36 @@ we get:
4750```
4851Instead, if we try to access with credentials:
4952```
50- http -a root:root1234 http://127.0.0.1:8000/api/v1/movies/3
53+ http http://127.0.0.1:8000/api/v1/movies/3 "Authorization: Token 7530ec9186a31a5b3dd8d03d84e34f80941391e3"
5154```
5255we get the movie with id = 3
5356```
5457{ "title": "Avengers", "genre": "Superheroes", "year": 2012, "creator": "admin" }
5558```
59+
60+ ## Login and Tokens
61+
62+ To get a token first we have to login
63+ ```
64+ http http://127.0.0.1:8000/rest-auth/login/ username="admin" password="root1234"
65+ ```
66+ after that, we get the token
67+ ```
68+ {
69+ "key": "2d500db1e51153318e300860064e52c061e72016"
70+ }
71+ ```
72+ ** ALL request must be authenticated with a valid token, otherwise they will be invalid**
73+
74+ We can create new users. (password1 and password2 must be equal)
75+ ```
76+ http POST http://127.0.0.1:8000/rest-auth/registration/ username="USERNAME" password1="PASSWORD" password2="PASSWORD"
77+ ```
78+ And we can logout, the token must be your actual token
79+ ```
80+ http POST http://127.0.0.1:8000/rest-auth/logout/ "Authorization: Token <YOUR_TOKEN>"
81+ ```
82+
5683The API have some restrictions:
5784- The movies are always associated with a creator (user who created it).
5885- Only authenticated users may create and see movies.
@@ -61,11 +88,11 @@ The API have some restrictions:
6188
6289### Commands
6390```
64- http -a root:root1234 http://127.0.0.1:8000/api/v1/movies/
65- http -a root:root1234 GET http://127.0.0.1:8000/api/v1/movies/3
66- http -a root:root1234 POST http://127.0.0.1:8000/api/v1/movies/ title="Ant Man and The Wasp" genre="Action" year=2018
67- http -a root:root1234 PUT http://127.0.0.1:8000/api/v1/movies/3 title="AntMan and The Wasp" genre="Action" year=2018
68- http -a root:root1234 DELETE http://127.0.0.1:8000/api/v1/movies/3
91+ http http://127.0.0.1:8000/api/v1/movies/ "Authorization: Token <YOUR_TOKEN>"
92+ http GET http://127.0.0.1:8000/api/v1/movies/3 "Authorization: Token <YOUR_TOKEN>"
93+ http POST http://127.0.0.1:8000/api/v1/movies/ "Authorization: Token <YOUR_TOKEN>" title="Ant Man and The Wasp" genre="Action" year=2018
94+ http PUT http://127.0.0.1:8000/api/v1/movies/3 "Authorization: Token <YOUR_TOKEN>" title="AntMan and The Wasp" genre="Action" year=2018
95+ http DELETE http://127.0.0.1:8000/api/v1/movies/3 "Authorization: Token <YOUR_TOKEN>"
6996```
7097Finally, I provide a DB to make these tests.
7198
0 commit comments