Skip to content

Python RestAPI using Quart ASGI framework. It runs on HTTP/2 and will be HTTP/3 when mainstream browsers support it in the near future.

Notifications You must be signed in to change notification settings

khteh/PythonRestAPI

Repository files navigation

PythonFlaskRestAPI

Python RestAPI using Quart ASGI framework. It runs on HTTP/2 and will be HTTP/3 when mainstream browsers support it in the near future.

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

  • run migrations initialization with db init command:
$ pipenv run alembic init 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
$ pytest -v
$ python -m pytest

Continuous Integration:

  • Integrated with CircleCI

Start the application:

  • ./quart.sh

Create User:

{
	"firstname": "First Name",
	"lastname": "LastName",
	"email": "[email protected]",
	"password": "P@$$w0rd"
}

Login:

{
	"email": "[email protected]",
	"password": "P@$$w0rd"
}
  • Response:
{
    "jwt_token": "token string"
}

Subsequent request header:

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

Create Author:

{
    "email": "[email protected]",
    "firstname": "JK",
    "lastname": "Rowing"
}

Create Book:

{
    "author_id": 1,
    "isbn": "123456",
    "page_count": "123",
    "title": "My First 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:

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

About

Python RestAPI using Quart ASGI framework. It runs on HTTP/2 and will be HTTP/3 when mainstream browsers support it in the near future.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published