Skip to content

fndmiranda/flask-api

Repository files navigation

Boilerplate of Python Flask Api, SQLAlchemy, migrations and OAuth2

This project simplifies the creation of a Python project with the Flask framework, database migrations and authentication with OAuth2.

Installation

Virtualenv

Create a Python version 3.7 environment and activate it.

Clone

Execute the following command to get the latest version of the project:

$ git clone --recursive [email protected]:fndmiranda/flask-api.git flask-api

Install dependencies

Execute the following commands to install dependencies:

$ pip install --upgrade pip
$ pip install -r requirements/development.txt

Configure the application environment

Copy and edit the .env file as needed.

$ cp .env.example .env

Run migrations

Execute the following command to create a migration:

$ flask app migrate:create "Create catalog_products table"

The migrations will be created in the alembic/versions folder.

Execute the following command to upgrade to last revision:

$ flask app migrate:run head

Or pass the version by parameter, see the example: Below, we use ae1 to refer to revision ae1027a6acf. Tre migration will stop and let you know if more than one version starts with that prefix.

$ flask app migrate:run ae1

Execute the following command to revert to an version:

$ flask app migrate:revert -1

Relative identifiers may also be in terms of a specific revision. For example, to upgrade to revision ae1027a6acf plus two additional steps:

$ flask app migrate:revert ae10+2

Execute the following command to display the current revision for a database.

$ flask app migrate:current

Execute the following command to list the history of migrations:

$ flask app migrate:history

The --verbose option will show us full information about each revision:

$ flask app migrate:history --verbose

User

Execute the following command to create a new user:

$ flask user user:create --admin

Or pass the parameters to silent create.

$ flask user user:create --name "YourName" --email [email protected] --password yourpass --admin

OAuth2 authentication

Execute the following command to create a new OAuth2 client:

$ flask auth client:create --scope=profile

The output will be something like.

New OAuth2 client created successfully.
Client ID: 8UTYVHgVBoNGj69pjS5e21Xa
Client secret: Cfa5VDPyHKJDxESc0ASKh8FmCbBEI4Fukp3jFxymnf0oxIcH
Grant type: ['password']

Optionally, the following arguments can be passed:

Argument Accept Default Description
--name string APP_NAME constant Name of the client.
--user integer None A client is registered by a user (developer) on your website.
--uri URI None Application URI.
--grant_type [password, authorization_code] password Grant type, separated by comma
--redirect URI None Where the user will be redirected after authorizing.
--response string code Response type of solicitation.
--scope string None Access level.

You can now request a token:

POST /oauth/token HTTP/1.1
Host: http://127.0.0.1:5000
Content-Type: application/x-www-form-urlencoded
Accept: application/json

grant_type=password&client_id=8UTYVHgVBoNGj69pjS5e21Xa&client_secret=Cfa5VDPyHKJDxESc0ASKh8FmCbBEI4Fukp3jFxymnf0oxIcH&[email protected]&password=testpass&scope=profile

An example successful response:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
    "access_token": "7iON6iBSkLWwHQJB3OmxtUezgBdbwDpKNJyLrrf8PH",
    "expires_in": 864000,
    "refresh_token": "DashRD7mBqFEz5vUPiHzer8QsidtasqXkANcpxaUHtJnBMPM",
    "scope": "profile",
    "token_type": "Bearer"
}

Run application

Execute the following command to run the application in a development environment:

$ make runserver-dev

Execute the following command to run the application with gunicorn:

$ make runserver

Display registered routes.

Execute the following command to list all registered routes:

$ flask routes

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

Boilerplate from Python Flask Api with MongoDB

Resources

License

Stars

Watchers

Forks

Packages

No packages published