refactoring

This commit is contained in:
Devaev Maxim 2020-05-17 16:11:49 +03:00
parent 1fd33bc8ed
commit 8a13f62911
5 changed files with 31 additions and 26 deletions

View File

@ -20,7 +20,8 @@
# ========================================================================== #
import aiohttp.web
from aiohttp.web import Request
from aiohttp.web import Response
from ....plugins.atx import BaseAtx
@ -39,11 +40,11 @@ class AtxApi:
# =====
@exposed_http("GET", "/atx")
async def __state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __state_handler(self, _: Request) -> Response:
return make_json_response(self.__atx.get_state())
@exposed_http("POST", "/atx/power")
async def __power_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
async def __power_handler(self, request: Request) -> Response:
action = valid_atx_power_action(request.query.get("action"))
processing = await ({
"on": self.__atx.power_on,
@ -54,7 +55,7 @@ class AtxApi:
return make_json_response({"processing": processing})
@exposed_http("POST", "/atx/click")
async def __click_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
async def __click_handler(self, request: Request) -> Response:
button = valid_atx_button(request.query.get("button"))
await ({
"power": self.__atx.click_power,

View File

@ -22,7 +22,9 @@
from typing import Dict
import aiohttp.web
from aiohttp.web import Request
from aiohttp.web import Response
from aiohttp.web import WebSocketResponse
from ....plugins.hid import BaseHid
@ -46,18 +48,18 @@ class HidApi:
# =====
@exposed_http("GET", "/hid")
async def __state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __state_handler(self, _: Request) -> Response:
return make_json_response(await self.__hid.get_state())
@exposed_http("POST", "/hid/reset")
async def __reset_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __reset_handler(self, _: Request) -> Response:
await self.__hid.reset()
return make_json_response()
# =====
@exposed_ws("key")
async def __ws_key_handler(self, _: aiohttp.web.WebSocketResponse, event: Dict) -> None:
async def __ws_key_handler(self, _: WebSocketResponse, event: Dict) -> None:
try:
key = valid_hid_key(event["key"])
state = valid_bool(event["state"])
@ -66,7 +68,7 @@ class HidApi:
await self.__hid.send_key_event(key, state)
@exposed_ws("mouse_button")
async def __ws_mouse_button_handler(self, _: aiohttp.web.WebSocketResponse, event: Dict) -> None:
async def __ws_mouse_button_handler(self, _: WebSocketResponse, event: Dict) -> None:
try:
button = valid_hid_mouse_button(event["button"])
state = valid_bool(event["state"])
@ -75,7 +77,7 @@ class HidApi:
await self.__hid.send_mouse_button_event(button, state)
@exposed_ws("mouse_move")
async def __ws_mouse_move_handler(self, _: aiohttp.web.WebSocketResponse, event: Dict) -> None:
async def __ws_mouse_move_handler(self, _: WebSocketResponse, event: Dict) -> None:
try:
to_x = valid_hid_mouse_move(event["to"]["x"])
to_y = valid_hid_mouse_move(event["to"]["y"])
@ -84,7 +86,7 @@ class HidApi:
await self.__hid.send_mouse_move_event(to_x, to_y)
@exposed_ws("mouse_wheel")
async def __ws_mouse_wheel_handler(self, _: aiohttp.web.WebSocketResponse, event: Dict) -> None:
async def __ws_mouse_wheel_handler(self, _: WebSocketResponse, event: Dict) -> None:
try:
delta_x = valid_hid_mouse_wheel(event["delta"]["x"])
delta_y = valid_hid_mouse_wheel(event["delta"]["y"])

View File

@ -20,7 +20,8 @@
# ========================================================================== #
import aiohttp.web
from aiohttp.web import Request
from aiohttp.web import StreamResponse
from ....validators.basic import valid_bool
@ -39,11 +40,11 @@ class LogApi:
# =====
@exposed_http("GET", "/log")
async def __log_handler(self, request: aiohttp.web.Request) -> aiohttp.web.StreamResponse:
async def __log_handler(self, request: Request) -> StreamResponse:
seek = valid_log_seek(request.query.get("seek", "0"))
follow = valid_bool(request.query.get("follow", "false"))
response = aiohttp.web.StreamResponse(status=200, reason="OK", headers={"Content-Type": "text/plain"})
response = StreamResponse(status=200, reason="OK", headers={"Content-Type": "text/plain"})
await response.prepare(request)
async for record in self.__log_reader.poll_log(seek, follow):

View File

@ -20,8 +20,8 @@
# ========================================================================== #
import aiohttp
import aiohttp.web
from aiohttp.web import Request
from aiohttp.web import Response
from ....logging import get_logger
@ -45,11 +45,11 @@ class MsdApi:
# =====
@exposed_http("GET", "/msd")
async def __state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __state_handler(self, _: Request) -> Response:
return make_json_response(await self.__msd.get_state())
@exposed_http("POST", "/msd/set_params")
async def __set_params_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
async def __set_params_handler(self, request: Request) -> Response:
params = {
key: validator(request.query.get(param))
for (param, key, validator) in [
@ -62,17 +62,17 @@ class MsdApi:
return make_json_response()
@exposed_http("POST", "/msd/connect")
async def __connect_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __connect_handler(self, _: Request) -> Response:
await self.__msd.connect()
return make_json_response()
@exposed_http("POST", "/msd/disconnect")
async def __disconnect_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __disconnect_handler(self, _: Request) -> Response:
await self.__msd.disconnect()
return make_json_response()
@exposed_http("POST", "/msd/write")
async def __write_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
async def __write_handler(self, request: Request) -> Response:
logger = get_logger(0)
reader = await request.multipart()
name = ""
@ -96,11 +96,11 @@ class MsdApi:
return make_json_response({"image": {"name": name, "size": written}})
@exposed_http("POST", "/msd/remove")
async def __remove_handler(self, request: aiohttp.web.Request) -> aiohttp.web.Response:
async def __remove_handler(self, request: Request) -> Response:
await self.__msd.remove(valid_msd_image_name(request.query.get("image")))
return make_json_response()
@exposed_http("POST", "/msd/reset")
async def __reset_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __reset_handler(self, _: Request) -> Response:
await self.__msd.reset()
return make_json_response()

View File

@ -20,7 +20,8 @@
# ========================================================================== #
import aiohttp.web
from aiohttp.web import Request
from aiohttp.web import Response
from ..wol import WakeOnLan
@ -36,10 +37,10 @@ class WolApi:
# =====
@exposed_http("GET", "/wol")
async def __wol_state_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __wol_state_handler(self, _: Request) -> Response:
return make_json_response(self.__wol.get_state())
@exposed_http("POST", "/wol/wakeup")
async def __wol_wakeup_handler(self, _: aiohttp.web.Request) -> aiohttp.web.Response:
async def __wol_wakeup_handler(self, _: Request) -> Response:
await self.__wol.wakeup()
return make_json_response()