Skip to main content

Extend websocket schema decorator for Django Channels

Project description

Extend websocket schema decorator for Django Channels

Project Status
CI/CD Latest Release
Quality Coverage
Package PyPI - Version PyPI - Support Python Versions Project PyPI - Downloads
Meta types - Mypy License - MIT code style - Ruff

Provides extend_ws_schema decorator to documentation Cunsumer methods from channels just like it does drf-spectacular

Install

  1. Install package

    pip install drf-spectacular-websocket
    
  2. Create sidecar static

    python manage.py collectstatic
    
  3. Add app name to INSTALLED_APPS

    drf_spectacular_websocket must be higher than drf_spectacular

    INSTALLED_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_APPLICATION setting.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_spectacular_websocket-1.2.7.tar.gz (162.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file drf_spectacular_websocket-1.2.7.tar.gz.

File metadata

File hashes

Hashes for drf_spectacular_websocket-1.2.7.tar.gz
Algorithm Hash digest
SHA256 ee95b08b7696071a18ae491c802f7fdebd976d7b8247e1c08dfc3872b64b464c
MD5 4dc593b4279fed45ce2dfbea9dd69d40
BLAKE2b-256 6a85dc2bc00dc4422e48f2fe2307a9027865fc916256b7e0732dfe81e14f02dc

See more details on using hashes here.

File details

Details for the file drf_spectacular_websocket-1.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_spectacular_websocket-1.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b9ccf83523a6443c9b05b2ac5e0862d2e644c1eb3265cfc92230e45564cb9cbd
MD5 e17f1318a0878f1c9ae0c07ff931a270
BLAKE2b-256 a6f4e0d11e9c386439001bdfed0517b2311a4e0b6a75e256f2306e4b2fbe1429

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page