zndraw-socketio 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zndraw_socketio-0.1.0/.claude/settings.local.json +11 -0
- zndraw_socketio-0.1.0/.gitattributes +4 -0
- zndraw_socketio-0.1.0/.github/workflows/publish.yml +81 -0
- zndraw_socketio-0.1.0/.github/workflows/test.yml +72 -0
- zndraw_socketio-0.1.0/.gitignore +168 -0
- zndraw_socketio-0.1.0/LICENSE +21 -0
- zndraw_socketio-0.1.0/PKG-INFO +212 -0
- zndraw_socketio-0.1.0/README.md +190 -0
- zndraw_socketio-0.1.0/pyproject.toml +54 -0
- zndraw_socketio-0.1.0/src/zndraw_socketio/__init__.py +13 -0
- zndraw_socketio-0.1.0/src/zndraw_socketio/params.py +48 -0
- zndraw_socketio-0.1.0/src/zndraw_socketio/py.typed +0 -0
- zndraw_socketio-0.1.0/src/zndraw_socketio/wrapper.py +1639 -0
- zndraw_socketio-0.1.0/tests/conftest.py +178 -0
- zndraw_socketio-0.1.0/tests/test_depends_integration.py +152 -0
- zndraw_socketio-0.1.0/tests/test_depends_request.py +328 -0
- zndraw_socketio-0.1.0/tests/test_fastapi_exception_handlers.py +150 -0
- zndraw_socketio-0.1.0/tests/test_fastapi_exception_integration.py +107 -0
- zndraw_socketio-0.1.0/tests/test_typing.py +172 -0
- zndraw_socketio-0.1.0/tests/test_wrapper.py +503 -0
- zndraw_socketio-0.1.0/uv.lock +1647 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
name: Publish Python 🐍 distributions 📦
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
publish_testpypi:
|
|
7
|
+
type: boolean
|
|
8
|
+
default: false
|
|
9
|
+
description: Publish to TestPyPI
|
|
10
|
+
publish_pypi:
|
|
11
|
+
type: boolean
|
|
12
|
+
default: false
|
|
13
|
+
description: Publish to PyPI
|
|
14
|
+
publish_gh_release:
|
|
15
|
+
type: boolean
|
|
16
|
+
default: true
|
|
17
|
+
description: Publish to GitHub Release
|
|
18
|
+
use_changelog:
|
|
19
|
+
type: boolean
|
|
20
|
+
default: true
|
|
21
|
+
description: Extract release notes from CHANGELOG.md
|
|
22
|
+
changelog_file:
|
|
23
|
+
type: string
|
|
24
|
+
default: CHANGELOG.md
|
|
25
|
+
description: Path to changelog file
|
|
26
|
+
required: false
|
|
27
|
+
release_tag:
|
|
28
|
+
type: string
|
|
29
|
+
description: Tag to package (empty for latest tag)
|
|
30
|
+
required: false
|
|
31
|
+
|
|
32
|
+
jobs:
|
|
33
|
+
get-tag:
|
|
34
|
+
runs-on: ubuntu-latest
|
|
35
|
+
outputs:
|
|
36
|
+
release_tag: ${{ steps.set_release_tag.outputs.tag }}
|
|
37
|
+
steps:
|
|
38
|
+
- name: Checkout code
|
|
39
|
+
uses: actions/checkout@v4
|
|
40
|
+
- name: Fetch all tags
|
|
41
|
+
run: |
|
|
42
|
+
git fetch --prune --unshallow --tags
|
|
43
|
+
- name: Verify and set release tag
|
|
44
|
+
id: set_release_tag
|
|
45
|
+
run: |
|
|
46
|
+
release_tag=${{ inputs.release_tag }}
|
|
47
|
+
if [ -z "$release_tag" ]; then
|
|
48
|
+
echo "Input tag is empty. Fetching latest tag."
|
|
49
|
+
release_tag=$(git describe --tags $(git rev-list --tags --max-count=1))
|
|
50
|
+
if [ -z "$release_tag" ]; then
|
|
51
|
+
echo "No latest tag available. Exiting workflow."
|
|
52
|
+
exit 1
|
|
53
|
+
fi
|
|
54
|
+
else
|
|
55
|
+
if ! git rev-parse -q --verify "refs/tags/$release_tag" >/dev/null; then
|
|
56
|
+
echo "Invalid tag '$release_tag'. Exiting workflow."
|
|
57
|
+
exit 1
|
|
58
|
+
fi
|
|
59
|
+
fi
|
|
60
|
+
echo "tag=$release_tag" >> $GITHUB_OUTPUT
|
|
61
|
+
test:
|
|
62
|
+
needs: get-tag
|
|
63
|
+
uses: ./.github/workflows/test.yml
|
|
64
|
+
with:
|
|
65
|
+
release_tag: ${{ needs.get-tag.outputs.release_tag }}
|
|
66
|
+
build-n-publish:
|
|
67
|
+
needs: test
|
|
68
|
+
permissions:
|
|
69
|
+
id-token: write # IMPORTANT: mandatory for trusted publishing and sigstore
|
|
70
|
+
contents: write # IMPORTANT: mandatory for making GitHub Releases
|
|
71
|
+
uses: atomiechen/reusable-workflows/.github/workflows/publish-python-distributions.yml@main
|
|
72
|
+
with:
|
|
73
|
+
publish_testpypi: ${{ inputs.publish_testpypi }}
|
|
74
|
+
publish_pypi: ${{ inputs.publish_pypi }}
|
|
75
|
+
publish_gh_release: ${{ inputs.publish_gh_release }}
|
|
76
|
+
use_changelog: ${{ inputs.use_changelog }}
|
|
77
|
+
changelog_file: ${{ inputs.changelog_file }}
|
|
78
|
+
release_tag: ${{ needs.get-tag.outputs.release_tag }}
|
|
79
|
+
secrets:
|
|
80
|
+
TEST_PYPI_API_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
|
81
|
+
PYPI_API_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
name: Test Python Package 🔧
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ "main" ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ "main" ]
|
|
8
|
+
workflow_call:
|
|
9
|
+
inputs:
|
|
10
|
+
release_tag:
|
|
11
|
+
type: string
|
|
12
|
+
required: false
|
|
13
|
+
workflow_dispatch:
|
|
14
|
+
inputs:
|
|
15
|
+
release_tag:
|
|
16
|
+
type: string
|
|
17
|
+
description: Tag to package (empty for latest commit)
|
|
18
|
+
required: false
|
|
19
|
+
|
|
20
|
+
jobs:
|
|
21
|
+
test:
|
|
22
|
+
strategy:
|
|
23
|
+
matrix:
|
|
24
|
+
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
25
|
+
python-version: [
|
|
26
|
+
"3.10", "3.11", "3.12", "3.13",
|
|
27
|
+
# "3.14"
|
|
28
|
+
]
|
|
29
|
+
runs-on: ${{ matrix.os }}
|
|
30
|
+
steps:
|
|
31
|
+
- name: Checkout
|
|
32
|
+
uses: actions/checkout@v4
|
|
33
|
+
- name: Checkout to release tag if provided
|
|
34
|
+
if: ${{ inputs.release_tag }}
|
|
35
|
+
run: |
|
|
36
|
+
echo "Checking out to release tag ${{ inputs.release_tag }}"
|
|
37
|
+
git fetch --prune --unshallow --tags
|
|
38
|
+
git checkout ${{ inputs.release_tag }}
|
|
39
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
40
|
+
uses: actions/setup-python@v3
|
|
41
|
+
with:
|
|
42
|
+
python-version: ${{ matrix.python-version }}
|
|
43
|
+
- name: Install uv
|
|
44
|
+
uses: astral-sh/setup-uv@v6
|
|
45
|
+
with:
|
|
46
|
+
enable-cache: true
|
|
47
|
+
- name: Install dependencies (with optional extras)
|
|
48
|
+
# ref: https://docs.astral.sh/uv/guides/integration/github/#syncing-and-running
|
|
49
|
+
run: |
|
|
50
|
+
uv sync --locked --all-extras
|
|
51
|
+
# python -m pip install --upgrade pip
|
|
52
|
+
# pip install -r requirements.txt
|
|
53
|
+
- name: Activate venv and store path (Windows)
|
|
54
|
+
# ref: https://stackoverflow.com/a/74669486/11854304
|
|
55
|
+
# ref: https://stackoverflow.com/a/72926104/11854304
|
|
56
|
+
if: runner.os == 'Windows'
|
|
57
|
+
run: |
|
|
58
|
+
.venv\Scripts\activate
|
|
59
|
+
echo PATH=$PATH >> $GITHUB_ENV
|
|
60
|
+
- name: Activate venv and store path (Linux/Mac)
|
|
61
|
+
# ref: https://stackoverflow.com/a/74669486/11854304
|
|
62
|
+
# ref: https://stackoverflow.com/a/72926104/11854304
|
|
63
|
+
if: runner.os != 'Windows'
|
|
64
|
+
run: |
|
|
65
|
+
source .venv/bin/activate
|
|
66
|
+
echo PATH=$PATH >> $GITHUB_ENV
|
|
67
|
+
- name: Lint
|
|
68
|
+
run: |
|
|
69
|
+
./scripts/lint.sh
|
|
70
|
+
- name: Test
|
|
71
|
+
run: |
|
|
72
|
+
./scripts/test.sh
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# ruff
|
|
2
|
+
.ruff_cache
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# == Below are commonly ignored files for Python projects == #
|
|
6
|
+
|
|
7
|
+
# Byte-compiled / optimized / DLL files
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
*$py.class
|
|
11
|
+
|
|
12
|
+
# C extensions
|
|
13
|
+
*.so
|
|
14
|
+
|
|
15
|
+
# Distribution / packaging
|
|
16
|
+
.Python
|
|
17
|
+
build/
|
|
18
|
+
develop-eggs/
|
|
19
|
+
dist/
|
|
20
|
+
downloads/
|
|
21
|
+
eggs/
|
|
22
|
+
.eggs/
|
|
23
|
+
lib/
|
|
24
|
+
lib64/
|
|
25
|
+
parts/
|
|
26
|
+
sdist/
|
|
27
|
+
var/
|
|
28
|
+
wheels/
|
|
29
|
+
share/python-wheels/
|
|
30
|
+
*.egg-info/
|
|
31
|
+
.installed.cfg
|
|
32
|
+
*.egg
|
|
33
|
+
MANIFEST
|
|
34
|
+
|
|
35
|
+
# PyInstaller
|
|
36
|
+
# Usually these files are written by a python script from a template
|
|
37
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
38
|
+
*.manifest
|
|
39
|
+
*.spec
|
|
40
|
+
|
|
41
|
+
# Installer logs
|
|
42
|
+
pip-log.txt
|
|
43
|
+
pip-delete-this-directory.txt
|
|
44
|
+
|
|
45
|
+
# Unit test / coverage reports
|
|
46
|
+
htmlcov/
|
|
47
|
+
.tox/
|
|
48
|
+
.nox/
|
|
49
|
+
.coverage
|
|
50
|
+
.coverage.*
|
|
51
|
+
.cache
|
|
52
|
+
nosetests.xml
|
|
53
|
+
coverage.xml
|
|
54
|
+
*.cover
|
|
55
|
+
*.py,cover
|
|
56
|
+
.hypothesis/
|
|
57
|
+
.pytest_cache/
|
|
58
|
+
cover/
|
|
59
|
+
|
|
60
|
+
# Translations
|
|
61
|
+
*.mo
|
|
62
|
+
*.pot
|
|
63
|
+
|
|
64
|
+
# Django stuff:
|
|
65
|
+
*.log
|
|
66
|
+
local_settings.py
|
|
67
|
+
db.sqlite3
|
|
68
|
+
db.sqlite3-journal
|
|
69
|
+
|
|
70
|
+
# Flask stuff:
|
|
71
|
+
instance/
|
|
72
|
+
.webassets-cache
|
|
73
|
+
|
|
74
|
+
# Scrapy stuff:
|
|
75
|
+
.scrapy
|
|
76
|
+
|
|
77
|
+
# Sphinx documentation
|
|
78
|
+
docs/_build/
|
|
79
|
+
|
|
80
|
+
# PyBuilder
|
|
81
|
+
.pybuilder/
|
|
82
|
+
target/
|
|
83
|
+
|
|
84
|
+
# Jupyter Notebook
|
|
85
|
+
.ipynb_checkpoints
|
|
86
|
+
|
|
87
|
+
# IPython
|
|
88
|
+
profile_default/
|
|
89
|
+
ipython_config.py
|
|
90
|
+
|
|
91
|
+
# pyenv
|
|
92
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
93
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
94
|
+
# .python-version
|
|
95
|
+
|
|
96
|
+
# pipenv
|
|
97
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
98
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
99
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
100
|
+
# install all needed dependencies.
|
|
101
|
+
#Pipfile.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Atomie CHEN
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zndraw-socketio
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Typed wrapper for python-socketio with Pydantic validation and dependency injection.
|
|
5
|
+
Project-URL: homepage, https://github.com/pythonFZ/zndraw-socketio
|
|
6
|
+
Project-URL: issues, https://github.com/pythonFZ/zndraw-socketio/issues
|
|
7
|
+
Author-email: Atomie CHEN <atomic_cwh@163.com>, Fabian Zills <fabian.zills@web.de>
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: pydantic,socketio,typed,wrapper,zndraw
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Requires-Dist: pydantic>=2.10.6
|
|
15
|
+
Requires-Dist: python-socketio>=5.12.1
|
|
16
|
+
Requires-Dist: typing-extensions>=4.13.0
|
|
17
|
+
Provides-Extra: asyncio-client
|
|
18
|
+
Requires-Dist: python-socketio[asyncio-client]>=5.12.1; extra == 'asyncio-client'
|
|
19
|
+
Provides-Extra: client
|
|
20
|
+
Requires-Dist: python-socketio[client]>=5.12.1; extra == 'client'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# zndraw-socketio
|
|
24
|
+
|
|
25
|
+
Typed wrapper for [python-socketio](https://github.com/miguelgrinberg/python-socketio) with Pydantic validation and dependency injection.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
pip install zndraw-socketio
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Optional extras for client transports:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
pip install zndraw-socketio[client]
|
|
38
|
+
pip install zndraw-socketio[asyncio-client]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### Wrap any socketio instance
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import socketio
|
|
48
|
+
from pydantic import BaseModel
|
|
49
|
+
from zndraw_socketio import wrap
|
|
50
|
+
|
|
51
|
+
class Ping(BaseModel):
|
|
52
|
+
message: str
|
|
53
|
+
|
|
54
|
+
class Pong(BaseModel):
|
|
55
|
+
reply: str
|
|
56
|
+
|
|
57
|
+
# Wrap any existing socketio instance
|
|
58
|
+
tsio = wrap(socketio.AsyncClient())
|
|
59
|
+
|
|
60
|
+
# Emit with automatic event name derivation (Ping -> "ping")
|
|
61
|
+
await tsio.emit(Ping(message="Hello, World!"))
|
|
62
|
+
|
|
63
|
+
# Emit with explicit event name
|
|
64
|
+
await tsio.emit("my-ping", Ping(message="Hello, World!"))
|
|
65
|
+
|
|
66
|
+
# Call with typed response
|
|
67
|
+
response = await tsio.call(Ping(message="Hello"), response_model=Pong)
|
|
68
|
+
# response is typed as Pong
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Handler registration with validation
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
# Event name derived from model class (Ping -> "ping")
|
|
75
|
+
@tsio.on(Ping)
|
|
76
|
+
async def handle_ping(data: Ping) -> Pong:
|
|
77
|
+
return Pong(reply=data.message)
|
|
78
|
+
|
|
79
|
+
# Or use function name as event name
|
|
80
|
+
@tsio.event
|
|
81
|
+
async def ping(data: Ping) -> Pong:
|
|
82
|
+
return Pong(reply=data.message)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Union response types
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from typing import Annotated, Literal
|
|
89
|
+
from pydantic import BaseModel, Discriminator
|
|
90
|
+
|
|
91
|
+
class Success(BaseModel):
|
|
92
|
+
kind: Literal["success"] = "success"
|
|
93
|
+
data: str
|
|
94
|
+
|
|
95
|
+
class Error(BaseModel):
|
|
96
|
+
kind: Literal["error"] = "error"
|
|
97
|
+
message: str
|
|
98
|
+
|
|
99
|
+
# Simple union
|
|
100
|
+
response = await tsio.call(request, response_model=Success | Error)
|
|
101
|
+
|
|
102
|
+
# Discriminated union
|
|
103
|
+
ResponseType = Annotated[Success | Error, Discriminator("kind")]
|
|
104
|
+
response = await tsio.call(request, response_model=ResponseType)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Custom event names
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from typing import ClassVar
|
|
111
|
+
|
|
112
|
+
class CustomEvent(BaseModel):
|
|
113
|
+
event_name: ClassVar[str] = "my_custom_event"
|
|
114
|
+
data: str
|
|
115
|
+
|
|
116
|
+
# Uses "my_custom_event" instead of "custom_event"
|
|
117
|
+
await tsio.emit(CustomEvent(data="hello"))
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Exception handlers
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from zndraw_socketio import wrap, EventContext
|
|
124
|
+
|
|
125
|
+
tsio = wrap(socketio.AsyncServer(async_mode="asgi"))
|
|
126
|
+
|
|
127
|
+
@tsio.exception_handler(ValueError)
|
|
128
|
+
async def handle_value_error(ctx: EventContext, exc: ValueError):
|
|
129
|
+
return {"error": "value_error", "message": str(exc)}
|
|
130
|
+
|
|
131
|
+
# Namespace-specific
|
|
132
|
+
@tsio.exception_handler(ValueError, namespace="/chat")
|
|
133
|
+
async def handle_chat_error(ctx: EventContext, exc: ValueError):
|
|
134
|
+
return {"error": "chat_error", "message": str(exc)}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Dependency injection
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from typing import Annotated
|
|
141
|
+
from fastapi import Depends
|
|
142
|
+
from zndraw_socketio import wrap
|
|
143
|
+
|
|
144
|
+
async def get_redis():
|
|
145
|
+
return my_redis_pool
|
|
146
|
+
|
|
147
|
+
RedisDep = Annotated[AsyncRedis, Depends(get_redis)]
|
|
148
|
+
|
|
149
|
+
tsio = wrap(socketio.AsyncServer(async_mode="asgi"))
|
|
150
|
+
|
|
151
|
+
@tsio.on(RoomLeave)
|
|
152
|
+
async def room_leave(sid: str, data: RoomLeave, redis: RedisDep) -> RoomLeaveResponse:
|
|
153
|
+
await redis.delete(f"presence:{data.room_id}:{sid}")
|
|
154
|
+
return RoomLeaveResponse(status="ok")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
When FastAPI is not installed, import `Depends` from `zndraw_socketio`:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from zndraw_socketio import Depends
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### FastAPI integration
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
import socketio
|
|
167
|
+
from typing import Annotated
|
|
168
|
+
from fastapi import FastAPI, Depends
|
|
169
|
+
from zndraw_socketio import wrap, AsyncServerWrapper
|
|
170
|
+
|
|
171
|
+
app = FastAPI()
|
|
172
|
+
tsio = wrap(socketio.AsyncServer(async_mode="asgi"))
|
|
173
|
+
|
|
174
|
+
SioServer = Annotated[AsyncServerWrapper, Depends(tsio)]
|
|
175
|
+
|
|
176
|
+
@app.post("/notify")
|
|
177
|
+
async def notify(server: SioServer):
|
|
178
|
+
await server.emit("notification", {"msg": "hello"})
|
|
179
|
+
return {"status": "sent"}
|
|
180
|
+
|
|
181
|
+
combined_app = socketio.ASGIApp(tsio, app)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### SimpleClient support
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
tsio = wrap(socketio.SimpleClient())
|
|
188
|
+
tsio.connect("http://localhost:5000")
|
|
189
|
+
|
|
190
|
+
tsio.emit(Ping(message="Hello"))
|
|
191
|
+
response = tsio.call(Ping(message="Hello"), response_model=Pong)
|
|
192
|
+
event_name, data = tsio.receive(response_model=Pong)
|
|
193
|
+
tsio.disconnect()
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Supported socketio types
|
|
197
|
+
|
|
198
|
+
`wrap()` auto-detects the socketio instance type:
|
|
199
|
+
|
|
200
|
+
| socketio type | Wrapper class |
|
|
201
|
+
|---|---|
|
|
202
|
+
| `AsyncClient` | `AsyncClientWrapper` |
|
|
203
|
+
| `AsyncServer` | `AsyncServerWrapper` |
|
|
204
|
+
| `Client` | `SyncClientWrapper` |
|
|
205
|
+
| `Server` | `SyncServerWrapper` |
|
|
206
|
+
| `SimpleClient` | `SimpleClientWrapper` |
|
|
207
|
+
| `AsyncSimpleClient` | `AsyncSimpleClientWrapper` |
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT License
|