- Extensive documentation here.
- Integrated with Pipenv for package managing.
- Fast deloyment to heroku with
$ pipenv run deploy
. - Use of
.env
file. - SQLAlchemy integration for database abstraction.
Important: The boiplerplate is made for python 3.7 but you can easily change the
python_version
on the Pipfile.
The following steps are automatically runned withing gitpod, if you are doing a local installation you have to do them manually:
-Need to install: python3 -m pip install flask-jwt-extended
pipenv install flask-jwt-extended
pip3 install requests
python3 -m pip install pandas
python3 -m pip install requests
pipenv install;
mysql -u root -e "CREATE DATABASE example";
pipenv run start;
pipenv run init;
pipenv run migrate;
pipenv run upgrade;
There is an example API working with an example database. All your application code should be written inside the ./src/
folder.
- src/main.py (it's where your endpoints should be coded)
- src/models.py (your database tables and serialization logic)
- src/utils.py (some reusable classes and functions)
- src/admin.py (add your models to the admin and manage your data easily)
For a more detailed explanation, look for the tutorial inside the docs
folder.
You have to migrate and upgrade the migrations for every update you make to your models:
$ pipenv run migrate (to make the migrations)
$ pipenv run upgrade (to update your databse with the migrations)
python 3.6+
and MySQL
installed on your computer and MySQL is running, then run the following commands:
$ pipenv install (to install pip packages)
$ pipenv run migrate (to create the database)
$ pipenv run start (to start the flask webserver)
⚠️ If you don't have docker installed on your computer, please go get docker⚠️ If on linux, install docker-compose following these instructions- Clone the repo and create a
.env
file; complete key-value pairs as proposed on.env.DockerExample
file - Run
docker-compose up
and give it a few minutes - Have some coffee (Really large coffee) 5.1 Delete Migration
- Your mysql service should be up on port 33060 (from host, a.k.a. your OS)
- Your api service should be up on port 3000 (from host, also your OS)
- Go to Docker Desktop, run flask-rest-hello and run cli
- Run on terminal:
mysql
- Now you can use commands as:
show databases;
use example;
show tables;
create database NameOfDB;
drop database NameOfDB; describe contact;
This template is 100% compatible with Heroku[https://www.heroku.com/], just make sure to understand and execute the following steps:
// Install heroku
$ npm i heroku -g
// Login to heroku on the command line
$ heroku login -i
// Create an application (if you don't have it already)
$ heroku create <your_application_name>
// Commit and push to heroku (commited your changes)
$ git push heroku master
pipenv run migrate
to implement change on Database tables at models.py to folder Migrationpipenv run upgrade
to update database with changes.
- Just eliminate folder migration.
-
- For docker users only have to use
docker-compose up --build
- For docker users only have to use
-
- Go to project in docker and run terminal
pipenv run init
pipenv run migrate
pipenv run upgrade
, then open mysql terminal on docker and run:drop database example;
andcreate database example;
- Go to project in docker and run terminal
- Try to save database, DO NOT delete migration folder
- Read /docs
Enter in mySQL with: `mysql -h localhost -u your_user`
drop database contact;
create database contact; (remember pipenv run migrate and pipenv run upgrade after create)
use contact;
show tables;
ALTER TABLE recipeingredients MODIFY id int NOT NULL AUTO_INCREMENT; (parece ya no ser necesario este paso)
exit
This is a POST endpoint, so you will need:
{
"email":"email@example.com",
"name":"aName",
"last_name":"aLastName",
"username":"some_user",
"password":"notEasy123"
}
then, if everything is correct, server will respond:
{
"email": "email@example.com",
"id": 1,
"last_name": "alastname",
"name": "aname",
"status": true,
"username": "some_user"
}
This is a GET endpoint, you will obtain a list o all users
This is a GET endpoint, you will need enter the user id to obtain data
When this endpoint is used with PATCH method, you can change user data as password, etc.
Just enter the following info:
{
"email":"email@example.com",
"password":"12345",
"name":"new Name",
"last_name":"new Last Name"
}
Note: This PATCH method needs a re-work for security reasons.
You need copy to your .env file the APP_JWT_SECRET LINE (with versión jwt extended, use this lib: create_access_token)
This is a POST endpoint, enter data as:
{
"username":"some_user",
"password":"notEasy123"
}
or this way works too:
{
"email": "email@example.com",
"password":"notEasy123"
}
if everything is allright you will get:
{
"email": "email@example.com",
"id": 1,
"jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTI5MDg1MDYsIm5iZiI6MTYxMjkwODUwNiwianRpIjoiNTNlM2RiOTgtYjI0Ni00NWFkLWFkOTMtOTUxZTZhMDY0ODUzIiwiZXhwIjoxNjEyOTA5NDA2LCJpZGVudGl0eSI6MSwiZnJlc2giOmZhbHNlLCJ0eXBlIjoiYWNjZXNzIiwiY3NyZiI6ImJlOTkzNDE5LWRiZjgtNGQ4MC1hMTVlLWM0OGQyOGJjNGY0NyJ9.s88fi9FzIMWE7AyYRfwDDc-jottegqeMatscAMjr9B0",
"last_name": "alastname",
"name": "aname",
"status": true,
"username": "some_user"
}
This is a GET endpoint, it will retrieve Authorization.
For that you must enter jwt generated Token at login endpoint, as Bearer Token on HEADER.
Example:
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MTI5MDg1MDYsIm5iZiI6MTYxMjkwODUwNiwianRpIjoiNTNlM2RiOTgtYjI0Ni00NWFkLWFkOTMtOTUxZTZhMDY0ODUzIiwiZXhwIjoxNjEyOTA5NDA2LCJpZGVudGl0eSI6MSwiZnJlc2giOmZhbHNlLCJ0eXBlIjoiYWNjZXNzIiwiY3NyZiI6ImJlOTkzNDE5LWRiZjgtNGQ4MC1hMTVlLWM0OGQyOGJjNGY0NyJ9.s88fi9FzIMWE7AyYRfwDDc-jottegqeMatscAMjr9B0
if everything is ok, you will get:
{
"msg": "Welcome, <Contact 'some_user'>"
}
This could be used as GET endpoint, you will obtain a list o all ingredients.
When this endpoint is used with POST method will be acting automatically inside of
/recipes endpoint
This could be used as GET endpoint, you will obtain a list o all recipes.
When this endpoint is used with POST method, data should be as:
{
"description":"Description of the recipe",
"name":"Name of recipe",
"instructions":"Some instructions of how to cook",
"tags":"tags will help to find recipe",
"img_url":"test",
"ingredients": ["onion", "rice"],
"date_published":"2021-02-20T14:04"
}
It will create necesary ingredients of recipes automatically.
At the moment, response body will be something like:
[
{
"date_published": "Sat, 20 Feb 2021 14:04:00 GMT",
"description": "description of the recipe test",
"id": 21,
"img_url": "test",
"ingredients_used": "[onion, rice]",
"instructions": "Some instructions of how to cook",
"likes": 0,
"name": "name of recipe test",
"price": 99.99,
"score": 9,
"tags": "tags will help to find recipe"
}
]
This is a GET endpoint, you will need enter the recipe id to obtain data
{
"date_published": "Sat, 20 Feb 2021 14:04:00 GMT",
"description": "description of the recipe3",
"id": 3,
"img_url": "test",
"ingredients_used": "['onion', 'potato', 'rice', 'tomato']",
"instructions": "Some instructions of how to cook",
"likes": 0,
"name": "name of recipe3",
"price": 99.99,
"score": 9,
"tags": "tags will help to find recipe"
}
This is a POST endpoint, you will need enter the ingredients name (no matter if uppercase, or lowercase) to obtain ID of recipes where those ingredients are beeing used.
{
"search":["onion","rice"]
}
if everything is correct, you will get:
{
"response": [1,2,3]
}
After that, you can use /recipes/<int:position> endpoint to obtain the specific recipe
Note: this response have Recipes ID and is ordered by counts of match of all ingredients between recipes. More coincidences of intgredients means to be at first places in list.
To run this test, just navigate to folder: ./src/
Then apply this command:
`python dataAuto.py`
it will send 20 recipes with random ingredients to /recipes endpoint.
Could take some time.
We are making this API as part of back-end of our final 4Geeks Academy Fullstack course project.
In alphabetical order:
Becerra, Humberto / Martínez, Antonio / Pérez, Vincent / Useche, César /