Skip to content

Commit

Permalink
AsyncAPI facade
Browse files Browse the repository at this point in the history
  • Loading branch information
KrySeyt committed Sep 17, 2024
1 parent c13e802 commit d27ee74
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from faststream.specification.schema.tag import Tag


class AsyncAPI(Protocol):
class AsyncAPIProto(Protocol):
@abstractmethod
def json(self) -> str:
...
Expand Down
68 changes: 68 additions & 0 deletions faststream/specification/asyncapi/facade.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions faststream/specification/asyncapi/v2_6_0/asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.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
from faststream.specification.schema.license import LicenseDict, License
from faststream.specification.schema.tag import Tag, TagDict


class AsyncAPI2(AsyncAPI):
class AsyncAPI2(AsyncAPIProto):
def __init__(
self,
broker: BrokerUsecase[Any, Any],
Expand Down
6 changes: 3 additions & 3 deletions faststream/specification/asyncapi/v3_0_0/asyncapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit d27ee74

Please sign in to comment.