Skip to content

Commit

Permalink
fix: separate fastapi and yaml dependencies (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik authored Sep 12, 2023
1 parent e2aab0c commit b891a64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions faststream/__about__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
"""Simple and fast framework to create message brokers based microservices"""
__version__ = "0.0.1-dev20230912"


INSTALL_YAML = """
To generate YAML documentation, please install dependencies:\n
pip install PyYAML
"""
10 changes: 6 additions & 4 deletions faststream/asyncapi/generate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Dict, Union

from faststream._compat import is_installed
from faststream.app import FastStream
from faststream.asyncapi.schema import (
Channel,
Expand All @@ -10,11 +11,12 @@
Schema,
Server,
)
from faststream.broker.fastapi.router import StreamRouter
from faststream.constants import ContentTypes

if is_installed("fastapi"):
from faststream.broker.fastapi.router import StreamRouter

def get_app_schema(app: Union[FastStream, StreamRouter[Any]]) -> Schema:
def get_app_schema(app: Union[FastStream, "StreamRouter[Any]"]) -> Schema:
"""Get the application schema.
Args:
Expand Down Expand Up @@ -95,7 +97,7 @@ def get_app_schema(app: Union[FastStream, StreamRouter[Any]]) -> Schema:


def get_app_broker_server(
app: Union[FastStream, StreamRouter[Any]]
app: Union[FastStream, "StreamRouter[Any]"]
) -> Dict[str, Server]:
"""Get the broker server for an application.
Expand Down Expand Up @@ -149,7 +151,7 @@ def get_app_broker_server(


def get_app_broker_channels(
app: Union[FastStream, StreamRouter[Any]]
app: Union[FastStream, "StreamRouter[Any]"]
) -> Dict[str, Channel]:
"""Get the broker channels for an application.
Expand Down
17 changes: 12 additions & 5 deletions faststream/cli/docs/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Optional

import typer
import yaml

from faststream.__about__ import INSTALL_YAML
from faststream._compat import model_parse
from faststream.asyncapi.generate import get_app_schema
from faststream.asyncapi.schema import Schema
Expand Down Expand Up @@ -41,9 +41,17 @@ def serve(
schema_filepath = Path.cwd() / app
if schema_filepath.suffix == ".json":
data = schema_filepath.read_text()

elif schema_filepath.suffix == ".yaml" or schema_filepath.suffix == ".yml":
try:
import yaml
except ImportError as e: # pragma: no cover
typer.echo(INSTALL_YAML, err=True)
raise typer.Exit(1) from e

with schema_filepath.open("r") as f:
schema = yaml.safe_load(f)

data = json.dumps(schema)
else:
raise ValueError(
Expand Down Expand Up @@ -91,10 +99,7 @@ def gen(
try:
schema = raw_schema.to_yaml()
except ImportError as e: # pragma: no cover
typer.echo(
"To generate YAML documentation, please install dependencies:\n"
"pip install PyYAML"
)
typer.echo(INSTALL_YAML, err=True)
raise typer.Exit(1) from e

name = out or "asyncapi.yaml"
Expand All @@ -108,3 +113,5 @@ def gen(

with open(name, "w") as f:
json.dump(schema, f, indent=2)

typer.echo(f"Your project AsyncAPI scheme was placed to `{name}`")

0 comments on commit b891a64

Please sign in to comment.