Skip to content

Commit

Permalink
Remove specs interface from Faststream and AsgiFaststream
Browse files Browse the repository at this point in the history
  • Loading branch information
KrySeyt committed Sep 17, 2024
1 parent 595955b commit 94cccd7
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 167 deletions.
23 changes: 0 additions & 23 deletions faststream/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ def __init__(
broker: Optional["BrokerUsecase[Any, Any]"] = None,
logger: Optional["LoggerProto"] = logger,
lifespan: Optional["Lifespan"] = None,
# Specification args,
title: str = "FastStream",
version: str = "0.1.0",
description: str = "",
terms_of_service: Optional["AnyHttpUrl"] = None,
license: Optional[Union["License", "LicenseDict", "AnyDict"]] = None,
contact: Optional[Union["Contact", "ContactDict", "AnyDict"]] = None,
tags: Optional[Sequence[Union["Tag", "AnyDict"]]] = None,
external_docs: Optional[
Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]
] = None,
identifier: Optional[str] = None,
on_startup: Sequence["AnyCallable"] = (),
after_startup: Sequence["AnyCallable"] = (),
on_shutdown: Sequence["AnyCallable"] = (),
Expand Down Expand Up @@ -104,17 +92,6 @@ def __init__(

self._should_exit = anyio.Event()

# Specification information
self.title = title
self.version = version
self.description = description
self.terms_of_service = terms_of_service
self.license = license
self.contact = contact
self.identifier = identifier
self.specs_tags = tags
self.external_docs = external_docs

def set_broker(self, broker: "BrokerUsecase[Any, Any]") -> None:
"""Set already existed App object broker.
Expand Down
21 changes: 0 additions & 21 deletions faststream/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ def __init__(
# regular broker args
logger: Optional["LoggerProto"] = logger,
lifespan: Optional["Lifespan"] = None,
# AsyncAPI args,
title: str = "FastStream",
version: str = "0.1.0",
description: str = "",
terms_of_service: Optional["AnyHttpUrl"] = None,
license: Optional[Union["License", "LicenseDict", "AnyDict"]] = None,
contact: Optional[Union["Contact", "ContactDict", "AnyDict"]] = None,
tags: Optional[Sequence[Union["Tag", "AnyDict"]]] = None,
external_docs: Optional[
Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]
] = None,
identifier: Optional[str] = None,
on_startup: Sequence["AnyCallable"] = (),
after_startup: Sequence["AnyCallable"] = (),
on_shutdown: Sequence["AnyCallable"] = (),
Expand All @@ -65,15 +53,6 @@ def __init__(
broker=broker,
logger=logger,
lifespan=lifespan,
title=title,
version=version,
description=description,
terms_of_service=terms_of_service,
license=license,
contact=contact,
tags=tags,
external_docs=external_docs,
identifier=identifier,
on_startup=on_startup,
after_startup=after_startup,
on_shutdown=on_shutdown,
Expand Down
6 changes: 4 additions & 2 deletions faststream/specification/asyncapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""AsyncAPI related functions."""

from faststream.specification.asyncapi.generate import get_app_schema
from .base import AsyncAPIProto
from .factory import AsyncAPI
from faststream.specification.asyncapi.site import get_asyncapi_html

__all__ = (
"AsyncAPIProto",
"AsyncAPI",
"get_asyncapi_html",
"get_app_schema",
)
6 changes: 4 additions & 2 deletions faststream/specification/asyncapi/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from faststream.specification.asyncapi.base import schema
from .asyncapi import AsyncAPIProto

__all__ = ("schema",)
__all__ = (
"AsyncAPIProto",
)
16 changes: 16 additions & 0 deletions faststream/specification/asyncapi/base/asyncapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from abc import abstractmethod
from typing import Any, Protocol


class AsyncAPIProto(Protocol):
@abstractmethod
def json(self) -> str:
...

@abstractmethod
def jsonable(self) -> Any:
...

@abstractmethod
def yaml(self) -> str:
...
23 changes: 0 additions & 23 deletions faststream/specification/asyncapi/base/asyncapiproto.py

This file was deleted.

36 changes: 19 additions & 17 deletions faststream/specification/asyncapi/factory.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
from typing import Any, Optional, Union, Sequence
from typing import TYPE_CHECKING, Any, Literal, Optional, Sequence, Union

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.base.asyncapi 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

if TYPE_CHECKING:
from faststream._internal.basic_types import AnyDict, AnyHttpUrl
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 __new__(
def __new__( # type: ignore[misc]
cls,
broker: BrokerUsecase[Any, Any],
/,
title: str = "FastStream",
version: str = "0.1.0",
schema_version: str = "2.6.0",
app_version: str = "0.1.0",
schema_version: Union[Literal["3.0.0", "2.6.0"], 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,
contact: Optional[Union["Contact", "ContactDict", "AnyDict"]] = None,
tags: Optional[Sequence[Union["Tag", "TagDict", "AnyDict"]]] = None,
external_docs: Optional[Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]] = None,
identifier: Optional[str] = None,
) -> AsyncAPIProto:
if schema_version.startswith("3.0."):
return AsyncAPI3(
broker,
title=title,
version=version,
app_version=app_version,
schema_version=schema_version,
description=description,
terms_of_service=terms_of_service,
contact=contact,
license=license,
identifier=identifier,
specs_tags=specs_tags,
tags=tags,
external_docs=external_docs,
)
elif schema_version.startswith("2.6."):
return AsyncAPI2(
broker,
title=title,
version=version,
app_version=app_version,
schema_version=schema_version,
description=description,
terms_of_service=terms_of_service,
contact=contact,
license=license,
identifier=identifier,
specs_tags=specs_tags,
tags=tags,
external_docs=external_docs,
)
else:
Expand Down
25 changes: 0 additions & 25 deletions faststream/specification/asyncapi/generate.py

This file was deleted.

6 changes: 5 additions & 1 deletion faststream/specification/asyncapi/v2_6_0/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from .asyncapi import AsyncAPI2
from .generate import get_app_schema

__all__ = ("get_app_schema",)
__all__ = (
"AsyncAPI2",
"get_app_schema",
)
38 changes: 20 additions & 18 deletions faststream/specification/asyncapi/v2_6_0/asyncapi.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Any, Optional, Union, Sequence
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union

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 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
from faststream.specification.asyncapi.base.asyncapi import AsyncAPIProto
from faststream.specification.asyncapi.v2_6_0.generate import get_app_schema

if TYPE_CHECKING:
from faststream._internal.basic_types import AnyDict, AnyHttpUrl
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 AsyncAPI2(AsyncAPIProto):
Expand All @@ -16,69 +18,69 @@ def __init__(
broker: BrokerUsecase[Any, Any],
/,
title: str = "FastStream",
version: str = "0.1.0",
app_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,
tags: Optional[Sequence[Union["Tag", "TagDict", "AnyDict"]]] = None,
external_docs: Optional[Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]] = None,
) -> None:
self.broker = broker
self.title = title
self.version = version
self.app_version = app_version
self.schema_version = schema_version
self.description = description
self.terms_of_service = terms_of_service
self.contact = contact
self.license = license
self.identifier = identifier
self.specs_tags = specs_tags
self.tags = tags
self.external_docs = external_docs

def json(self) -> str:
return get_app_schema(
self.broker,
title=self.title,
app_version=self.version,
app_version=self.app_version,
schema_version=self.schema_version,
description=self.description,
terms_of_service=self.terms_of_service,
contact=self.contact,
license=self.license,
identifier=self.identifier,
specs_tags=self.specs_tags,
tags=self.tags,
external_docs=self.external_docs,
).to_json()

def jsonable(self) -> Any:
return get_app_schema(
self.broker,
title=self.title,
app_version=self.version,
app_version=self.app_version,
schema_version=self.schema_version,
description=self.description,
terms_of_service=self.terms_of_service,
contact=self.contact,
license=self.license,
identifier=self.identifier,
specs_tags=self.specs_tags,
tags=self.tags,
external_docs=self.external_docs,
).to_jsonable()

def yaml(self) -> str:
return get_app_schema(
self.broker,
title=self.title,
app_version=self.version,
app_version=self.app_version,
schema_version=self.schema_version,
description=self.description,
terms_of_service=self.terms_of_service,
contact=self.contact,
license=self.license,
identifier=self.identifier,
specs_tags=self.specs_tags,
tags=self.tags,
external_docs=self.external_docs,
).to_yaml()
19 changes: 12 additions & 7 deletions faststream/specification/asyncapi/v2_6_0/generate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, Sequence
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Union

from faststream._internal._compat import DEF_KEY
from faststream._internal.basic_types import AnyDict, AnyHttpUrl
Expand All @@ -19,14 +19,19 @@
tag_from_spec,
)
from faststream.specification.asyncapi.v2_6_0.schema.message import Message
from faststream.specification.schema.tag import Tag as SpecsTag
from faststream.specification.schema.contact import ContactDict, Contact
from faststream.specification.schema.docs import ExternalDocs, ExternalDocsDict
from faststream.specification.schema.license import License, LicenseDict

