Skip to main content

AsyncAPI (v2) specification schema as pydantic class

Project description

asyncapi-schema-pydantic

AsyncAPI (v2) specification schema as Pydantic classes.

The naming of the classes follows the schema in AsyncAPI specification.

Installation

pip install asyncapi-schema-pydantic

Try me

from asyncapi_schema_pydantic import (  AsyncAPI,
                                        Info,
                                        ChannelItem,
                                        Operation,
                                        Message,
                                        ChannelBindings,
                                        AmqpChannelBinding,
                                        AmqpQueue,
                                        Tag )

# Construct AsyncAPI by pydantic objects
async_api = AsyncAPI(
    info=Info(
        title="Email Service",
        version="1.0.0",
        description='description'
    ),
    channels={
        "user/signedup": ChannelItem(
            description='This channel is used to exchange messages about users signing up',
            subscribe=Operation(
                summary='A user signed up.',
                message=Message(
                    name='UserSignup',
                    title='User signup',
                    summary='Action to sign a user up.',
                    description='A longer description of the message',
                    contentType='application/json',
                    tags=[
                        Tag(name='user'),
                        Tag(name='signup'),
                        Tag(name='register')
                    ]
                ),
            ),
            bindings=ChannelBindings(
                amqp=AmqpChannelBinding(
                    param_is='queue',
                    queue=AmqpQueue(
                        name='my-queue-name',
                        durable=True,
                        exclusive=True,
                        autoDelete=False,
                        vhost='/'
                    )
                )
            )
        )
    }
)

print(async_api.json(by_alias=True, exclude_none=True, indent=2))

Result:

{
  "asyncapi": "2.3.0",
  "info": {
    "title": "Email Service",
    "version": "1.0.0",
    "description": "description"
  },
  "channels": {
    "user/signedup": {
      "description": "This channel is used to exchange messages about users signing up",
      "subscribe": {
        "summary": "A user signed up.",
        "message": {
          "contentType": "application/json",
          "name": "UserSignup",
          "title": "User signup",
          "summary": "Action to sign a user up.",
          "description": "A longer description of the message",
          "tags": [
            {
              "name": "user"
            },
            {
              "name": "signup"
            },
            {
              "name": "register"
            }
          ]
        }
      },
      "bindings": {
        "amqp": {
          "queue": {
            "name": "my-queue-name",
            "durable": true,
            "exclusive": true,
            "autoDelete": false,
            "vhost": "/"
          }
        }
      }
    }
  }
}

Take advantage of Pydantic

Pydantic is a great tool, allow you to use object / dict / mixed data for for input.

The following examples give the same AsyncAPI result as above:

from asyncapi_schema_pydantic import AsyncAPI, ChannelItem, Operation

# Construct AsyncAPI from dict
async_api = AsyncAPI.parse_obj({
  "asyncapi": "2.3.0",
  "info": {
    "title": "Email Service",
    "version": "1.0.0",
    "description": "description"
  },
  "channels": {
    "user/signedup": {
      "description": "This channel is used to exchange messages about users signing up",
      "subscribe": {
        "summary": "A user signed up.",
        "message": {
          "contentType": "application/json",
          "name": "UserSignup",
          "title": "User signup",
          "summary": "Action to sign a user up.",
          "description": "A longer description of the message"
        }
      }
    }
  }
})

# Construct AsyncAPI with mix of dict/object
async_api = AsyncAPI.parse_obj({
  "asyncapi": "2.3.0",
  "info": {
    "title": "Email Service",
    "version": "1.0.0",
    "description": "description"
  },
  "channels": {
    "user/signedup": ChannelItem(
            description='This channel is used to exchange messages about users signing up',
            subscribe=Operation(
                summary='A user signed up.',
                message={
                  "contentType": "application/json",
                  "name": "UserSignup",
                  "title": "User signup",
                  "summary": "Action to sign a user up.",
                  "description": "A longer description of the message"
                }
            )
        )
  }
})

Load and validate an AsyncAPI specification from a YAML file

from asyncapi_schema_pydantic import AsyncAPI

async_api = AsyncAPI.load_from_file("tests/data/sample.yaml")
print(async_api.json(by_alias=True, exclude_none=True, indent=2))

License

MIT License

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

asyncapi-schema-pydantic-1.0.1.tar.gz (35.7 kB view details)

Uploaded Source

Built Distributions

asyncapi_schema_pydantic-1.0.1-py3-none-any.whl (54.6 kB view details)

Uploaded Python 3

asyncapi_schema_pydantic-1.0.1-py2.py3-none-any.whl (54.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file asyncapi-schema-pydantic-1.0.1.tar.gz.

File metadata

File hashes

Hashes for asyncapi-schema-pydantic-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1d8eccba006bc95958f80df462d6ae264f4fecdfe45a1087ef27b50c5010f852
MD5 95fc2bbde2f27fb6d24e2ccdeebc9362
BLAKE2b-256 c5c82cbf5649d1c9bfa5adfa60d73b54f2f88abb83b1abf6cfd3638873b582ca

See more details on using hashes here.

File details

Details for the file asyncapi_schema_pydantic-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncapi_schema_pydantic-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0279183596f0f046900c061ebdc5c29be60da208086922c6971f2729d735f9df
MD5 d9989da9b0432f258fad4b99a04050ae
BLAKE2b-256 ab72689c5ffbe2b09186704e498e647f12898576fbd7191e133b280803b1a326

See more details on using hashes here.

File details

Details for the file asyncapi_schema_pydantic-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for asyncapi_schema_pydantic-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1c6b8a5af4fdee13cabb78e63d917cc5e36024bf84f397c6c58eb194c91ba3d
MD5 bd1d1b446d91405d3813f1ae8cd7e0f9
BLAKE2b-256 05a75d08a822f2d2917f97f0a4c0ffccd60e221d723a3a5edb9226dad3fba13e

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