Skip to main content

A simple way to create hooks (callbacks) to common packages and functions.

Project description

Common-hooks

This package is made to provide a simple way to create hooks (callback) to common packages and functions. Generally speaking, its relatively simple to create a hook, you import and attach to the process.

Next steps

[x] Create basic tests [ ] Upload to pypi [ ] Advanced test support [ ] Integrate requests library [ ] Integrate aiohttp library

Installation (not yet working)

There are multiple possible installations, depending on your need.

  1. Simple install only the core hooks that do no require any dependencies (which is currently none!):

    pip install common_hooks
    
  2. Installing all (Not recommended):

    pip install common_hooks[all]
    
  3. Installing only the hooks that require a specific package:

    pip install common_hooks[package_name]
    
  4. You can install multiple hooks using comma separated list, for example:

    pip install common_hooks[httpx,fastapi]
    

Available hooks include

  • httpx
  • fastapi

Usage

  1. You need to define a callback function that is structured like fastapi lifespans:
def my_callback(input):
    print(f"BEFORE: {inputs=}")
    result = yield
    print(f"AFTER: {result=}")

To attach the callback function to all httpx GET calls:

from common_hooks.httpx import hook
from common_hooks.conditions import HttpRequestCondition # optional condition

complex_condition = HttpRequestCondition(methods=["GET"])
hook.attach(my_callback, condition=complex_condition)

To attach a callback function to all httpx POST calls:

from common_hooks.httpx import hook
from common_hooks.conditions import HttpRequestCondition

complex_condition = HttpRequestCondition(methods=["POST"]) # optional
hook.attach(my_callback, condition=complex_condition)

After attaching, you must install the hook to apply the callback(s):

hook.install()

To use multiple hooks in the same script rename them using "as", common conditions can be reused:

from common_hooks.conditions import HttpRequestCondition
from common_hooks.httpx import hook as httpx_hook
from common_hooks.fastapi import hook as fastapi_hook

complex_condition = HttpRequestCondition(methods=["POST"])

hook.attach(my_callback, condition=complex_condition)
httpx_hook.install()

fastapi_hook.attach(my_callback, condition=complex_condition)
fastapi_hook.install()

This script will apply the callback to all POST requests made by httpx and all POST requests received by fastapi.

Planned future hooks (Still needs POC)

  • aiohttp
  • requests

Possible future hook ideas

  • flask
  • django
  • sqlalchemy
  • inbuilt functions
  • inbuilt classes

Contribution

If you have a hook you would like to add, please create a pull request with the hook and a test to ensure it works as expected. A hook must inherit from the CoreHook class you can import using:

from common_hooks import CoreHook

Check implementation of other hooks to see how to implement your own.

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

common_hooks-0.0.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

common_hooks-0.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file common_hooks-0.0.1.tar.gz.

File metadata

  • Download URL: common_hooks-0.0.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for common_hooks-0.0.1.tar.gz
Algorithm Hash digest
SHA256 eb8c8eeed57b8026776b2f8de85b0e99e9facc1ecc687ebff8125e21c7ca64fe
MD5 c850b4e1f1fcc4876db82fb1ac0420c5
BLAKE2b-256 66e575eb93ae47879d639711a9f13f0bff959e39960ba735d7e3d3d412de2a2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for common_hooks-0.0.1.tar.gz:

Publisher: publish.yml on Fadope1/common-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file common_hooks-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: common_hooks-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for common_hooks-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc55e0503ae94c6d7e1a27eab7815af7265868cfdf954ec19eeb297a05bec2e
MD5 90e542b84997a4ea54afbfebc2238b29
BLAKE2b-256 e6cc176ac2348fc134667ac408aac0c638b676a926ef31c08861c13bde737ede

See more details on using hashes here.

Provenance

The following attestation bundles were made for common_hooks-0.0.1-py3-none-any.whl:

Publisher: publish.yml on Fadope1/common-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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