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

Download files

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

Source Distribution

sag_py_web_common-1.1.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

sag_py_web_common-1.1.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file sag_py_web_common-1.1.2.tar.gz.

File metadata

  • Download URL: sag_py_web_common-1.1.2.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for sag_py_web_common-1.1.2.tar.gz
Algorithm Hash digest
SHA256 d9e1f635d81ed3f7a2f3dc83ec8f02e5bb806a9f72a757c244d02c529ad73f1b
MD5 f18add4855a16e87d445a9230c47e602
BLAKE2b-256 f2eebdd9d8964998674dee797e77f0cde11adb577022f1551436fb4faaf9a572

See more details on using hashes here.

File details

Details for the file sag_py_web_common-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sag_py_web_common-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3fdf998aacd800013c861de8c84c3465c2ac9ba88124999d0fafc78d63f19adb
MD5 f3e90674f46165b3a8c8e0addb99bad9
BLAKE2b-256 37f7b8bc04072f5f80983f7832aae7360f771920ebe4c56fc24b764deb38e83d

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.4

2 files

This release

1.1.2 This release

2 files

1.1.0

2 files

1.0.9

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

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