Files
connexion/examples/frameworks/hello_starlette.py
Robbe Sneyders edb0381af3 Implement user facing interface for ConnexionMiddleware (#1621)
This PR adds an interface for the ConnexionMiddleware, similar to the
interface of the Connexion Apps.

The Connexion Apps are now a simple wrapper around the
ConnexionMiddleware and framework app, delegating the work to the
middleware. This enables a similar interface and behavior for users when
using either the middleware or apps.

The arguments are repeated everywhere there is a user interface, but are
parsed in a central place. Repeating the arguments is not DRY, but
needed to provide users with IDE autocomplete, typing, etc. They are
parsed in a single `_Options` class, which also provides a mechanism to
set default options on an App level, and override them on the more
granular API level.

This makes the long list of provided parameters a lot more manageable,
so I would like to use it for the `Jsonifier` as well, and re-add the
`debug` and `extra_files` arguments which I have dropped in previous
PRs. I'll submit a separate PR for this.

I renamed the `options` parameter to `swagger_ui_options` since it only
contains swagger UI options. This is a breaking change though, and we'll
need to highlight this upon release.

We still have quite a lot of `App`, `MiddlewareApp`, and abstract
classes. It would be great if we could find a way to reduce those
further, or at least find better naming to make it more clear what each
one does 🙂 .

Finally, I added examples on how the middleware can be used with third
party frameworks under `examples/frameworks`. Currently there's an
example for Starlette and Quart, but this should be easy to extend. They
also show how the `ASGIDecorator` and `StarletteDecorator` from my
previous PR can be used.
2023-01-26 14:40:29 +01:00

35 lines
954 B
Python

import typing as t
from pathlib import Path
import connexion
from connexion.decorators import StarletteDecorator
from connexion.resolver import RelativeResolver
from starlette.applications import Starlette
from starlette.routing import Route
@StarletteDecorator()
def post_greeting(name: str, number: t.Optional[int] = None) -> str:
return f"Hello {name}, your number is {number}!"
app = Starlette(
debug=True,
routes=[
Route("/openapi/greeting/{name}", post_greeting, methods=["POST"]),
Route("/swagger/greeting/{name}", post_greeting, methods=["POST"]),
],
)
app = connexion.ConnexionMiddleware(
app,
specification_dir="spec/",
resolver=RelativeResolver("hello_starlette"),
)
app.add_api("openapi.yaml", arguments={"title": "Hello World Example"})
app.add_api("swagger.yaml", arguments={"title": "Hello World Example"})
if __name__ == "__main__":
app.run(f"{Path(__file__).stem}:app", port=8080)