litestar-utils
Utilities for the Litestar framework.
Source Code: https://github.com/Alurith/litestar-utils
Features:
This package include some utilities I generaly use in my projects:
- Timing Middleware: Basic timing for each request
- Slugify: Basic slugify function with customizable options
Installation:
$ pip install litestar-utils
or
$ poetry add litestar-utils
Usage:
Timing Middleware:
Use the create_timing_middleware function to create the middleware, the only required argument is emit.
emit must be a callable that accept 2 arguments a Request and a float
from litestar import Litestar, get
from litestar_utils import create_timing_middleware
@get("/")
async def hello_world() -> str:
return "Hello, world!"
@get("/base", exclude_timing=True)
async def base_all() -> str:
return "All your base are belong to us"
app = Litestar(
[hello_world, base_all],
middleware=[create_timing_middleware(emit=print)],
)
Slugify:
Use the SlugifyOptions class to customize the behaviour.
class SlugifyOptions(BaseModel):
collapse_whitespace: bool = True
disallowed_characters: str = r"[^\w\s-]"
separator: str = "-"
replacements: List = []
Simple example:
from litestar_utils import slugify, SlugifyOptions
expected = "this-is-easy"
input_string = "this is easy"
assert slugify(input_string) == expected
Don't collaps whitespaces (default to True):
from litestar_utils import slugify, SlugifyOptions
expected = "should--not---collapse----whitespaces"
input_string = " should not collapse whitespaces "
options = SlugifyOptions(collapse_whitespace=False)
assert slugify(input_string, options=options) == expected
Custom separator (default to -):
from litestar_utils import slugify, SlugifyOptions
expected = "this.is.easy"
input_string = "this is easy"
options = SlugifyOptions(separator=".")
assert slugify(input_string, options=options) == expected
Replacements:
from litestar_utils import slugify, SlugifyOptions
expected = "emailatexmapledotcom"
input_string = "email@exmaple.com"
options = SlugifyOptions(replacements=[("@", "at"), [".", "dot"]])
assert slugify(input_string, options=options) == expected
HTTPS Redirect Middleware:
Use the HTTPSRedirectMiddleware middleware to redirect all the incoming requests from http or ws to https or wss.
Ported from starlette HTTPSRedirectMiddleware
from litestar import Litestar, get
from litestar_utils import HTTPSRedirectMiddleware
@get("/")
async def hello_world() -> str:
return "Hello, world!"
@get("/base", exclude_timing=True)
async def base_all() -> str:
return "All your base are belong to us"
app = Litestar(
[hello_world, base_all],
middleware=[HTTPSRedirectMiddleware],
)
Metadata
Release files for litestar-utils 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| litestar_utils-0.3.0.tar.gz | 4.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| litestar_utils-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.0 kB
Release files / litestar_utils-0.3.0.tar.gz
| Download URL | litestar_utils-0.3.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
115ba98caa978853bb8db1e97b1f35abd767271d90cc9c928ea50ac02623993a
|
|
BLAKE2b-256 checksum How to use checksums |
f2cc535a1a3e563e39a0b29e587e3c0d4c0abd122623536e545ab4a52f380fae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Windows/10
|
Release files / litestar_utils-0.3.0-py3-none-any.whl
| Download URL | litestar_utils-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14ad5f46174ff1eb68cba2c480272eaccaee139b9f5cd4d0de2dd884628d4c0e
|
|
BLAKE2b-256 checksum How to use checksums |
91186a5cccc06fdcb1ae45a6e1be1da632b3be6ef38b45f02f9ac6f995ad5a76
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Windows/10
|
Security for litestar-utils 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.