Skip to main content

Small helper functions for web projects

Project description

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 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)

Fo logging any HHTP-Exception use log_exception funktion.

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", what could be use 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

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

sag-py-web-common-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

sag_py_web_common-0.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file sag-py-web-common-0.1.1.tar.gz.

File metadata

  • Download URL: sag-py-web-common-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for sag-py-web-common-0.1.1.tar.gz
Algorithm Hash digest
SHA256 79cdcd599b20875fd2a881acdce4137db3bb74857061cad4a63016a2a5ee97dd
MD5 84b9e3743727662ed3ee59742a7bdfd4
BLAKE2b-256 9655d842877f0c1bbb0cecc4fad8c5243961e47141e0782abeff9c2c5f9a1d82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sag_py_web_common-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5156e10d204bf9bcfae02f7855870e9b4d19d03fe894a1a11a4d68ba21277a28
MD5 3a9161e43899188524a080854b0eb069
BLAKE2b-256 89679490da706783d6b26b93b0163501bda26ce091ea0171e64b37b6ccf1f4d2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page