Skip to main content

Autodoc for socket

Project description

Autodoc for Socketio and Fastapi

Create autodoc for socketio and access using API Router.

Use decorator and method for describe the event running, Use sub for 'subscribe' and use pub for 'publish'. This code also integrated with pydantic, so it will be detect data model in your params if using pydantic. Beside that other type data like array or general type like str, int, etc can be solved. Other type from python typing like Optional and Union especially Union with none type can be solved.

Example Using

To use this import the class SokcetDocumentations, and assign the class to var. Use decorator (@) to create documentation. There is two option to create documentation. First is class method sub to define the event under decorator is subscribe, The second is pub to define the event or method under decorator is to publish. In this bellow is example how to use the code:

from pdc_event_autodoc import SocketDocumentation 

doc = SocketDocumentation()

First time is set your application info like bellow:

doc.set_info_app(title= 'Example App', version= '1.0.0', description= 'This is example documentation of Example App version 1.0.0')

You can decla your params using pydantic model:

class PydanticModel(BaseModel):
    id: int
    name: str
    address: str

# Example using pydantic model
@doc.sub(event_name= 'message', 
        tags= ['SubcribeMessage'], 
        schema= PydanticModel, 
        summary= 'ReceiveMessage', 
        description= 'This is event when we receive a message.')
@sm.on('message')
def receive_message(sid, data: PydanticModel):
    return data.dict()

Or using general type like

@doc.pub(event_name= 'message', 
        tags= ['PublishMessage'],
        summary= 'To Send a Message', 
        description= 'This is event when we want to send a message.')
def sending_message(sid, data: str):
    return sm.emit('message', data)

To preview the documentation, you can using FastAPI or APIRouter. This is an example using APIRouter:

from fastapi import APIRouter
from fastapi.encoders import jsonable_encoder
from pydantic import AnyHttpUrl

from pdc_event_autodoc import SocketDocumentation, get_asyncapi_html

doc = SocketDocumentation()

router = APIRouter(tags= ['Socket Documentations])

@router.get('/socket.json')
def get_socket_json():
    return jsonable_encoder(doc.main_data)

@router.get('/socket_doc')
def get_socket_documentation():
    async_url= AnyHttpUrl('/socket.json', scheme= 'http')
    return get_asyncapi_html(asyncapi_url= async_url, title= 'Notification Service')

In AnyHttpUrl if you using prefix, don't forget to include your prefix.!

The documentattion can be access in path '/socket_doc'.

Notice

Dont't forget to declare your type params (it can be Pydantic model, array, str, int, Optional or Union, etc).

If not be declare it will be detect that your function using Any params.

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

pdc_event_autodoc-1.0.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdc_event_autodoc-1.0.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file pdc_event_autodoc-1.0.0.tar.gz.

File metadata

  • Download URL: pdc_event_autodoc-1.0.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.7 Windows/10

File hashes

Hashes for pdc_event_autodoc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 58098ab9736cb5c63532bc72b6b7b49e4e46c4a299b81f45874ee9714fe1216f
MD5 dad53feedf69c4e5a569947c65a69530
BLAKE2b-256 0a5ca049e4e82db833e7006a9dd00d2131652a739feb595f2193b25d975bf898

See more details on using hashes here.

File details

Details for the file pdc_event_autodoc-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pdc_event_autodoc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.7 Windows/10

File hashes

Hashes for pdc_event_autodoc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b63099824c745f62f81fa3dd7e473797f9b2648e1807fc25bef8f7730e4e030
MD5 3c643920862fe03049d89cfc48f000de
BLAKE2b-256 452d9bfe58fb6483695e4c743fbf28bb01c2ae4358d4e034cd26b440707dfe68

See more details on using hashes here.

Supported by

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