Flask application just for learning basics about REST API web services.
Implements a dummy database to handle authors, languages, editorials and books.
Database model is conceived just for learning proposes, it can not be considered as a reference for production purposes.
myLibrary shows how to develop a complete REST API service, test it and document it through swagger.
myLibrary REST API has been inspired by Miguel Grinberg Flask Tutorial. His excellent code can be found here.
git clone -b master https://github.com/MarcosFernandez/myLibrary.git
pip3 install virtualenv
virtualenv venv
source venv/bin/activate
(venv) pip3 install Flask
(venv) pip3 install flask-sqlalchemy
(venv) pip3 install flask-migrate
(venv) pip3 install flask-login
(venv) pip3 install PyJWT==1.5.3
(venv) pip3 install Flask-HTTPAuth==4.0.0
(venv) pip3 install python-dotenv
echo "FLASK_APP=myLibrary.py" > .flaskenv
(venv) flask run
(venv) flask db init
(venv) flask db migrate -m "First Migration"
(venv) flask db upgrade
userflask@flaskserver:~/myLibrary$ flask shell
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
App: app [production]
Instance: /home/userflask/myLibrary/instance
>>> from app.models import User
>>> from app import db
>>> user = User()
>>> user.username = 'userflask'
>>> user.set_password('USERPASSWORD')
>>> db.session.add(user)
>>> db.session.commit()
>>>
now exiting InteractiveConsole...
curl -X POST -u userflask:userflask "http://127.0.0.1:5000/api/tokens""
{"token" : "TOKENUSERFLASK"}
curl -X GET "http://127.0.0.1:5000/api/authors" -H "Authorization":"Bearer TOKENUSERFLASK"
curl -X POST "http://127.0.0.1:5000/api/author" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"name":"Bertrand Russell", "country":"England"}'
curl -X GET "http://127.0.0.1:5000/api/author/Bertrand%20Russell" -H "Authorization":"Bearer TOKENUSERFLASK"
{"_links":{"self":"/api/author/Bertrand%20Russell"},"country":"England","id":1,"name":"Bertrand Russell"}
curl -X PUT "http://127.0.0.1:5000/api/author/Manuel%20Garrido" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"name":"Manolo Garrido"}'
curl -X DELETE "http://127.0.0.1:5000/api/author/J.R.R%20Tolkien" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json'
{"success":true}
curl -X POST "http://127.0.0.1:5000/api/language" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"name":"English"}'
curl -X GET "http://127.0.0.1:5000/api/languages" -H "Authorization":"Bearer TOKENUSERFLASK"
curl -X PUT "http://127.0.0.1:5000/api/language/English" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"name":"England"}'
curl -X DELETE "http://127.0.0.1:5000/api/language/Italian" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json'
{"success":true}
curl -X POST "http://127.0.0.1:5000/api/editorial" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"name":"Simon&Schuster", "country":"USA"}'
curl -X GET "http://127.0.0.1:5000/api/editorials" -H "Authorization":"Bearer TOKENUSERFLASK"
curl -X PUT "http://127.0.0.1:5000/api/editorial/Simon%26Schuster" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"country":"England"}'
curl -X DELETE "http://127.0.0.1:5000/api/editorial/Plaza%26Janes" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json'
curl -X POST "http://127.0.0.1:5000/api/book" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json' -d '{"title":"The problems of Philosophy", "author":"Bertrand Russell","language":"English","editorial":"Simon&Schuster","location":"London","status":"NOT PRESENT"}'
curl -X GET "http://127.0.0.1:5000/api/book/German%20Social%20Democracy" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json'
{"_links":{"author":"/api/author/Bertrand%20Russell","editorial":"/api/editorial/Simon%26Schuster","language":"/api/language/English","self":"/api/book/German%20Social%20Democracy"},"author":"Bertrand Russell","editorial":"Simon&Schuster","id":2,"language":"English","location":"London","status":"NOT PRESENT","title":"German Social Democracy"}
curl -X GET "http://127.0.0.1:5000/api/books" -H "Authorization":"Bearer TOKENUSERFLASK" -H 'Content-Type: application/json'
Runs unit test on all REST APIs endpoints.
python -m unittest tests/test_api_endpoint.py
(venv) pip3 install flask_swagger_ui
(venv) flask run
Open in browser http://127.0.0.1:5000/swagger/ Check swagger JSON: http://127.0.0.1:5000/static/swagger.json
GNU General Public License gpl-3.0
- Marcos Fernandez-Callejo - [email protected]