Small helper functions for web projects
Project description
sag_py_web_common
This contains samhammer specific and internally used helper functions for web projects.
Requirements for code to be added here:
- It's sag/project specific and not of general/public use, so that it does not make sense to create a individual lib
- It's nothing private either, that should not be publically on the internet
- It has no, or very little dependencies (because the deps are in all projects using the lib, even if the feaute isn't required)
Note: See this as last option and try to create individual libs as much as possible.
Installation
pip install sag-py-web-common
How to use
Default routing
All requests to the main route / are redirected to /swagger if nothing specified.
from sag_py_web_common.default_route import build_default_route
app: FastAPI = FastAPI(...)
app.include_router(build_default_route(ingress_base_path=config.ingress_base_path))
- ingress_base_path: Empty or a path starting with / if proxy hosting like kubernetes is used
- default_redirect_path: Per default /swagger but can be configured to an alternative route
Filtered access logging
Extends the asgi-logger and adds a log entry for received requests. Furthermore the requests are filtered, so that health checks don't spam the logs.
For requests to be filtered, they need to have the header "healthcheck" with one of these values: "livenessprobe", "readinessprobe", "startupprobe", "prtg"
from sag_py_web_common.filtered_access_logger import FilteredAccessLoggerMiddleware
app: FastAPI = FastAPI(...)
app.add_middleware(
FilteredAccessLoggerMiddleware,
format="Completed: %(R)s - %(st)s - %(L)s",
logger=logging.getLogger("access"),
)
Also see this page for further configuration details: https://github.com/Kludex/asgi-logger
Json exception handler
Per default fastapi falls back to text responses if there are unknown exceptions. That's not the desired behaviour for json api's.
This handler ensures that a json is returned. It contains the field "detail" with the exception message.
from sag_py_web_common.json_exception_handler import handle_unknown_exception
app.add_exception_handler(Exception, handle_unknown_exception)
For logging any HHTP-Exception use the log_exception function.
from starlette.exceptions import HTTPException as StarletteHTTPException
from sag_py_web_common.json_exception_handler import log_exception
app.add_exception_handler(StarletteHTTPException, log_exception)
Json Exception handler uses logger "http_error_logger", which could be used for reporting concepts.
How to start developing
With vscode
Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.
With pycharm
- Install latest pycharm
- Install pycharm plugin BlackConnect
- Install pycharm plugin Mypy
- Configure the python interpreter/venv
- pip install requirements-dev.txt
- pip install black[d]
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
- Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
- Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
- Ctl+Alt+S => Click Tools => Actions on save => Reformat code
- Restart pycharm
How to publish
- Update the version in setup.py and commit your change
- Create a tag with the same version number
- Let github do the rest
How to test
To avoid publishing to pypi unnecessarily you can do as follows
- Tag your branch however you like
- Use the chosen tag in the requirements.txt-file of the project you want to test this library in, eg.
sag_py_web_common==<your tag>
- Rebuild/redeploy your project
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
Built Distribution
File details
Details for the file sag_py_web_common-1.0.0.tar.gz
.
File metadata
- Download URL: sag_py_web_common-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9a348e2426a0c7e614c2e4cd8c575afd6a6991ee2539b6957f4393747a88c57 |
|
MD5 | c14ed03380a511aa74c3fcb944925794 |
|
BLAKE2b-256 | 81996f73a87c64a885e48d622d6f57c7f89ce6b750f379357b9930acd7d9f41f |
File details
Details for the file sag_py_web_common-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: sag_py_web_common-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3163aecba2d6bdb1ccbdcfad0c87f75e4c17076a4f5b928de7665d4a6c1af264 |
|
MD5 | 7ec283acf23bf889a27911d53780dae8 |
|
BLAKE2b-256 | 6660465b516b4f21e6945eeb312853a8f751111757195c21e028fc083791d2b8 |