https://github.com/khteh/pythonrestapi
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.
https://github.com/khteh/pythonrestapi
http2-web-server hypercorn python3 quart
Last synced: about 2 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/khteh/pythonrestapi
- Owner: khteh
- Created: 2020-05-31T11:32:43.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-28T09:56:56.000Z (12 months ago)
- Last Synced: 2024-05-29T01:22:41.118Z (12 months ago)
- Topics: http2-web-server, hypercorn, python3, quart
- Language: Python
- Homepage:
- Size: 585 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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 -f `DB_foo` in `/etc/pythonrestapi_config.json`
- 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
```
$ pipenv run pytest -v
$ python -m pytest
```# Continuous Integration:
- Integrated with CircleCI
# Start the application:
- `./quart.sh`
## Create User:
- POST https://localhost:4433/users/create with the following JSON data:
```
{
"firstname": "First Name",
"lastname": "LastName",
"email": "[email protected]",
"password": "P@$$w0rd"
}
```## Login:
- POST https://localhost:4433/users/login with the following JSON data:
```
{
"email": "[email protected]",
"password": "P@$$w0rd"
}
```
- Sample response:
```
{
"jwt_token": "token string"
}
```## Subsequent request header:
```
Key: api-key
Vaue: jwt_token from the login response
```## Create Author:
- POST https://localhost:4433/authors/create with the following JSON data:
```
{
"email": "[email protected]",
"firstname": "JK",
"lastname": "Rowing"
}
```## Create Book:
- POST https://localhost:4433/books/create with the following JSON data:
```
{
"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
```- visit https://localhost:4433
- visit https://localhost:4433/fibonacci/
- visit https://localhost:4433/users/all
- visit https://localhost:4433/authors/all
- visit https://localhost:4433/books/all## Diagnostics
- HTTP/3 curl:
```
$ docker run --rm ymuski/curl-http3 curl --http3 --verbose https://:/healthz/ready
```