Skip to main content

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

  1. pip install drf-spectacular-websocket
  2. python manage.py collectstatic for sidecar static
  3. 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


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.5.tar.gz (158.1 kB view details)

Uploaded Source

Built Distribution

drf_spectacular_websocket-1.2.5-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for drf_spectacular_websocket-1.2.5.tar.gz
Algorithm Hash digest
SHA256 b768d81d675e5536ea79863aaebbf39bfd7e61714841008757a679c0cc5f387a
MD5 5247523c7967bceac3cb8f1b883411e0
BLAKE2b-256 d52bea9a302024d94e440624c2e18af7a782f8f4fdf16288f920269ebe83919c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for drf_spectacular_websocket-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e66769016e767114c2666f4e3eea65c9045a232c51e0bca41cf28caec622389e
MD5 61beb3f3b77bd5b539f26085764b8f03
BLAKE2b-256 b8e436b02fdd8804a4d61ff404a1f69be3c22fb765b7b93700370014c0926d04

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