Skip to content

Python RestAPI using Quart web microframework and served using Hypercorn ASGI and WSGI web server.

Notifications You must be signed in to change notification settings

khteh/PythonRestAPI

Folders and files

NameName
Last commit message
Last commit date
Apr 6, 2025
Jan 4, 2024
Oct 1, 2022
Mar 27, 2025
Apr 24, 2025
Apr 26, 2025
May 31, 2020
Sep 25, 2021
Mar 26, 2021
Apr 24, 2025
Apr 18, 2025
Apr 26, 2025
Apr 10, 2025
Oct 1, 2022
Sep 28, 2021
Mar 27, 2025
Mar 27, 2025
Mar 27, 2025
Mar 27, 2025
Mar 27, 2025
Feb 23, 2025

Repository files navigation

PythonFlaskRestAPI

Python RestAPI using Quart HTTP/3 ASGI framework.

Environment setup

  • On a development host, create a virtual environment for development and test. Both must be using the same virtual environment.
  • Virtual environment is optional for CI/CD do because everything runs in a docker container.
  • Add the following to .env for database access credentials:
DB_USERNAME=username
DB_PASSWORD=password

Clean up pipenv

  • pipenv --rm
  • pipenv --clear
  • pipenv lock --clear --verbose

Setup new pipenv

$ pipenv install
$ pipenv shell

Database setup:

  • This project uses PostgreSQL database with SQLAlchemy ORM with marshmallow for object SerDes.

Install python modules

$ pipenv install --python=/path/to/python
$ cd src
$ pipenv install --python=/path/to/python
$ cd test
$ pipenv install --python=/path/to/python

Create Database

  • Firstly, create an empty database "library" in PostgreSQL

Database Migration

  • Copy env.py to migrations/ folder.

  • Set the values DB_foo in /etc/pythonrestapi_config.json

  • run migrations initialization with db init command:

    $ pipenv run alembic init migrations
    $ cp env.py migrations
    $ pipenv run alembic revision --autogenerate -m "Initial migration"
    $ pipenv run alembic upgrade head
    
  • There will be 3 tables, "users", "books", and "authors" created in the PostgreSQL database "library" after the upgrade.

Test using PyTest:

  • There are 7 test cases
    • JWT token generation and decoding
    • HomeController
    • FibonacciController
    $ pipenv run pytest -v
    $ python -m pytest
    

Continuous Integration:

  • Integrated with CircleCI

Start the application:

  • ./hypercorn.sh

Curl

  • Add Host header which is defined as server_names in hypercorn.toml
curl --http3 --insecure -v https://localhost:4433/<path> -H "Host: khteh.com"

Browser

  • Add extensions to add Host header based on filter

Create User:

  • POST https://localhost:4433/users/create with the following JSON data:
    {
        "firstname": "First Name",
        "lastname": "LastName",
        "email": "firstname.lastname@email.com",
        "password": "P@$$w0rd"
    }
    

Login:

  • POST https://localhost:4433/users/login with the following JSON data:
    {
        "email": "firstname.lastname@email.com",
        "password": "P@$$w0rd"
    }
    
  • Sample response:
    {
        "jwt_token": "token string"
    }
    

Subsequent request header:

Key: api-key
Vaue: jwt_token from the login response

Create Author:

Create Book:

Delete an author:

  • Books table has a foreign key id to Authors.id and this is defined as required in BookSchema.
  • Therefore, when a DELETE RESTful operation is sent to the application to delete an author which has associated book:
mysql.connector.errors.IntegrityError: 1048 (23000): Column 'author_id' cannot be null

Get Requests:

Diagnostics

  • HTTP/3 curl:
$ docker run --rm ymuski/curl-http3 curl --http3 --verbose https://<nodeport service>:<nodeport>/healthz/ready