if TYPE_CHECKING:
from faststream._internal.broker.broker import BrokerUsecase
from faststream._internal.types import ConnectionType, MsgType
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 as SpecsTag,
)
from faststream.specification.schema.tag import (
TagDict as SpecsTagDict,
)


def get_app_schema(
Expand All @@ -40,7 +45,7 @@ def get_app_schema(
contact: Optional[Union["Contact", "ContactDict", "AnyDict"]],
license: Optional[Union["License", "LicenseDict", "AnyDict"]],
identifier: Optional[str],
specs_tags: Optional[Sequence[Union["SpecsTag", "AnyDict"]]],
tags: Optional[Sequence[Union["SpecsTag", "SpecsTagDict", "AnyDict"]]],
external_docs: Optional[Union["ExternalDocs", "ExternalDocsDict", "AnyDict"]],
) -> Schema:
"""Get the application schema."""
Expand Down Expand Up @@ -70,7 +75,7 @@ def get_app_schema(
asyncapi=schema_version,
defaultContentType=ContentTypes.json.value,
id=identifier,
tags=[tag_from_spec(tag) for tag in specs_tags] if specs_tags else None,
tags=[tag_from_spec(tag) for tag in tags] if tags else None,
externalDocs=docs_from_spec(external_docs) if external_docs else None,
servers=servers,
channels=channels,
Expand Down
Loading

0 comments on commit 94cccd7

Please sign in to comment.