Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Jan 22, 2023
1 parent 0bed769 commit 2199c02
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
68 changes: 66 additions & 2 deletions connexion/apps/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import pathlib
import typing as t

from starlette.responses import Response as StarletteResponse
Expand All @@ -17,6 +18,7 @@
RoutedMiddleware,
)
from connexion.operations import AbstractOperation
from connexion.resolver import Resolver
from connexion.uri_parsing import AbstractURIParser

logger = logging.getLogger("Connexion.app")
Expand Down Expand Up @@ -96,7 +98,7 @@ def add_url_rule(
endpoint: str = None,
view_func: t.Callable = None,
methods: t.List[str] = None,
**options
**options,
):
self.router.add_route(rule, endpoint=view_func, name=endpoint, methods=methods)

Expand All @@ -105,7 +107,69 @@ class AsyncApp(AbstractApp):
"""Connexion Application based on ConnexionMiddleware wrapping a async Connexion application
based on starlette tools."""

middleware_app = AsyncMiddlewareApp()
def __init__(
self,
import_name: str,
*,
specification_dir: t.Union[pathlib.Path, str] = "",
middlewares: t.Optional[list] = None,
arguments: t.Optional[dict] = None,
auth_all_paths: t.Optional[bool] = None,
pythonic_params: t.Optional[bool] = None,
resolver: t.Optional[t.Union[Resolver, t.Callable]] = None,
resolver_error: t.Optional[int] = None,
strict_validation: t.Optional[bool] = None,
swagger_ui_options: t.Optional[dict] = None,
uri_parser_class: t.Optional[AbstractURIParser] = None,
validate_responses: t.Optional[bool] = None,
validator_map: t.Optional[dict] = None,
) -> None:
"""
:param import_name: The name of the package or module that this object belongs to. If you
are using a single module, __name__ is always the correct value. If you however are
using a package, it’s usually recommended to hardcode the name of your package there.
:param specification_dir: The directory holding the specification(s). The provided path
should either be absolute or relative to the root path of the application. Defaults to
the root path.
:param middlewares: The list of middlewares to wrap around the application. Defaults to
:obj:`middleware.main.ConnexionmMiddleware.default_middlewares`
:param arguments: Arguments to substitute the specification using Jinja.
:param auth_all_paths: whether to authenticate not paths not defined in the specification.
Defaults to False.
:param pythonic_params: When True, CamelCase parameters are converted to snake_case and an
underscore is appended to any shadowed built-ins. Defaults to False.
:param resolver: Callable that maps operationId to a function or instance of
:class:`resolver.Resolver`.
:param resolver_error: Error code to return for operations for which the operationId could
not be resolved. If no error code is provided, the application will fail when trying to
start.
:param strict_validation: When True, extra form or query parameters not defined in the
specification result in a validation error. Defaults to False.
:param swagger_ui_options: A :class:`options.ConnexionOptions` instance with configuration
options for the swagger ui.
:param uri_parser_class: Class to use for uri parsing. See :mod:`uri_parsing`.
:param validate_responses: Whether to validate responses against the specification. This has
an impact on performance. Defaults to False.
:param validator_map: A dictionary of validators to use. Defaults to
:obj:`validators.VALIDATOR_MAP`.
"""
self.middleware_app: AsyncMiddlewareApp = AsyncMiddlewareApp()

super().__init__(
import_name,
specification_dir=specification_dir,
middlewares=middlewares,
arguments=arguments,
auth_all_paths=auth_all_paths,
swagger_ui_options=swagger_ui_options,
pythonic_params=pythonic_params,
resolver=resolver,
resolver_error=resolver_error,
strict_validation=strict_validation,
uri_parser_class=uri_parser_class,
validate_responses=validate_responses,
validator_map=validator_map,
)

def add_url_rule(
self, rule, endpoint: str = None, view_func: t.Callable = None, **options
Expand Down
3 changes: 2 additions & 1 deletion connexion/middleware/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def add_api(
self, specification: t.Union[pathlib.Path, str, dict], **kwargs
) -> None:
"""
Register een API represented by a single OpenAPI specification on this middleware.
Register an API represented by a single OpenAPI specification on this middleware.
Multiple APIs can be registered on a single middleware.
"""

Expand Down Expand Up @@ -68,6 +68,7 @@ def _set_base_path(self, base_path: t.Optional[str] = None) -> None:


OP = t.TypeVar("OP")
""""""


class AbstractRoutingAPI(AbstractSpecAPI, t.Generic[OP]):
Expand Down
2 changes: 1 addition & 1 deletion connexion/middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def __init__(
self.extra_files: t.List[str] = []

def ensure_absolute(self, path: t.Union[str, pathlib.Path]) -> pathlib.Path:
"""Ensure that a path is absolute. If the path is not resolute, it is assumed to relative
"""Ensure that a path is absolute. If the path is not absolute, it is assumed to relative
to the application root path and made absolute."""
path = pathlib.Path(path)
if path.is_absolute():
Expand Down
5 changes: 3 additions & 2 deletions examples/frameworks/hello_quart.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import typing as t
from pathlib import Path

import connexion
from connexion.decorators import ASGIDecorator
from connexion.resolver import RelativeResolver
from quart import Quart, json, request
from quart import Quart

app = Quart(__name__)


@app.route("/openapi/greeting/<name>", methods=["POST"])
@app.route("/swagger/greeting/<name>", methods=["POST"])
@ASGIDecorator()
def post_greeting(name: str, number: int = None) -> str:
def post_greeting(name: str, number: t.Optional[int] = None) -> str:
return f"Hello {name}, your number is {number}!"


Expand Down
3 changes: 2 additions & 1 deletion examples/frameworks/hello_starlette.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing as t
from pathlib import Path

import connexion
Expand All @@ -8,7 +9,7 @@


@StarletteDecorator()
def post_greeting(name: str, number: int = None) -> str:
def post_greeting(name: str, number: t.Optional[int] = None) -> str:
return f"Hello {name}, your number is {number}!"


Expand Down

0 comments on commit 2199c02

Please sign in to comment.