AlexKupreev / easyto-api

RESTful API boilerplate built with Flask and poetry

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EasyTo API backend

This is a minimalistic ready-to-go API backend using Flask.

Requirements

  • poetry
  • make (to ease if docker to be used for development/testing)

Configuration

  • Rename and set environment variables:

    • .example.env -> .env - development server
    • .example.testenv -> .testenv - testing setup
  • For docker setup there are specific configuration files (as redis setup is different):

    • .dockerenv - for environment setup
    • .dockertestenv - for testing setup
  • Specific variables:

    • CELERY_BROKER_URL - broker connection in format
    redis://:password@hostname:port/db_number
    
    • CELERY_RESULT_BACKEND_URL - backend connection in format
    redis://:password@hostname:port/db_number
    

Development approach

There are two possible ways of making development:

  1. Using virtual environment (created by poetry)
  2. Using docker

They can be taken alone or mixing together.

Development using virtual environment

Running the backend

To have an instance up and running locally:

  • Go to
poetry shell
  • Init database:
manage db upgrade
manage init
  • Run worker (redis backend should be running - e.g. $ redis-server)
celery worker -A easyto_api.celery_app:app --loglevel=info
  • Run development server (will be available at http://127.0.0.1:5000/ or http://localhost:5000/)
manage run
  • Alternatively run gunicorn as wsgi server (available at http://127.0.0.1:8000)
gunicorn easyto_api.wsgi:app

Testing and linting

  • Go to
poetry shell
  • You can use pytest, tox, as well as flake8 and black like
pytest
tox
tox -e lint
flake8
black .
  • Also you can use them from outside of shell:
poetry run pytest
poetry run flake8
poetry run black .

Development using Docker

Running the backend

Use make from the root project directory (or you can take docker commands from Makefile):

  • initializing the instance (backend will be available at http://127.0.0.1:5000/ or http://localhost:5000/):
(sudo) make init
  • shutting down the instance:
(sudo) docker-compose down

Testing and linting

  • running all the tests (still not properly working):
(sudo) make test

Authentication

  • Login to get access and refresh tokens
curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "password"}' http://localhost:5000/auth/login

response:
{
  "access_token": "%access_token%",
  "refresh_token": "%refresh_token%"
}
  • Make signed API call
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer %access_token%" http://localhost:5000/api/v1/endpoint
  • Get new access token using refresh token
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer %refresh_token%" http://localhost:5000/auth/refresh

response:
{
  "access_token": "%access_token%"
}
  • Revoke access token using access token
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer %access_token%" http://localhost:5000/auth/revoke_access
  • Revoke refresh token using refresh token
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer %refresh_token%" http://localhost:5000/auth/revoke_refresh

Credits

About

RESTful API boilerplate built with Flask and poetry


Languages

Language:Python 94.8%Language:Makefile 2.0%Language:Dockerfile 1.8%Language:Mako 1.4%