Open source library with useful utils for fast development
Project description
hexfrost-toolbox - batteries for FastAPI projects
Open source library with useful utils for fast development
Installation
pip install hexfrost-toolbox
Usage
Test Client
from toolbox.testing import debug_client
app = FastAPI()
async with debug_client(app) as client:
response = await client.get('/')
assert response.status_code == 200
You can use app with this client for debug code like as django.testclient
Test Database
The function will create a new database with a prefix next to the one specified in the settings.
- The original settings file will be overwritten so that in all tests queries will go to the new database.
POSTGRES_DB = "postgres" # will overwrite -> "test_postgres"
You can use one database settings file for all tests, without worrying that the original database will be overwritten
from toolbox.testing import temporary_database
from toolbox.sqlalchemy.connection import DatabaseConnectionSettings
from your_project.alchemy_models import BaseModel
@pytest.fixture(autouse=True)
def db_settings():
data = DatabaseConnectionSettings(
POSTGRES_USER="postgres",
POSTGRES_PASSWORD = "postgres",
POSTGRES_HOST = "0.0.0.0",
POSTGRES_PORT = "5432",
POSTGRES_DB = "postgres"
)
return data
@pytest.fixture(autouse=True)
async def temp_db(db_settings):
async with temporary_database(
settings=db_settings,
base_model=BaseModel,
# db_prefix = "test" # optional
):
yield
pass
Database Connect
from fastapi import Depends, FastAPI
from toolbox.sqlalchemy.connection import DatabaseConnectionManager, DatabaseConnectionSettings
settings = DatabaseConnectionSettings(
POSTGRES_USER="postgres",
POSTGRES_PASSWORD = "postgres",
POSTGRES_HOST = "0.0.0.0",
POSTGRES_PORT = "5432",
POSTGRES_DB = "postgres"
)
get_db_conn = DatabaseConnectionManager(settings=settings)
app = FastAPI()
@app.get("/")
async def index(database_conn = Depends(get_db_conn)):
...
Auth Middleware
FastAPI
from toolbox.auth.middlewares.fastapi_ import BearerTokenMiddleware, BearerTokenMiddlewareSettings
class TokenStorage:
async def __call__(self, token: str) -> bool:
...
token_validator = TokenStorage()
settings = BearerTokenMiddlewareSettings(
token_validator=token_validator,
exclude_paths=["/docs"]
)
BearerTokenMiddleware.set_settings(settings)
app = FastAPI()
app.add_middleware(BearerTokenMiddleware)
Tools
That's it! Enjoy! 🚀
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hexfrost_toolbox-0.1.5b0.tar.gz.
File metadata
- Download URL: hexfrost_toolbox-0.1.5b0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.11.0-1014-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
810509a7323440199b1b01fa26dde89ab239780e7037d69c85fb257b12146f2e
|
|
| MD5 |
a1a15875477a354f7cf7aebc0ca5c4d0
|
|
| BLAKE2b-256 |
30a4fac0eb2700accc0e610ab655f606593916d7d1ece85de2fbd1fba5bd97eb
|
File details
Details for the file hexfrost_toolbox-0.1.5b0-py3-none-any.whl.
File metadata
- Download URL: hexfrost_toolbox-0.1.5b0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.11.0-1014-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58b96999a27dd0b00bd7a39763cbcdc8b19178b01422909950bcdd82a4176ce1
|
|
| MD5 |
1d65442609db0d428cc402a82960dc52
|
|
| BLAKE2b-256 |
71c4b7535e3df2913ada35ad75e146d597436400522a6f3e900595fab741f8c0
|