Extend websocket schema decorator for Django Channels
Provides extend_ws_schema decorator to documentation Cunsumer methods from channels just like it does drf-spectacular
Install
-
Install package
pip install drf-spectacular-websocket
-
Create sidecar static
python manage.py collectstatic
-
Add app name to
INSTALLED_APPSdrf_spectacular_websocketmust be higher thandrf_spectacularINSTALLED_APPS = [ 'drf_spectacular_websocket', 'drf_spectacular', 'drf_spectacular_sidecar', ]
Configure settings
ASGI_APPLICATION = 'path.to.your.application'
# (Optional) this is default settings are automatically set by the drf_spectacular_websocket.
# You can override them in your application if necessary.
SPECTACULAR_SETTINGS = {
'DEFAULT_GENERATOR_CLASS': 'drf_spectacular_websocket.schemas.WsSchemaGenerator',
'SWAGGER_UI_DIST': 'SIDECAR',
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
'SWAGGER_UI_SETTINGS': {
'connectSocket': True, # Automatically establish a WS connection when opening swagger
'socketMaxMessages': 8, # Max number of messages displayed in the log window in swagger
'socketMessagesInitialOpened': False, # Automatically open the log window when opening swagger
},
}
drf_spectacular_websocket automatically finds websocket urls and related consumer using
ASGI_APPLICATIONsetting.
About decorator
extend_ws_schema decorator accepts one new type parameter relative to drf_spectacular extend_schema.
- possible values:
send- Type of interaction, [request -> response]receive- Type of interaction, [response without request]
Usage example
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from rest_framework.serializers import Serializer, CharField
from drf_spectacular_websocket.decorators import extend_ws_schema
class SomeMethodInputSerializer(Serializer):
some_field = CharField()
class SomeMethodOutputSerializer(Serializer):
some_field = CharField()
class SendMessageOutputSerializer(Serializer):
some_field = CharField()
class SomeConsumer(AsyncJsonWebsocketConsumer):
async def receive_json(self, content, **kwargs):
some_value = content.get('some_key')
if some_value:
await self.some_method(some_value)
@extend_ws_schema(
type='send',
summary='some_method_summary',
description='some_method_description',
request=SomeMethodInputSerializer,
responses=SomeMethodOutputSerializer,
)
async def some_method(self, some_value):
input_serializer = SomeMethodInputSerializer(data=some_value)
input_serializer.is_valid(raise_exception=True)
return_value = await some_business_logic(input_serializer.validated_data)
output_serializer = SomeMethodOutputSerializer(data=return_value)
output_serializer.is_valid(raise_exception=True)
await self.send_json(output_serializer.validated_data)
@extend_ws_schema(
type='receive',
summary='send_message_summary',
description='send_message_description',
request=None,
responses=SendMessageOutputSerializer,
)
async def send_message(self, message):
await self.send_json(message)
Contributing
We would love you to contribute to drf-spectacular-websocket, pull requests are very welcome! Please see CONTRIBUTING.md for more information.
Swagger Examples
Send
Receive
Metadata
Release files for drf-spectacular-websocket 1.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_spectacular_websocket-1.3.1.tar.gz | 164.3 kB | With provenance | Details for drf_spectacular_websocket-1.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_spectacular_websocket-1.3.1-py3-none-any.whl | Python 3 | none | any | Details for drf_spectacular_websocket-1.3.1-py3-none-any.whl |
Total release size: 181.1 kB
Release files / drf_spectacular_websocket-1.3.1.tar.gz
| Download URL | drf_spectacular_websocket-1.3.1.tar.gz |
|---|---|
| Size | 164.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9203df1e5e9e2ca51a9f61104b2fc3f84346783333e4d8782c825a91ed60246c
|
|
BLAKE2b-256 checksum How to use checksums |
4e57104b48aa0bc3d28bbce19774c9bfe4ff5c0cd92a584931c3b200cd78970b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2024.
Transparency logRelease files / drf_spectacular_websocket-1.3.1-py3-none-any.whl
| Download URL | drf_spectacular_websocket-1.3.1-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aeda4bf9c25a4d3888f1ebccadc9dc8da7f25c5c2884f08bead5a3e8e467f280
|
|
BLAKE2b-256 checksum How to use checksums |
ddbed8a805df17735aeeef06c04a86cf570c78dc758c8ac54a1b4ae6a9c8a894
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2024.
Transparency logSecurity for drf-spectacular-websocket 1.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for drf_spectacular_websocket-1.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2024.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.