Python aiohttp.web.RouteTableDef() Examples

The following are 5 code examples of aiohttp.web.RouteTableDef(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module aiohttp.web , or try the search function .
Example #1
Source File: test_docs_request_bodies.py    From aiohttp-swagger3 with Apache License 2.0 5 votes vote down vote up
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() == {} 
Example #2
Source File: test_docs_queries.py    From aiohttp-swagger3 with Apache License 2.0 5 votes vote down vote up
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 
Example #3
Source File: test_validation.py    From aiohttp-swagger3 with Apache License 2.0 4 votes vote down vote up
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."}},
            }
        }
    } 
Example #4
Source File: test_custom_keys.py    From aiohttp-swagger3 with Apache License 2.0 4 votes vote down vote up
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 
Example #5
Source File: server.py    From chia-blockchain with Apache License 2.0 4 votes vote down vote up
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