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", # Required
        POSTGRES_PASSWORD = "postgres", # Required
        POSTGRES_HOST = "0.0.0.0", # Required
        POSTGRES_PORT = "5432", # Required
        POSTGRES_DB = "postgres" # Required
    )

    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", # Required
        POSTGRES_PASSWORD = "postgres", # Required
        POSTGRES_HOST = "0.0.0.0", # Required
        POSTGRES_PORT = "5432", # Required
        POSTGRES_DB = "postgres" # Required
    )

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, # Required
    exclude_paths=["/docs"] # Required
)

BearerTokenMiddleware.set_settings(settings)

app = FastAPI()
app.add_middleware(BearerTokenMiddleware)

Sensetive Fields in Pydantic Models

import os
from toolbox.schemes import SensitiveDataScheme
from toolbox.cipher import CipherSuiteManager, CipherSuiteSettings


settings = CipherSuiteSettings(
    MASTER_KEY=os.getenv("MASTER_KEY") # Required
)


cipher_suite = CipherSuiteManager.set_settings(settings)


SensitiveDataScheme.set_cipher_suite_manager(cipher_suite)


class OpenAPIToken(SensitiveDataScheme):
    _sensitive_attributes = ["token"]

    default_model: str
    token: str | bytes


new_token = OpenAPIToken(
        default_model = "o3-mini",
        token = "sk-12fefx33k34h1v4h1cl14c"
    ).encrypt_fields()

unsafe_data = new_token.decrypt_fields()

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.7b0.tar.gz (12.8 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.7b0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file hexfrost_toolbox-0.1.7b0.tar.gz.

File metadata

  • Download URL: hexfrost_toolbox-0.1.7b0.tar.gz
  • Upload date:
  • Size: 12.8 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.7b0.tar.gz
Algorithm Hash digest
SHA256 63e364c99e3bbe3ba21dcb97f38f3d499f069da9a25cfa50086e818c450da686
MD5 21ad4a2a3db62405e33a546a5e624658
BLAKE2b-256 c584fe9db8db7d3e34876d0d06f139a6d1d05cd2389bb0ae7d4a5378500863b6

See more details on using hashes here.

File details

Details for the file hexfrost_toolbox-0.1.7b0-py3-none-any.whl.

File metadata

  • Download URL: hexfrost_toolbox-0.1.7b0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.7b0-py3-none-any.whl
Algorithm Hash digest
SHA256 44b5aa2e3058ec1f1fcb2d7a121a34829e23692b2fa9d860185c4d6ebbca5bf8
MD5 8716b11707f37294acad42d3b7bdf69a
BLAKE2b-256 b3897b5d24be57853571aed97be98e19b9bc2fd47aef03be038530bc59ab1412

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