Add a command line tool to generate OAS in a file
This commit is contained in:
8
aiohttp_pydantic/oas/__main__.py
Normal file
8
aiohttp_pydantic/oas/__main__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
import argparse
|
||||
|
||||
from .cmd import setup
|
||||
|
||||
parser = argparse.ArgumentParser(description="Generate Open API Specification")
|
||||
setup(parser)
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
45
aiohttp_pydantic/oas/cmd.py
Normal file
45
aiohttp_pydantic/oas/cmd.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import argparse
|
||||
import importlib
|
||||
import json
|
||||
|
||||
from .view import generate_oas
|
||||
|
||||
|
||||
def application_type(value):
|
||||
"""
|
||||
Return aiohttp application defined in the value.
|
||||
"""
|
||||
try:
|
||||
module_name, app_name = value.split(":")
|
||||
except ValueError:
|
||||
module_name, app_name = value, "app"
|
||||
|
||||
module = importlib.import_module(module_name)
|
||||
try:
|
||||
if app_name.endswith("()"):
|
||||
app_name = app_name.strip("()")
|
||||
factory_app = getattr(module, app_name)
|
||||
return factory_app()
|
||||
return getattr(module, app_name)
|
||||
|
||||
except AttributeError as error:
|
||||
raise argparse.ArgumentTypeError(error) from error
|
||||
|
||||
|
||||
def setup(parser: argparse.ArgumentParser):
|
||||
parser.add_argument(
|
||||
"apps",
|
||||
metavar="APP",
|
||||
type=application_type,
|
||||
nargs="*",
|
||||
help="The name of the module containing the asyncio.web.Application."
|
||||
" By default the variable named 'app' is loaded but you can define"
|
||||
" an other variable name ending the name of module with : characters"
|
||||
" and the name of variable. Example: my_package.my_module:my_app",
|
||||
)
|
||||
|
||||
parser.set_defaults(func=show_oas)
|
||||
|
||||
|
||||
def show_oas(args: argparse.Namespace):
|
||||
print(json.dumps(generate_oas(args.apps), sort_keys=True, indent=4))
|
||||
@@ -1,7 +1,8 @@
|
||||
import typing
|
||||
from typing import Type
|
||||
from typing import List, Type
|
||||
|
||||
from aiohttp.web import Response, json_response
|
||||
from aiohttp.web_app import Application
|
||||
from pydantic import BaseModel
|
||||
|
||||
from aiohttp_pydantic.oas.struct import OpenApiSpec3, OperationObject, PathItem
|
||||
@@ -106,11 +107,10 @@ def _add_http_method_to_oas(
|
||||
_OASResponseBuilder(oas_operation).build(return_type)
|
||||
|
||||
|
||||
async def get_oas(request):
|
||||
def generate_oas(apps: List[Application]) -> dict:
|
||||
"""
|
||||
Generate Open Api Specification from PydanticView in application.
|
||||
Generate and return Open Api Specification from PydanticView in application.
|
||||
"""
|
||||
apps = request.app["apps to expose"]
|
||||
oas = OpenApiSpec3()
|
||||
for app in apps:
|
||||
for resources in app.router.resources():
|
||||
@@ -125,7 +125,15 @@ async def get_oas(request):
|
||||
else:
|
||||
_add_http_method_to_oas(path, resource_route.method, view)
|
||||
|
||||
return json_response(oas.spec)
|
||||
return oas.spec
|
||||
|
||||
|
||||
async def get_oas(request):
|
||||
"""
|
||||
View to generate the Open Api Specification from PydanticView in application.
|
||||
"""
|
||||
apps = request.app["apps to expose"]
|
||||
return json_response(generate_oas(apps))
|
||||
|
||||
|
||||
async def oas_ui(request):
|
||||
|
||||
@@ -9,8 +9,14 @@ from aiohttp.web_exceptions import HTTPMethodNotAllowed
|
||||
from aiohttp.web_response import StreamResponse
|
||||
from pydantic import ValidationError
|
||||
|
||||
from .injectors import (AbstractInjector, BodyGetter, HeadersGetter,
|
||||
MatchInfoGetter, QueryGetter, _parse_func_signature)
|
||||
from .injectors import (
|
||||
AbstractInjector,
|
||||
BodyGetter,
|
||||
HeadersGetter,
|
||||
MatchInfoGetter,
|
||||
QueryGetter,
|
||||
_parse_func_signature,
|
||||
)
|
||||
|
||||
|
||||
class PydanticView(AbstractView):
|
||||
|
||||
Reference in New Issue
Block a user