diff --git a/faststream/specification/asyncapi/base/asyncapi.py b/faststream/specification/asyncapi/base/asyncapiproto.py similarity index 95% rename from faststream/specification/asyncapi/base/asyncapi.py rename to faststream/specification/asyncapi/base/asyncapiproto.py index b8357567da..4d116443d4 100644 --- a/faststream/specification/asyncapi/base/asyncapi.py +++ b/faststream/specification/asyncapi/base/asyncapiproto.py @@ -9,7 +9,7 @@ from faststream.specification.schema.tag import Tag -class AsyncAPI(Protocol): +class AsyncAPIProto(Protocol): @abstractmethod def json(self) -> str: ... diff --git a/faststream/specification/asyncapi/facade.py b/faststream/specification/asyncapi/facade.py new file mode 100644 index 0000000000..42100ae531 --- /dev/null +++ b/faststream/specification/asyncapi/facade.py @@ -0,0 +1,68 @@ +from typing import Any, Optional, Union, Sequence + +from faststream._internal.basic_types import AnyHttpUrl, AnyDict +from faststream._internal.broker.broker import BrokerUsecase +from faststream.specification.asyncapi.base.asyncapiproto import AsyncAPIProto +from faststream.specification.asyncapi.v2_6_0.asyncapi import AsyncAPI2 +from faststream.specification.asyncapi.v3_0_0.asyncapi import AsyncAPI3 +from faststream.specification.schema.contact import Contact, ContactDict +from faststream.specification.schema.docs import ExternalDocs, ExternalDocsDict +from faststream.specification.schema.license import License, LicenseDict +from faststream.specification.schema.tag import Tag, TagDict + + +class AsyncAPI(AsyncAPIProto): + def __init__( + self, + broker: BrokerUsecase[Any, Any], + /, + title: str = "FastStream", + version: str = "0.1.0", + schema_version: str = "2.6.0", + description: str = "", + terms_of_service: Optional["AnyHttpUrl"] = None, + contact: Optional[Union["Contact", "ContactDict", "AnyDict"]] = None, + license: Optional[Union["License", "LicenseDict", "AnyDict"]] = None, + identifier: Optional[str] = None, + specs_tags: Optional[Sequence[Union["Tag", "TagDict", "AnyDict"]]] = None, + external_docs: Optional[Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]] = None, + ): + if schema_version.startswith("3.0."): + self.schema = AsyncAPI3( + broker, + title=title, + version=version, + schema_version=schema_version, + description=description, + terms_of_service=terms_of_service, + contact=contact, + license=license, + identifier=identifier, + specs_tags=specs_tags, + external_docs=external_docs, + ) + elif schema_version.startswith("2.6."): + self.schema = AsyncAPI2( + broker, + title=title, + version=version, + schema_version=schema_version, + description=description, + terms_of_service=terms_of_service, + contact=contact, + license=license, + identifier=identifier, + specs_tags=specs_tags, + external_docs=external_docs, + ) + else: + raise NotImplementedError(f"Unsupported schema version: {schema_version}") + + def json(self) -> str: + return self.schema.json() + + def jsonable(self) -> str: + return self.schema.jsonable() + + def yaml(self) -> str: + return self.schema.yaml() diff --git a/faststream/specification/asyncapi/v2_6_0/asyncapi.py b/faststream/specification/asyncapi/v2_6_0/asyncapi.py index 3018a1edce..f675886954 100644 --- a/faststream/specification/asyncapi/v2_6_0/asyncapi.py +++ b/faststream/specification/asyncapi/v2_6_0/asyncapi.py @@ -2,7 +2,7 @@ from faststream._internal.basic_types import AnyHttpUrl, AnyDict from faststream._internal.broker.broker import BrokerUsecase -from faststream.specification.asyncapi.base.asyncapi import AsyncAPI +from faststream.specification.asyncapi.base.asyncapiproto import AsyncAPIProto from faststream.specification.asyncapi.v2_6_0 import get_app_schema from faststream.specification.schema.contact import Contact, ContactDict from faststream.specification.schema.docs import ExternalDocs, ExternalDocsDict @@ -10,7 +10,7 @@ from faststream.specification.schema.tag import Tag, TagDict -class AsyncAPI2(AsyncAPI): +class AsyncAPI2(AsyncAPIProto): def __init__( self, broker: BrokerUsecase[Any, Any], diff --git a/faststream/specification/asyncapi/v3_0_0/asyncapi.py b/faststream/specification/asyncapi/v3_0_0/asyncapi.py index a9950bbec1..74c43dcb45 100644 --- a/faststream/specification/asyncapi/v3_0_0/asyncapi.py +++ b/faststream/specification/asyncapi/v3_0_0/asyncapi.py @@ -2,15 +2,15 @@ from faststream._internal.basic_types import AnyHttpUrl, AnyDict from faststream._internal.broker.broker import BrokerUsecase -from faststream.specification.asyncapi.base.asyncapi import AsyncAPI -from faststream.specification.asyncapi.v2_6_0 import get_app_schema +from faststream.specification.asyncapi.base.asyncapiproto import AsyncAPIProto +from faststream.specification.asyncapi.v3_0_0 import get_app_schema from faststream.specification.schema.contact import Contact, ContactDict from faststream.specification.schema.docs import ExternalDocs, ExternalDocsDict from faststream.specification.schema.license import LicenseDict, License from faststream.specification.schema.tag import Tag, TagDict -class AsyncAPI3(AsyncAPI): +class AsyncAPI3(AsyncAPIProto): def __init__( self, broker: BrokerUsecase[Any, Any],