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.
-
Simple install only the core hooks that do no require any dependencies (which is currently none!):
pip install common_hooks
-
Installing all (Not recommended):
pip install common_hooks[all]
-
Installing only the hooks that require a specific package:
pip install common_hooks[package_name]
-
You can install multiple hooks using comma separated list, for example:
pip install common_hooks[httpx,fastapi]
Available hooks include
- httpx
- fastapi
Usage
- 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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb8c8eeed57b8026776b2f8de85b0e99e9facc1ecc687ebff8125e21c7ca64fe
|
|
| MD5 |
c850b4e1f1fcc4876db82fb1ac0420c5
|
|
| BLAKE2b-256 |
66e575eb93ae47879d639711a9f13f0bff959e39960ba735d7e3d3d412de2a2b
|
Provenance
The following attestation bundles were made for common_hooks-0.0.1.tar.gz:
Publisher:
publish.yml on Fadope1/common-hooks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
common_hooks-0.0.1.tar.gz -
Subject digest:
eb8c8eeed57b8026776b2f8de85b0e99e9facc1ecc687ebff8125e21c7ca64fe - Sigstore transparency entry: 155457631
- Sigstore integration time:
-
Permalink:
Fadope1/common-hooks@af4fae89825e6bb4f3cc72be059fbc2576ade20a -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/Fadope1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@af4fae89825e6bb4f3cc72be059fbc2576ade20a -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8dc55e0503ae94c6d7e1a27eab7815af7265868cfdf954ec19eeb297a05bec2e
|
|
| MD5 |
90e542b84997a4ea54afbfebc2238b29
|
|
| BLAKE2b-256 |
e6cc176ac2348fc134667ac408aac0c638b676a926ef31c08861c13bde737ede
|
Provenance
The following attestation bundles were made for common_hooks-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on Fadope1/common-hooks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
common_hooks-0.0.1-py3-none-any.whl -
Subject digest:
8dc55e0503ae94c6d7e1a27eab7815af7265868cfdf954ec19eeb297a05bec2e - Sigstore transparency entry: 155457632
- Sigstore integration time:
-
Permalink:
Fadope1/common-hooks@af4fae89825e6bb4f3cc72be059fbc2576ade20a -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/Fadope1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@af4fae89825e6bb4f3cc72be059fbc2576ade20a -
Trigger Event:
release
-
Statement type: