Repository: Zapix/mtpylon (language: python)

Issue:
Implement view to return schema
View to return schema as json object. 


Discussion / hints:

Fix patch:
diff --git a/mtpylon/aiohandlers/schema.py b/mtpylon/aiohandlers/schema.py
new file mode 100644
index 0000000..d0eff61
--- /dev/null
+++ b/mtpylon/aiohandlers/schema.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+from functools import partial
+
+from aiohttp.web import Request, Response
+from aiohttp.web_response import json_response
+
+from .types import RequestHandler
+from ..schema import Schema
+from ..serializers import to_dict
+
+
+async def schema_view(request: Request, schema: Schema) -> Response:
+    """
+    Returns schema as dumped json
+    """
+    return json_response(
+        to_dict(schema),
+        status=200,
+    )
+
+
+def schema_view_factory(schema: Schema) -> RequestHandler:
+    return partial(schema_view, schema=schema)
diff --git a/mtpylon/aiohandlers/types.py b/mtpylon/aiohandlers/types.py
new file mode 100644
index 0000000..f3b0e3e
--- /dev/null
+++ b/mtpylon/aiohandlers/types.py
@@ -0,0 +1,8 @@
+# -*- coding: utf-8 -*-
+from typing import Callable, Awaitable
+
+from aiohttp.web import Response, Request, WebSocketResponse
+
+RequestHandler = Callable[[Request], Awaitable[Response]]
+
+WebSocketHandler = Callable[[Request], Awaitable[WebSocketResponse]]
diff --git a/mtpylon/aiohandlers/websockets.py b/mtpylon/aiohandlers/websockets.py
index d380ea9..92fb623 100644
--- a/mtpylon/aiohandlers/websockets.py
+++ b/mtpylon/aiohandlers/websockets.py
@@ -1,12 +1,13 @@
 # -*- coding: utf-8 -*-
 import logging
 from functools import partial
-from typing import cast, Optional, Callable, Awaitable
+from typing import cast, Optional
 from asyncio import create_task
 
 from aiohttp import WSMsgType
 from aiohttp.web import Request, WebSocketResponse
 
+from mtpylon.aiohandlers.types import WebSocketHandler
 from mtpylon.schema import Schema
 from mtpylon.transports import (
     parse_header,
@@ -18,8 +19,6 @@
 
 logger = logging.getLogger(__name__)
 
-WebSocketHandler = Callable[[Request], Awaitable[WebSocketResponse]]
-
 
 async def ws_handler(request: Request, schema: Schema) -> WebSocketResponse:
     """
diff --git a/requirements.txt b/requirements.txt
index f260a0c..29fd590 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -12,6 +12,7 @@ pyflakes==2.2.0
 pyparsing==2.4.7
 pytest==6.1.1
 pytest-asyncio==0.14.0
+pytest-aiohttp==0.3.0
 pytest-flake8==1.0.6
 six==1.15.0
 toml==0.10.1


Test patch:
diff --git a/tests/test_aiohandlers/test_schema.py b/tests/test_aiohandlers/test_schema.py
new file mode 100644
index 0000000..b95f8db
--- /dev/null
+++ b/tests/test_aiohandlers/test_schema.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+import pytest
+from aiohttp.web import Application
+
+from mtpylon.aiohandlers.schema import schema_view_factory
+
+from tests.simpleschema import schema
+from tests.test_serializers import json_schema
+
+
+@pytest.fixture
+def cli(loop, aiohttp_client):
+    schema_view = schema_view_factory(schema)
+
+    app = Application()
+    app.router.add_get('/schema', schema_view)
+
+    return loop.run_until_complete(aiohttp_client(app))
+
+
+async def test_get_schema(cli):
+    resp = await cli.get('/schema')
+
+    assert resp.status == 200
+
+    schema_data = await resp.json()
+
+    assert schema_data == json_schema