Name | common-hooks JSON |
Version |
0.0.1
JSON |
| download |
home_page | None |
Summary | A simple way to create hooks (callbacks) to common packages and functions. |
upload_time | 2024-12-15 17:25:16 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.11 |
license | None |
keywords |
common-hooks
callbacks
python
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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!):
```bash
pip install common_hooks
```
1. Installing all (Not recommended):
```bash
pip install common_hooks[all]
```
1. Installing only the hooks that require a specific package:
```bash
pip install common_hooks[package_name]
```
1. You can install multiple hooks using comma separated list, for example:
```bash
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:
```python
def my_callback(input):
print(f"BEFORE: {inputs=}")
result = yield
print(f"AFTER: {result=}")
```
To attach the callback function to all httpx GET calls:
```python
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:
```python
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):
```python
hook.install()
```
To use multiple hooks in the same script rename them using "as", common conditions can be reused:
```python
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:
```python
from common_hooks import CoreHook
```
Check implementation of other hooks to see how to implement your own.
Raw data
{
"_id": null,
"home_page": null,
"name": "common-hooks",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "common-hooks, callbacks, python",
"author": null,
"author_email": "Fabian Peschke <fabian-peschke@web.de>",
"download_url": "https://files.pythonhosted.org/packages/66/e5/75eb93ae47879d639711a9f13f0bff959e39960ba735d7e3d3d412de2a2b/common_hooks-0.0.1.tar.gz",
"platform": null,
"description": "# Common-hooks\n\nThis package is made to provide a simple way to create hooks (callback) to common packages and functions.\nGenerally speaking, its relatively simple to create a hook, you import and attach to the process.\n\n## Next steps\n[x] Create basic tests\n[ ] Upload to pypi\n[ ] Advanced test support\n[ ] Integrate requests library\n[ ] Integrate aiohttp library\n\n## Installation (not yet working)\n\nThere are multiple possible installations, depending on your need.\n\n1. Simple install only the core hooks that do no require any dependencies (which is currently none!):\n\n ```bash\n pip install common_hooks\n ```\n\n1. Installing all (Not recommended):\n\n ```bash\n pip install common_hooks[all]\n ```\n\n1. Installing only the hooks that require a specific package:\n\n ```bash\n pip install common_hooks[package_name]\n ```\n\n1. You can install multiple hooks using comma separated list, for example:\n\n ```bash\n pip install common_hooks[httpx,fastapi]\n ```\n\n## Available hooks include\n\n- httpx\n- fastapi\n\n## Usage\n\n1. You need to define a callback function that is structured like fastapi lifespans:\n\n```python\ndef my_callback(input):\n print(f\"BEFORE: {inputs=}\")\n result = yield\n print(f\"AFTER: {result=}\")\n```\n\nTo attach the callback function to all httpx GET calls:\n\n```python\nfrom common_hooks.httpx import hook\nfrom common_hooks.conditions import HttpRequestCondition # optional condition\n\ncomplex_condition = HttpRequestCondition(methods=[\"GET\"])\nhook.attach(my_callback, condition=complex_condition)\n```\n\nTo attach a callback function to all httpx POST calls:\n\n```python\nfrom common_hooks.httpx import hook\nfrom common_hooks.conditions import HttpRequestCondition\n\ncomplex_condition = HttpRequestCondition(methods=[\"POST\"]) # optional\nhook.attach(my_callback, condition=complex_condition)\n```\n\nAfter attaching, you must install the hook to apply the callback(s):\n\n```python\nhook.install()\n```\n\nTo use multiple hooks in the same script rename them using \"as\", common conditions can be reused:\n\n```python\nfrom common_hooks.conditions import HttpRequestCondition\nfrom common_hooks.httpx import hook as httpx_hook\nfrom common_hooks.fastapi import hook as fastapi_hook\n\ncomplex_condition = HttpRequestCondition(methods=[\"POST\"])\n\nhook.attach(my_callback, condition=complex_condition)\nhttpx_hook.install()\n\nfastapi_hook.attach(my_callback, condition=complex_condition)\nfastapi_hook.install()\n```\nThis script will apply the callback to all POST requests made by httpx and all POST requests received by fastapi.\n\n## Planned future hooks (Still needs POC)\n\n- aiohttp\n- requests\n\n## Possible future hook ideas\n\n- flask\n- django\n- sqlalchemy\n- inbuilt functions\n- inbuilt classes\n\n## Contribution\n\nIf 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.\nA hook must inherit from the CoreHook class you can import using:\n\n```python\nfrom common_hooks import CoreHook\n```\n\nCheck implementation of other hooks to see how to implement your own.\n",
"bugtrack_url": null,
"license": null,
"summary": "A simple way to create hooks (callbacks) to common packages and functions.",
"version": "0.0.1",
"project_urls": {
"Homepage": "https://github.com/Fadope1/common-hooks"
},
"split_keywords": [
"common-hooks",
" callbacks",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e6cc176ac2348fc134667ac408aac0c638b676a926ef31c08861c13bde737ede",
"md5": "90e542b84997a4ea54afbfebc2238b29",
"sha256": "8dc55e0503ae94c6d7e1a27eab7815af7265868cfdf954ec19eeb297a05bec2e"
},
"downloads": -1,
"filename": "common_hooks-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "90e542b84997a4ea54afbfebc2238b29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 7387,
"upload_time": "2024-12-15T17:25:14",
"upload_time_iso_8601": "2024-12-15T17:25:14.251084Z",
"url": "https://files.pythonhosted.org/packages/e6/cc/176ac2348fc134667ac408aac0c638b676a926ef31c08861c13bde737ede/common_hooks-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "66e575eb93ae47879d639711a9f13f0bff959e39960ba735d7e3d3d412de2a2b",
"md5": "c850b4e1f1fcc4876db82fb1ac0420c5",
"sha256": "eb8c8eeed57b8026776b2f8de85b0e99e9facc1ecc687ebff8125e21c7ca64fe"
},
"downloads": -1,
"filename": "common_hooks-0.0.1.tar.gz",
"has_sig": false,
"md5_digest": "c850b4e1f1fcc4876db82fb1ac0420c5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 7216,
"upload_time": "2024-12-15T17:25:16",
"upload_time_iso_8601": "2024-12-15T17:25:16.521760Z",
"url": "https://files.pythonhosted.org/packages/66/e5/75eb93ae47879d639711a9f13f0bff959e39960ba735d7e3d3d412de2a2b/common_hooks-0.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-15 17:25:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Fadope1",
"github_project": "common-hooks",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "common-hooks"
}