This Application contains few endpoints that consume the SWAPI (https://swapi.dev/). Following are the basic operations you can perform:
- List of endpoints available to use
- Ability to add new Movie/Planet to the storage
- Listing the Movies/Planets by fetching from SWAPI & storage used
- Searching/Filtering by Planets
Firstly, open your terminal or command prompt and clone this repository.
$ git clone https://github.com/satyasashi/swapi.git
Create a virtual environment
$ python -m venv swapienv
Activate the environment
$ source swapienv/bin/activate
Install dependencies by moving to project path.
$ cd swapi && pip install -r requirements.txt
Do the initial migrations
$ python manage.py migrate
Before we begin our usage, you can run automated tests for this application by using following command. This will run tests listed in favorite/tests.py
module.
(swapienv)$: python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...
----------------------------------------------------------------------
Ran 3 tests in 0.888s
OK
Destroying test database for alias 'default'...
The 3 tests it ran are for operations such as "Adding Movie", "Adding Planet" and "Search" which interacts with SWAPI and DB.
To use the endpoints in this application, navigate to: (http://localhost:8000/)
You will see API ROOT which shows the list of endpoints available in this application and can call endpoints using utilities like http
, curl
etc,. In this scenario, I will be using http
utility.
(swapienv)$ http http://localhost:8000
HTTP/1.1 200 OK
Allow: GET, HEAD, OPTIONS
Content-Length: 85
Content-Type: application/json
Date: Wed, 30 Jun 2021 19:43:00 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"movies": "http://localhost:8000/movies/",
"planets": "http://localhost:8000/planets/"
}
You can click on any endpoint to see list of Movies. These movies are fetched from (http://localhost:8000/movies/) and also it does get all the movies that are saved in our storage. The result will be joined together to show the data in JSON format as a API response.
Here is the sample output
(swapienv)$: http http://localhost:8000/movies/
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 217
Content-Type: application/json
Date: Wed, 30 Jun 2021 19:44:27 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
[
{
"title": "A New Hope"
},
{
"title": "The Empire Strikes Back"
},
{
"title": "Return of the Jedi"
},
{
"title": "The Phantom Menace"
},
{
"title": "Attack of the Clones"
},
{
"title": "Revenge of the Sith"
}
]
You can click on any endpoint to see list of Planets. These planets are fetched from (http://localhost:8000/planets/) and also it does get all the planets that are saved in our storage. The result will be joined together to show the data in JSON format as a API response.
Here is the sample output:
(swapienv)$: http http://localhost:8000/planets/
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 217
Content-Type: application/json
Date: Wed, 30 Jun 2021 19:47:18 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
[
{
"name": "Tatooine"
},
{
"name": "Alderaan"
},
{
"name": "Yavin IV"
},
{
"name": "Hoth"
},
{
"name": "Dagobah"
},
{
"name": "Bespin"
},
{
"name": "Endor"
},
{
"name": "Naboo"
},
{
"name": "Coruscant"
},
{
"name": "Kamino"
}
]
You can filter the Planets by using following endpoint. Trying to filter using non existing data will result:
(swapienv)$: http http://localhost:8000/planets/search/XHSDK/
HTTP/1.1 400 Bad Request
Allow: GET, HEAD, OPTIONS
Content-Length: 2
Content-Type: application/json
Date: Wed, 30 Jun 2021 20:25:29 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{}
Trying to filter with existing data result:
(swapienv)$: http http://localhost:8000/planets/search/Mars/
HTTP/1.1 200 OK
Allow: GET, HEAD, OPTIONS
Content-Length: 22
Content-Type: application/json
Date: Wed, 30 Jun 2021 20:27:13 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
[
{
"name": "mars"
}
]
Movie should have a title
. So when passing data, you should pass the title
as your argument. If not, it will throw This field is required
for that field.
(swapienv)$: http --form http://localhost:8000/movies/ title='Interstellar'
HTTP/1.1 201 Created
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 19
Content-Type: application/json
Date: Wed, 30 Jun 2021 19:52:09 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"title": "Interstellar"
}
Planet should have a name
. So when passing data, you should pass the name
as your argument. If not, it will throw This field is required
for that field.
(swapienv)$: http --form http://localhost:8000/planets/ name='XVHI'
HTTP/1.1 201 Created
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 19
Content-Type: application/json
Date: Wed, 30 Jun 2021 19:52:09 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"name": "XVHI"
}
Accessing the Movie/Planet endpoints now should be showing the information from both SWAPI & our storage.