Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/connexion/apps/abstract.py: 70%
30 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 06:12 +0000
1"""
2This module defines an AbstractApp, which defines a standardized user interface for a Connexion
3application.
4"""
5import abc
6import pathlib
7import typing as t
9from starlette.testclient import TestClient
10from starlette.types import Receive, Scope, Send
12from connexion.jsonifier import Jsonifier
13from connexion.middleware import ConnexionMiddleware, SpecMiddleware
14from connexion.middleware.lifespan import Lifespan
15from connexion.resolver import Resolver
16from connexion.uri_parsing import AbstractURIParser
19class AbstractApp:
20 """
21 Abstract class for a Connexion Application. A Connexion Application provides an interface for a
22 framework application wrapped by Connexion Middleware. Since its main function is to provide an
23 interface, it delegates most of the work to the middleware and framework application.
24 """
26 middleware_app: SpecMiddleware
27 """
28 The application wrapped by the ConnexionMiddleware, which in its turn wraps the framework
29 application.
30 """
32 def __init__(
33 self,
34 import_name: str,
35 *,
36 lifespan: t.Optional[Lifespan] = None,
37 middlewares: t.Optional[list] = None,
38 specification_dir: t.Union[pathlib.Path, str] = "",
39 arguments: t.Optional[dict] = None,
40 auth_all_paths: t.Optional[bool] = None,
41 jsonifier: t.Optional[Jsonifier] = None,
42 pythonic_params: t.Optional[bool] = None,
43 resolver: t.Optional[t.Union[Resolver, t.Callable]] = None,
44 resolver_error: t.Optional[int] = None,
45 strict_validation: t.Optional[bool] = None,
46 swagger_ui_options: t.Optional[dict] = None,
47 uri_parser_class: t.Optional[AbstractURIParser] = None,
48 validate_responses: t.Optional[bool] = None,
49 validator_map: t.Optional[dict] = None,
50 ) -> None:
51 """
52 :param import_name: The name of the package or module that this object belongs to. If you
53 are using a single module, __name__ is always the correct value. If you however are
54 using a package, it’s usually recommended to hardcode the name of your package there.
55 :param middlewares: The list of middlewares to wrap around the application. Defaults to
56 :obj:`middleware.main.ConnexionmMiddleware.default_middlewares`
57 :param specification_dir: The directory holding the specification(s). The provided path
58 should either be absolute or relative to the root path of the application. Defaults to
59 the root path.
60 :param arguments: Arguments to substitute the specification using Jinja.
61 :param auth_all_paths: whether to authenticate not paths not defined in the specification.
62 Defaults to False.
63 :param jsonifier: Custom jsonifier to overwrite json encoding for json responses.
64 :param pythonic_params: When True, CamelCase parameters are converted to snake_case and an
65 underscore is appended to any shadowed built-ins. Defaults to False.
66 :param resolver: Callable that maps operationId to a function or instance of
67 :class:`resolver.Resolver`.
68 :param resolver_error: Error code to return for operations for which the operationId could
69 not be resolved. If no error code is provided, the application will fail when trying to
70 start.
71 :param strict_validation: When True, extra form or query parameters not defined in the
72 specification result in a validation error. Defaults to False.
73 :param swagger_ui_options: A :class:`options.ConnexionOptions` instance with configuration
74 options for the swagger ui.
75 :param uri_parser_class: Class to use for uri parsing. See :mod:`uri_parsing`.
76 :param validate_responses: Whether to validate responses against the specification. This has
77 an impact on performance. Defaults to False.
78 :param validator_map: A dictionary of validators to use. Defaults to
79 :obj:`validators.VALIDATOR_MAP`.
80 """
81 self.middleware = ConnexionMiddleware(
82 self.middleware_app,
83 import_name=import_name,
84 lifespan=lifespan,
85 middlewares=middlewares,
86 specification_dir=specification_dir,
87 arguments=arguments,
88 auth_all_paths=auth_all_paths,
89 jsonifier=jsonifier,
90 swagger_ui_options=swagger_ui_options,
91 pythonic_params=pythonic_params,
92 resolver=resolver,
93 resolver_error=resolver_error,
94 strict_validation=strict_validation,
95 uri_parser_class=uri_parser_class,
96 validate_responses=validate_responses,
97 validator_map=validator_map,
98 )
100 def add_api(
101 self,
102 specification: t.Union[pathlib.Path, str, dict],
103 *,
104 base_path: t.Optional[str] = None,
105 arguments: t.Optional[dict] = None,
106 auth_all_paths: t.Optional[bool] = None,
107 jsonifier: t.Optional[Jsonifier] = None,
108 pythonic_params: t.Optional[bool] = None,
109 resolver: t.Optional[t.Union[Resolver, t.Callable]] = None,
110 resolver_error: t.Optional[int] = None,
111 strict_validation: t.Optional[bool] = None,
112 swagger_ui_options: t.Optional[dict] = None,
113 uri_parser_class: t.Optional[AbstractURIParser] = None,
114 validate_responses: t.Optional[bool] = None,
115 validator_map: t.Optional[dict] = None,
116 **kwargs,
117 ) -> t.Any:
118 """
119 Register een API represented by a single OpenAPI specification on this application.
120 Multiple APIs can be registered on a single application.
122 :param specification: OpenAPI specification. Can be provided either as dict, or as path
123 to file.
124 :param base_path: Base path to host the API. This overrides the basePath / servers in the
125 specification.
126 :param arguments: Arguments to substitute the specification using Jinja.
127 :param auth_all_paths: whether to authenticate not paths not defined in the specification.
128 Defaults to False.
129 :param jsonifier: Custom jsonifier to overwrite json encoding for json responses.
130 :param pythonic_params: When True, CamelCase parameters are converted to snake_case and an
131 underscore is appended to any shadowed built-ins. Defaults to False.
132 :param resolver: Callable that maps operationId to a function or instance of
133 :class:`resolver.Resolver`.
134 :param resolver_error: Error code to return for operations for which the operationId could
135 not be resolved. If no error code is provided, the application will fail when trying to
136 start.
137 :param strict_validation: When True, extra form or query parameters not defined in the
138 specification result in a validation error. Defaults to False.
139 :param swagger_ui_options: A :class:`options.ConnexionOptions` instance with configuration
140 options for the swagger ui.
141 :param uri_parser_class: Class to use for uri parsing. See :mod:`uri_parsing`.
142 :param validate_responses: Whether to validate responses against the specification. This has
143 an impact on performance. Defaults to False.
144 :param validator_map: A dictionary of validators to use. Defaults to
145 :obj:`validators.VALIDATOR_MAP`
146 :param kwargs: Additional keyword arguments to pass to the `add_api` method of the managed
147 middlewares. This can be used to pass arguments to middlewares added beyond the default
148 ones.
150 :return: The Api registered on the middleware application wrapping the framework.
151 """
152 return self.middleware.add_api(
153 specification,
154 base_path=base_path,
155 arguments=arguments,
156 auth_all_paths=auth_all_paths,
157 jsonifier=jsonifier,
158 pythonic_params=pythonic_params,
159 resolver=resolver,
160 resolver_error=resolver_error,
161 strict_validation=strict_validation,
162 swagger_ui_options=swagger_ui_options,
163 uri_parser_class=uri_parser_class,
164 validate_responses=validate_responses,
165 validator_map=validator_map,
166 **kwargs,
167 )
169 def add_url_rule(
170 self, rule, endpoint: str = None, view_func: t.Callable = None, **options
171 ):
172 """
173 Connects a URL rule. Works exactly like the `route` decorator.
175 Basically this example::
177 @app.route('/')
178 def index():
179 pass
181 Is equivalent to the following::
183 def index():
184 pass
185 app.add_url_rule('/', 'index', index)
187 Internally`route` invokes `add_url_rule` so if you want to customize the behavior via
188 subclassing you only need to change this method.
190 :param rule: the URL rule as string.
191 :param endpoint: the name of the endpoint for the registered URL rule, which is used for
192 reverse lookup. Flask defaults to the name of the view function.
193 :param view_func: the function to call when serving a request to the provided endpoint.
194 :param options: the options to be forwarded to the underlying `werkzeug.routing.Rule`
195 object. A change to Werkzeug is handling of method options. methods is a list of
196 methods this rule should be limited to (`GET`, `POST` etc.). By default a rule just
197 listens for `GET` (and implicitly `HEAD`).
198 """
200 def route(self, rule: str, **options):
201 """
202 A decorator that is used to register a view function for a
203 given URL rule. This does the same thing as `add_url_rule`
204 but is intended for decorator usage::
206 @app.route('/')
207 def index():
208 return 'Hello World'
210 :param rule: the URL rule as string
211 :param options: the options to be forwarded to the underlying `werkzeug.routing.Rule`
212 object. A change to Werkzeug is handling of method options. methods is a
213 list of methods this rule should be limited to (`GET`, `POST` etc.).
214 By default a rule just listens for `GET` (and implicitly `HEAD`).
215 """
217 def decorator(func: t.Callable) -> t.Callable:
218 self.add_url_rule(rule, view_func=func, **options)
219 return func
221 return decorator
223 @abc.abstractmethod
224 def add_error_handler(
225 self, code_or_exception: t.Union[int, t.Type[Exception]], function: t.Callable
226 ) -> None:
227 """
228 Register a callable to handle application errors.
230 :param code_or_exception: An exception class or the status code of HTTP exceptions to
231 handle.
232 :param function: Callable that will handle exception.
233 """
235 def test_client(self, **kwargs):
236 """Creates a test client for this application."""
237 return TestClient(self, **kwargs)
239 def run(self, import_string: str = None, **kwargs):
240 """Run the application using uvicorn.
242 :param import_string: application as import string (eg. "main:app"). This is needed to run
243 using reload.
244 :param kwargs: kwargs to pass to `uvicorn.run`.
245 """
246 self.middleware.run(import_string, **kwargs)
248 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
249 return await self.middleware(scope, receive, send)