Flask Api Tutorial

screenshot of Flask Api Tutorial

Flask Api Tutorial

Boilerplate for a Flask REST API with JWT-based authentication, built with SQLAlchemy, Flask-RESTx, PyJWT, and pytest. This is a companion repo for a multi-part tutorial series on my personal website.

Overview

The Flask API tutorial is a project that provides a comprehensive guide on creating a Flask API with JWT-based authentication. The tutorial aims to provide a robust and secure REST API that can be used in real-world production applications. The tutorial emphasizes the importance of automated test coverage and explains how to configure pytest plugins for code style enforcement, linting, and test coverage calculation. The tutorial also covers deploying the application as part of a CI/CD system using GitHub Actions and Azure Pipelines.

Features

  • Secure authentication using JSON Web Tokens (JWT)
  • Built on Flask, SQLAlchemy, Swagger UI, and pytest
  • Automated test coverage using pytest plugins
  • Deployment as part of a CI/CD system with GitHub Actions and Azure Pipelines

Installation

To install the Flask API tutorial project, follow these steps:

  1. Clone the project repository from GitHub.
git clone https://github.com/<username>/<project>.git
  1. Install the required packages and extensions.
pip install Flask-RESTx Flask-SQLAlchemy Flask-Migrate PyJWT pytest tox
  1. Configure pytest plugins for code style enforcement, linting, and test coverage.
pytest --cov=myapp --flake8 --mypy
  1. Execute test cases using tox to simulate end-user environment.
tox
  1. Deploy the application as part of a CI/CD system using GitHub Actions or Azure Pipelines.
# GitHub Actions
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.x

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run tests
        run: pytest

      - name: Deploy to Azure
        uses: azure/webapps-deploy@v2
        with:
          app-name: <your_app_name>
          slot-name: <your_slot_name>
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
          package: .

For Azure Pipelines deployment, refer to the official Azure Pipelines documentation.

Summary

The Flask API tutorial project provides a comprehensive guide on creating a secure and robust REST API using Flask, SQLAlchemy, and JWT-based authentication. The tutorial emphasizes the importance of automated test coverage and demonstrates the deployment of the application as part of a CI/CD system using GitHub Actions and Azure Pipelines. Developers can refer to the tutorial series on the author's website for detailed step-by-step instructions on building the application and can provide feedback through GitHub issues or commenting on the website.