Commit bb9dc935 authored by cr0hn's avatar cr0hn Committed by GitHub
Browse files

Merge pull request #14 from timofurrer/feature/backport-py3.4

parents 8e5ad3ce 55d662dd
......@@ -8,12 +8,12 @@ aiohttp-swagger
Code | https://github.com/cr0hn/aiohttp-swagger
---- | ----------------------------------------------
Issues | https://github.com/cr0hn/aiohttp-swagger/issues/
Python version | Python 3.5 and above
Python version | Python 3.4 and above
What's aiohttp-swagger
----------------------
aiohttp-swagger is a plugin for aiohttp.web server that allow to document APIs using Swagger show the Swagger-ui console.
aiohttp-swagger is a plugin for aiohttp.web server that allow to document APIs using Swagger show the Swagger-ui console.
Installation
------------
......@@ -36,7 +36,7 @@ To install the tool with extra performance you must do:
$ python3.5 -m pip install 'aiohttp-swagger[performance]'
```
**Remember that aiohttp-swagger only runs in Python 3.5 and above**.
**Remember that aiohttp-swagger only runs in Python 3.4 and above**.
Quick start
-----------
......@@ -272,7 +272,7 @@ async def ping(request):
>>> import json
>>> ping(None)
:param request: Context injected by aiohttp framework
:type request: RequestHandler
"""
......@@ -301,13 +301,13 @@ F.A.Q.
async def ping(request):
"""
This is my usually Sphinx doc
>>> import json
>>> ping(None)
:param request: Context injected by aiohttp framework
:type request: RequestHandler
:param request: Context injected by aiohttp framework
:type request: RequestHandler
---
description: This end-point allow to test that service is up.
tags:
......@@ -329,7 +329,7 @@ async def ping(request):
- Q: How can I change the Title of a group of End-Points?
- A: Swagger has a tag that uses to build the titles. The tag name is **tags**. The format is:
```yaml
tags: # <-- TAG USEF FOR THE TITLE
- Health check
......@@ -352,7 +352,7 @@ Swagger Documentation & Examples
You can read the Swagger parameters and format at:
[OpenAPI Spec](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)
If you need more examples, there're some available in at folder: [aiohttp_swagger/examples](https://github.com/cr0hn/aiohttp-swagger/tree/master/examples).
What's new?
......
import asyncio
from os.path import join, dirname, abspath
from aiohttp import web
......@@ -5,14 +6,16 @@ from aiohttp import web
from .helpers import swagger_path, generate_doc_from_each_end_point, load_doc_from_yaml_file
async def _swagger_home(request):
@asyncio.coroutine
def _swagger_home(request):
"""
Return the index.html main file
"""
return web.Response(text=request.app["SWAGGER_TEMPLATE_CONTENT"], content_type="text/html")
async def _swagger_def(request):
@asyncio.coroutine
def _swagger_def(request):
"""
Returns the Swagger JSON Definition
"""
......@@ -33,7 +36,7 @@ def setup_swagger(app: web.Application,
contact: str = ""):
_swagger_url = "/{}".format(swagger_url) if not swagger_url.startswith("/") else swagger_url
_swagger_def_url = '{}/swagger.json'.format(_swagger_url)
# Build Swagget Info
if swagger_from_file:
swagger_info = load_doc_from_yaml_file(swagger_from_file)
......@@ -44,16 +47,16 @@ def setup_swagger(app: web.Application,
api_version=api_version,
title=title,
contact=contact)
# Add API routes
app.router.add_route('GET', _swagger_url, _swagger_home)
app.router.add_route('GET', "{}/".format(_swagger_url), _swagger_home)
app.router.add_route('GET', _swagger_def_url, _swagger_def)
# Set statics
statics_path = '{}/swagger_static'.format(_swagger_url)
app.router.add_static(statics_path, STATIC_PATH)
# --------------------------------------------------------------------------
# Build templates
# --------------------------------------------------------------------------
......
import asyncio
import json
from os.path import join, dirname, abspath
from aiohttp import web
from aiohttp_swagger import *
from os.path import join, dirname, abspath
async def ping(request):
@asyncio.coroutine
def ping(request):
"""
---
description: This end-point allow to test that service is up.
......@@ -22,33 +24,38 @@ async def ping(request):
return web.Response(text="pong")
async def test_ping(test_client, loop):
@asyncio.coroutine
def test_ping(test_client, loop):
app = web.Application(loop=loop)
app.router.add_route('GET', "/ping", ping)
client = await test_client(app)
resp = await client.get('/ping')
client = yield from test_client(app)
resp = yield from client.get('/ping')
assert resp.status == 200
text = await resp.text()
text = yield from resp.text()
assert 'pong' in text
async def test_swagger_file_url(test_client, loop):
@asyncio.coroutine
def test_swagger_file_url(test_client, loop):
TESTS_PATH = abspath(join(dirname(__file__)))
app = web.Application(loop=loop)
setup_swagger(app,
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml")
client = await test_client(app)
resp1 = await client.get('/api/doc/swagger.json')
client = yield from test_client(app)
resp1 = yield from client.get('/api/doc/swagger.json')
assert resp1.status == 200
text = await resp1.text()
text = yield from resp1.text()
result = json.loads(text)
assert '/example1' in result['paths']
assert '/example2' in result['paths']
assert 'API Title' in result['info']['title']
async def test_custom_swagger(test_client, loop):
@asyncio.coroutine
def test_custom_swagger(test_client, loop):
app = web.Application(loop=loop)
app.router.add_route('GET', "/ping", ping)
description = "Test Custom Swagger"
......@@ -59,10 +66,10 @@ async def test_custom_swagger(test_client, loop):
api_version="1.0.0",
contact="my.custom.contact@example.com")
client = await test_client(app)
resp1 = await client.get('/api/v1/doc/swagger.json')
client = yield from test_client(app)
resp1 = yield from client.get('/api/v1/doc/swagger.json')
assert resp1.status == 200
text = await resp1.text()
text = yield from resp1.text()
result = json.loads(text)
assert '/ping' in result['paths']
assert 'Test Custom Title' in result['info']['title']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment