My intention is to represent and recognize in a simple way the use of the different interaction methods towards a web application through a RESTful-API (REpresentational State Transfer - Application Programming Interface) info.
The use of it, I have programmed it only to interact with the concepts without the application of a database, that is, those data inserted (CREATE), deleted (DELETE), or updated (PUT) will not be permanent, they will be ephemeral data which are represented in JSON format.
It executes the "hypothetical need" to automate pet registration through an API (very simple by the way) in a pet care and adoption center, in this case I have only decided to idealize "dogs
and cats
".
For a quick execution I decided to use a container with the Nginx image (which is a fairly small Debian), deployed docker from a virtual machine (Ubuntu) which he installed from this script:
Once with Docker, I run the container
with a type=Bind
mount in whose target I host the application code.
In the created container
, python3
and its flask
library must be installed (globally), and the code is executed indicating the host and desired port of the application.
In example of the above:
# apt update
# apt install -y python3 python3-flask
For the case of my example, I have called the main app as minimal-rest-api.py
and the target is located in /APP
. So:
# docker container exec -it minimal-app python3 /APP/minimal-rest-api.py
For the various factors of execution methods GET
, CREATE
, UPDATE
, DELETE
, I have applied the Curl
command for the said purpose:
- Listing the entire list of
dogs
in the center:
$ curl -X GET "172.17.0.2:4000/dogs"
{
"dog's list": [
{
"age": "1",
"gender": "male",
"name": "tobby"
},
{
"age": "2",
"gender": "male",
"name": "ringo"
},
{
"age": "4",
"gender": "male",
"name": "chucho"
},
{
"age": "4",
"gender": "female",
"name": "chocolate"
}
]
}
- Listing a
cat
specifying its specific'name'
:
$ curl -X GET "http://172.17.0.2:4000/cats/rocky"
{
"I found this cat on my list": {
"age": "3",
"gender": "male",
"name": "rocky"
}
}
- Inserting a new pet:
$ curl -X POST -H "Content-Type: application/json" -d '{"name":"pirata","age":"7","gender":"male"}' "172.17.0.2:4000/dogs"
{
"Our new pet was successfully added": [
{
"age": "1",
"gender": "male",
"name": "tobby"
},
{
"age": "2",
"gender": "male",
"name": "ringo"
},
{
"age": "4",
"gender": "male",
"name": "chucho"
},
{
"age": "4",
"gender": "female",
"name": "chocolate"
},
{
"age": "7",
"gender": "male",
"name": "pirata"
}
]
}
- Updating pet data:
$ curl -X PUT -H "Content-Type: application/json" -d '{"name":"chucho","age":"5","gender":"male"}' "172.17.0.2:4000/dogs/chucho"
{
"dogs list": {
"age": "5",
"gender": "male",
"name": "chucho"
},
"message": "We have a new pet now!"
}
- Deleting pet data:
$ curl -X DELETE "172.17.0.2:4000/cats/michi"
{
"Our cats": [
{
"age": "1",
"gender": "female",
"name": "blancky"
},
{
"age": "3",
"gender": "male",
"name": "rocky"
}
],
"message": "We have given a kitten up for adoption"
}