Skip to content

Commit

Permalink
Prefix and in_place param
Browse files Browse the repository at this point in the history
  • Loading branch information
maximdanilchenko committed Aug 12, 2019
1 parent 96ee443 commit 5aec5e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions aiohttp_apispec/aiohttp_apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def __init__(
swagger_path=None,
static_path='/static/swagger',
error_callback=None,
in_place=False,
prefix='',
**kwargs
):

Expand All @@ -38,25 +40,30 @@ def __init__(
self._registered = False
self._request_data_name = request_data_name
self.error_callback = error_callback
self.prefix = prefix
if app is not None:
self.register(app)
self.register(app, in_place)

def swagger_dict(self):
return self.spec.to_dict()

def register(self, app: web.Application):
def register(self, app: web.Application, in_place: bool = False):
if self._registered is True:
return None

app["_apispec_request_data_name"] = self._request_data_name

if self.error_callback:
parser.error_callback = self.error_callback
app["_apispec_parser"] = parser

async def doc_routes(app_):
self._register(app_)
if in_place:
self._register(app)
else:
async def doc_routes(app_):
self._register(app_)
app.on_startup.append(doc_routes)

app.on_startup.append(doc_routes)
self._registered = True

async def swagger_handler(request):
Expand Down Expand Up @@ -106,7 +113,7 @@ def _register_route(
if not url_path:
return None

self._update_paths(view.__apispec__, method, url_path)
self._update_paths(view.__apispec__, method, self.prefix + url_path)

def _update_paths(self, data: dict, method: str, url_path: str):
if method not in VALID_METHODS_OPENAPI_V2:
Expand Down Expand Up @@ -159,6 +166,8 @@ def setup_aiohttp_apispec(
swagger_path: str = None,
static_path: str = '/static/swagger',
error_callback=None,
in_place: bool = False,
prefix: str = '',
**kwargs
) -> None:
"""
Expand Down Expand Up @@ -211,6 +220,10 @@ async def index(request):
:param str static_path: path for static files used by SwaggerUI
(if it is enabled with ``swagger_path``)
:param error_callback: custom error handler
:param in_place: register all routes at the moment of calling this function
instead of the moment of the on_startup signal.
If True, be sure all routes are added to router
:param prefix: prefix to add to all registered routes
:param kwargs: any apispec.APISpec kwargs
"""
AiohttpApiSpec(
Expand All @@ -222,5 +235,7 @@ async def index(request):
swagger_path=swagger_path,
static_path=static_path,
error_callback=error_callback,
in_place=in_place,
prefix=prefix,
**kwargs
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def read(file_name):

setup(
name='aiohttp-apispec',
version='1.3.1',
version='1.4.0rc',
description='Build and document REST APIs with aiohttp and apispec',
long_description=read('README.md'),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 5aec5e1

Please sign in to comment.