本文整理汇总了Python中aiohttp.web.RouteTableDef方法的典型用法代码示例。如果您正苦于以下问题:Python web.RouteTableDef方法的具体用法?Python web.RouteTableDef怎么用?Python web.RouteTableDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web
的用法示例。
在下文中一共展示了web.RouteTableDef方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_object_can_have_optional_props
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import RouteTableDef [as 别名]
def test_object_can_have_optional_props(swagger_docs, aiohttp_client):
routes = web.RouteTableDef()
@routes.post("/r")
async def handler(request, body: Dict):
"""
---
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
integer:
type: integer
string:
type: string
array:
type: array
items:
type: string
object:
type: object
responses:
'200':
description: OK.
"""
return web.json_response(body)
swagger = swagger_docs()
swagger.add_routes(routes)
client = await aiohttp_client(swagger._app)
resp = await client.post("/r", json={})
assert resp.status == 200
assert await resp.json() == {}
示例2: test_decorated_routes
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import RouteTableDef [as 别名]
def test_decorated_routes(swagger_docs, aiohttp_client):
routes = web.RouteTableDef()
@routes.get("/r")
async def handler(request, int32: int):
"""
---
parameters:
- name: int32
in: query
required: true
schema:
type: integer
responses:
'200':
description: OK.
"""
return web.json_response({"int32": int32})
swagger = swagger_docs()
swagger.add_routes(routes)
client = await aiohttp_client(swagger._app)
params = {"int32": 15}
resp = await client.get("/r", params=params)
assert resp.status == 200
assert await resp.json() == params
示例3: test_validation_false
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import RouteTableDef [as 别名]
def test_validation_false(swagger_docs, swagger_ui_settings, aiohttp_client):
routes = web.RouteTableDef()
@routes.post("/r")
async def handler(request):
"""
---
parameters:
- name: query
in: query
required: true
schema:
type: string
responses:
'200':
description: OK.
"""
assert "data" not in request
assert request.rel_url.query["query"] == "str"
return web.json_response()
swagger = swagger_docs(swagger_ui_settings=swagger_ui_settings(), validate=False)
swagger.add_routes(routes)
client = await aiohttp_client(swagger._app)
params = {"query": "str"}
resp = await client.post("/r", params=params)
assert resp.status == 200
resp = await client.get("/docs/")
assert resp.status == 200
resp = await client.get("/docs/swagger.json")
assert resp.status == 200
spec = await resp.json()
assert spec["paths"] == {
"/r": {
"post": {
"parameters": [
{
"name": "query",
"in": "query",
"required": True,
"schema": {"type": "string"},
"style": "form",
"explode": True,
}
],
"responses": {"200": {"description": "OK."}},
}
}
}
示例4: test_custom_request_key
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import RouteTableDef [as 别名]
def test_custom_request_key(swagger_docs, aiohttp_client):
routes = web.RouteTableDef()
@routes.post("/r/{path}")
async def handler(request, header: str, query: str, path: str, body: str):
"""
---
parameters:
- name: header
in: header
required: true
schema:
type: string
- name: query
in: query
required: true
schema:
type: string
- name: path
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: string
responses:
'200':
description: OK.
"""
assert "data" not in request
assert "test_key_321" in request
assert request["test_key_321"]["header"] == header
assert request["test_key_321"]["query"] == query
assert request["test_key_321"]["path"] == path
assert request["test_key_321"]["body"] == body
return web.json_response()
swagger = swagger_docs(request_key="test_key_321")
swagger.add_routes(routes)
client = await aiohttp_client(swagger._app)
params = {"query": "str"}
headers = {"header": "str"}
req = "str"
resp = await client.post("/r/str", headers=headers, params=params, json=req)
assert resp.status == 200
示例5: create_server_for_daemon
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import RouteTableDef [as 别名]
def create_server_for_daemon(root_path):
routes = web.RouteTableDef()
services: Dict = dict()
@routes.get("/daemon/ping/")
async def ping(request):
return web.Response(text="pong")
@routes.get("/daemon/service/start/")
async def start_service(request):
service_name = request.query.get("service")
if not validate_service(service_name):
r = "unknown service"
return web.Response(text=str(r))
if is_running(services, service_name):
r = "already running"
return web.Response(text=str(r))
try:
process, pid_path = launch_service(root_path, service_name)
services[service_name] = process
r = "started"
except (subprocess.SubprocessError, IOError):
log.exception(f"problem starting {service_name}")
r = "start failed"
return web.Response(text=str(r))
@routes.get("/daemon/service/stop/")
async def stop_service(request):
service_name = request.query.get("service")
r = await kill_service(root_path, services, service_name)
return web.Response(text=str(r))
@routes.get("/daemon/service/is_running/")
async def is_running_handler(request):
service_name = request.query.get("service")
r = is_running(services, service_name)
return web.Response(text=str(r))
@routes.get("/daemon/exit/")
async def exit(request):
jobs = []
for k in services.keys():
jobs.append(kill_service(root_path, services, k))
if jobs:
await asyncio.wait(jobs)
services.clear()
# we can't await `site.stop()` here because that will cause a deadlock, waiting for this
# request to exit