Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexfrost_toolbox-0.1.5.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexfrost_toolbox-0.1.5-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file hexfrost_toolbox-0.1.5.tar.gz.

File metadata

  • Download URL: hexfrost_toolbox-0.1.5.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

Hashes for hexfrost_toolbox-0.1.5.tar.gz
Algorithm Hash digest
SHA256 7ccc3f4b44c595275ca9f24a22d3b6e5e57341b8f84669f57da66da3311ed212
MD5 eeb1af14c8c04a57fb3005cb9ff4ac63
BLAKE2b-256 9ca63f5be5badf70f38e6f1629c1676f65e2cdd1d3efd38402b2e2d4e4cbd5f1

See more details on using hashes here.

File details

Details for the file hexfrost_toolbox-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: hexfrost_toolbox-0.1.5-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

Hashes for hexfrost_toolbox-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d7dcca722bdd7f2423318f7230218ab656a68f87a24aabbdfa87685d9f14ea5a
MD5 0262b0be2e9deb4832f034339fe20c30
BLAKE2b-256 9208c0ee24164969c8158d1be07f982199b99f44e480826e1f9731f118ff0e91

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page