Extend websocket schema decorator for Django Channels
Project description
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
pip install drf-spectacular-websocket
python manage.py collectstatic
for sidecar static- Add app name to
INSTALLED_APPS
INSTALLED_APPS = [
# drf_spectacular_websocket must be higher than drf_spectacular
'drf_spectacular_websocket',
'drf_spectacular',
'drf_spectacular_sidecar',
]
Configure settings
ASGI_APPLICATION = 'path.to.your.application'
# default settings (there is no need to define in your application)
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
},
}
# you can override this default settings in your application:
# connectSocket
# socketMaxMessages
# socketMessagesInitialOpened
About decorator
drf_spectacular_websocket extend_ws_schema
accepts one new type
parameter relative to drf_spectacular extend_schema
.
type
send
- Type of interaction, [request -> response]receive
- Type of interaction, [response without request]
Usage example
drf_spectacular_websocket automatically finds websocket urls and related consumer using
ASGI_APPLICATION
setting.
Direct usage
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)
Send
Receive
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
File details
Details for the file drf_spectacular_websocket-1.2.4.tar.gz
.
File metadata
- Download URL: drf_spectacular_websocket-1.2.4.tar.gz
- Upload date:
- Size: 158.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bdb6501c439b397f5c7a30b5f3fb1d7f4e5fa321b1a38907ec9ee480591ec06 |
|
MD5 | 51a10723c44529fbe8750eb88d534039 |
|
BLAKE2b-256 | a2e9ba23b9b2f0262050a916a4cfbcc3f08bc94c247ca33df9e8e6e887fb4a0a |
File details
Details for the file drf_spectacular_websocket-1.2.4-py3-none-any.whl
.
File metadata
- Download URL: drf_spectacular_websocket-1.2.4-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73a60eeaec3cbe365a694d3760e636109444841cdc427e7ebb033e248ed3925f |
|
MD5 | 0948a96ce184b4942d19c811c54a0137 |
|
BLAKE2b-256 | 3653c5676954eebc4b87f907225b03e3496e39faa1639f38d3581c31cee09f68 |