Skip to main content

sag_py_web_common

Maintainability Coverage Status Known Vulnerabilities

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 can be filtered, so that health checks (or similar) don't spam the logs.

Requests can be filtered via one of two ways:

  • via the optional parameter "excluded_paths": Simply append all paths that should be ignored, separated by comma.
  • via the optional parameter "exclude_header": This one will require you to define a name for the header (f.ex. 'myHeaderExclude'), and then also send this defined header as an HTTP Header with your requests.

This filter will apply to substrings, as well since the filter is using a contains-search.

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"),
    excluded_paths=["pathPart/partOne", "pathPart/partTwo"],  # optional
    exclude_header="myHeaderExclude",  # optional
)

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 Mypy
  • Configure the python interpreter/venv
  • pip install requirements-dev.txt
  • 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

Metadata

Release files for sag-py-web-common 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sag-py-web-common 1.1.4
File Size Uploaded Provenance Actions
sag_py_web_common-1.1.4.tar.gz 11.9 kB Without provenance Details for sag_py_web_common-1.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sag-py-web-common 1.1.4
File Interpreter ABI Platform Actions
sag_py_web_common-1.1.4-py3-none-any.whl Python 3 none any Details for sag_py_web_common-1.1.4-py3-none-any.whl

Total release size: 20.4 kB

Release files / sag_py_web_common-1.1.4.tar.gz

Download URL sag_py_web_common-1.1.4.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
76d17a16993b359b664fab01089f4c8ef15e2b630a71c5b2b63f5934d950101c
BLAKE2b-256 checksum
How to use checksums
123c16aab734fd56aa26fabf4d6dd6428fa0e0be09ee2c9f878b7819d24ea384
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / sag_py_web_common-1.1.4-py3-none-any.whl

Download URL sag_py_web_common-1.1.4-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a4f68277107d7bd4eb32964a9d54de384a7eac6576d02b3bc77c13dcb40375f
BLAKE2b-256 checksum
How to use checksums
40f64c990e366a448199a9f400f5e1ffa149832ceb0c61b9c8eec01dd50c25a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for sag-py-web-common 1.1.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page