|
| 1 | +from typing import List, Literal, Optional, Type |
| 2 | + |
| 3 | +import pydantic_asyncapi.v3 as pa |
| 4 | +from pydantic import BaseModel |
| 5 | + |
| 6 | +_info: pa.Info = pa.Info( |
| 7 | + title="AsyncAPI", |
| 8 | + version="1.0.0", |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +_servers = {} # type: ignore |
| 13 | +_channels = {} # type: ignore |
| 14 | +_operations = {} # type: ignore |
| 15 | +_components_schemas = {} # type: ignore |
| 16 | + |
| 17 | + |
| 18 | +def get_schema() -> pa.AsyncAPI: |
| 19 | + """ |
| 20 | + Function `get_schema` provides the complete AsyncAPI schema for the application, complying with |
| 21 | + version 3.0.0 of the AsyncAPI specification. It includes detailed information about info metadata, |
| 22 | + components, servers, channels, and operations required to set up and describe the asynchronous |
| 23 | + communication layer. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + pa.AsyncAPI: A fully constructed AsyncAPI schema object based on predefined configurations. |
| 27 | + """ |
| 28 | + return pa.AsyncAPI( |
| 29 | + asyncapi="3.0.0", |
| 30 | + info=_info, |
| 31 | + components=pa.Components( |
| 32 | + schemas=_components_schemas, |
| 33 | + ), |
| 34 | + servers=_servers, |
| 35 | + channels=_channels, |
| 36 | + operations=_operations, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def init_asyncapi_info( |
| 41 | + title: str, |
| 42 | + version: str = "1.0.0", |
| 43 | +) -> None: |
| 44 | + """ |
| 45 | + Initializes the AsyncAPI information object with the specified title and version. |
| 46 | +
|
| 47 | + This function creates and initializes an AsyncAPI Info object, which includes |
| 48 | + mandatory fields such as title and version. The title represents the name of the |
| 49 | + AsyncAPI document, and the version represents the version of the API. |
| 50 | +
|
| 51 | + Parameters: |
| 52 | + title (str): The title of the AsyncAPI document. |
| 53 | + version (str): The version of the AsyncAPI document. Defaults to "1.0.0". |
| 54 | +
|
| 55 | + Returns: |
| 56 | + None |
| 57 | + """ |
| 58 | + # We can potentially add the other info supported by pa.Info |
| 59 | + global _info |
| 60 | + _info = pa.Info( |
| 61 | + title=title, |
| 62 | + version=version, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def register_server( |
| 67 | + id: str, |
| 68 | + host: str, |
| 69 | + protocol: str, |
| 70 | + pathname: Optional[str] = None, |
| 71 | +) -> None: |
| 72 | + """ |
| 73 | + Registers a server with a unique identifier and its associated properties. |
| 74 | + This function accepts information about the server such as its host, |
| 75 | + protocol, and optionally its pathname, and stores it in the internal |
| 76 | + server registry identified by the unique ID. The parameters must be |
| 77 | + provided appropriately for proper registration. The server registry |
| 78 | + ensures that server configurations can be retrieved and managed based |
| 79 | + on the assigned identifier. |
| 80 | +
|
| 81 | + Args: |
| 82 | + id: str |
| 83 | + A unique identifier for the server being registered. It is used |
| 84 | + as the key in the internal server registry. |
| 85 | + host: str |
| 86 | + The host address of the server. This may be an IP address or |
| 87 | + a domain name. |
| 88 | + protocol: str |
| 89 | + Communication protocol used by the server, such as "http" or "https". |
| 90 | + pathname: Optional[str] |
| 91 | + The optional pathname of the server. If provided, it will be |
| 92 | + associated with the registered server. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + None |
| 96 | + This function does not return a value. It modifies the internal |
| 97 | + server registry to include the provided server details. |
| 98 | + """ |
| 99 | + # TODO: Implement other server parameters |
| 100 | + _servers[id] = pa.Server( |
| 101 | + host=host, |
| 102 | + protocol=protocol, |
| 103 | + ) |
| 104 | + if pathname is not None: |
| 105 | + _servers[id].pathname = pathname |
| 106 | + |
| 107 | + |
| 108 | +def _create_base_channel(address: str, channel_id: str) -> pa.Channel: |
| 109 | + """Create a basic channel with minimum required parameters.""" |
| 110 | + return pa.Channel( |
| 111 | + address=address, |
| 112 | + servers=[], |
| 113 | + messages={}, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def _add_channel_metadata(channel: pa.Channel, description: Optional[str], title: Optional[str]) -> None: |
| 118 | + """Add optional metadata to the channel.""" |
| 119 | + if description is not None: |
| 120 | + channel.description = description |
| 121 | + if title is not None: |
| 122 | + channel.title = title |
| 123 | + |
| 124 | + |
| 125 | +def _add_server_reference(channel: pa.Channel, server_id: Optional[str]) -> None: |
| 126 | + """Add server reference to the channel if server exists.""" |
| 127 | + if server_id is not None and server_id in _servers: |
| 128 | + channel.servers.append(pa.Reference(ref=f"#/servers/{server_id}")) # type: ignore |
| 129 | + |
| 130 | + |
| 131 | +def register_channel( |
| 132 | + address: str, |
| 133 | + id: Optional[str] = None, |
| 134 | + description: Optional[str] = None, |
| 135 | + title: Optional[str] = None, |
| 136 | + server_id: Optional[str] = None, |
| 137 | +) -> None: |
| 138 | + """ |
| 139 | + Registers a communication channel with the specified parameters. |
| 140 | +
|
| 141 | + Args: |
| 142 | + address (str): The address of the channel. |
| 143 | + id (Optional[str]): Unique identifier for the channel. Defaults to None. |
| 144 | + description (Optional[str]): Description of the channel. Defaults to None. |
| 145 | + title (Optional[str]): Title to be associated with the channel. Defaults to None. |
| 146 | + server_id (Optional[str]): Server identifier to link this channel to. Defaults to None. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + None |
| 150 | + """ |
| 151 | + channel_id = id or address |
| 152 | + channel = _create_base_channel(address, channel_id) |
| 153 | + _add_channel_metadata(channel, description, title) |
| 154 | + _add_server_reference(channel, server_id) |
| 155 | + _channels[channel_id] = channel |
| 156 | + |
| 157 | + |
| 158 | +def _register_message_schema(message: Type[BaseModel], operation_type: Literal["receive", "send"]) -> None: |
| 159 | + """Register message schema in components schemas.""" |
| 160 | + message_json_schema = message.model_json_schema( |
| 161 | + mode="validation" if operation_type == "receive" else "serialization", |
| 162 | + ref_template="#/components/schemas/{model}", |
| 163 | + ) |
| 164 | + |
| 165 | + _components_schemas[message.__name__] = message_json_schema |
| 166 | + |
| 167 | + if message_json_schema.get("$defs"): |
| 168 | + _components_schemas.update(message_json_schema["$defs"]) |
| 169 | + |
| 170 | + |
| 171 | +def _create_channel_message(channel_id: str, message: Type[BaseModel]) -> pa.Reference: |
| 172 | + """Create channel message and return reference to it.""" |
| 173 | + _channels[channel_id].messages[message.__name__] = pa.Message( # type: ignore |
| 174 | + payload=pa.Reference(ref=f"#/components/schemas/{message.__name__}") |
| 175 | + ) |
| 176 | + return pa.Reference(ref=f"#/channels/{channel_id}/messages/{message.__name__}") |
| 177 | + |
| 178 | + |
| 179 | +def register_channel_operation( |
| 180 | + channel_id: str, |
| 181 | + operation_type: Literal["receive", "send"], |
| 182 | + messages: List[Type[BaseModel]], |
| 183 | + operation_name: Optional[str] = None, |
| 184 | +) -> None: |
| 185 | + """ |
| 186 | + Registerm a channel operation with associated messages. |
| 187 | +
|
| 188 | + Args: |
| 189 | + channel_id: Channel identifier |
| 190 | + operation_type: Type of operation ("receive" or "send") |
| 191 | + messages: List of message models |
| 192 | + operation_name: Optional operation name |
| 193 | +
|
| 194 | + Raises: |
| 195 | + ValueError: If channel_id doesn't exist |
| 196 | + """ |
| 197 | + if not _channels.get(channel_id): |
| 198 | + raise ValueError(f"Channel {channel_id} does not exist.") |
| 199 | + |
| 200 | + operation_message_refs = [] |
| 201 | + |
| 202 | + for message in messages: |
| 203 | + _register_message_schema(message, operation_type) |
| 204 | + message_ref = _create_channel_message(channel_id, message) |
| 205 | + operation_message_refs.append(message_ref) |
| 206 | + |
| 207 | + operation_id = operation_name or f"{channel_id}-{operation_type}" |
| 208 | + _operations[operation_id] = pa.Operation( |
| 209 | + action=operation_type, |
| 210 | + channel=pa.Reference(ref=f"#/channels/{channel_id}"), |
| 211 | + messages=operation_message_refs, |
| 212 | + traits=[], |
| 213 | + ) |
| 214 | + |
| 215 | + # TODO: Define operation traits |
| 216 | + # if operation_name is not None: |
| 217 | + # _operations[operation_name or f"{channel_id}-{operation_type}"].traits.append( |
| 218 | + # pa.OperationTrait( |
| 219 | + # title=operation_name, |
| 220 | + # summary=f"{operation_name} operation summary", |
| 221 | + # description=f"{operation_name} operation description", |
| 222 | + # ) |
| 223 | + # ) |
0 commit comments