From 2efa03450e7965afce2767436e9a701dff466d79 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Thu, 30 Mar 2017 23:26:26 +0200 Subject: [PATCH] Update api code --- hassio_api/hassio/api/__init__.py | 1 + hassio_api/hassio/api/homeassistant.py | 8 +++--- hassio_api/hassio/api/host.py | 39 +++++++++++++++++++------- hassio_api/hassio/api/supervisor.py | 13 +++++---- hassio_api/hassio/api/util.py | 30 ++++++++++++++++++++ hassio_api/hassio/const.py | 9 ++++++ 6 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 hassio_api/hassio/api/util.py diff --git a/hassio_api/hassio/api/__init__.py b/hassio_api/hassio/api/__init__.py index b3fe14dfc..0191f300b 100644 --- a/hassio_api/hassio/api/__init__.py +++ b/hassio_api/hassio/api/__init__.py @@ -19,6 +19,7 @@ class RestAPI(object): self.loop = loop self.webapp = web.Application(loop=self.loop) + # service stuff self._handler = None self.server = None diff --git a/hassio_api/hassio/api/homeassistant.py b/hassio_api/hassio/api/homeassistant.py index 560628d50..627de3a4e 100644 --- a/hassio_api/hassio/api/homeassistant.py +++ b/hassio_api/hassio/api/homeassistant.py @@ -1,9 +1,9 @@ """Init file for HassIO homeassistant rest api.""" import logging -from aiohttp import web -from aiohttp.web_exceptions import HTTPNotAcceptable +from aiohttp.web_exceptions import HTTPServiceUnavailable +from .util import api_return_ok from ..const import ATTR_VERSION _LOGGER = logging.getLogger(__name__) @@ -20,10 +20,10 @@ class APIHomeAssistant(object): async def info(self, request): """Return host information.""" - return web.json_response({ + return api_return_ok({ ATTR_VERSION: self.dock_hass.version, }) async def update(self, request): """Update host OS.""" - raise HTTPNotAcceptable() + raise HTTPServiceUnavailable() diff --git a/hassio_api/hassio/api/host.py b/hassio_api/hassio/api/host.py index 0f7926932..e2fdbbb03 100644 --- a/hassio_api/hassio/api/host.py +++ b/hassio_api/hassio/api/host.py @@ -2,8 +2,9 @@ import logging from aiohttp import web -from aiohttp.web_exceptions import HTTPOk, HTTPNotAcceptable +from aiohttp.web_exceptions import HTTPServiceUnavailable +from .util import api_return_ok, api_return_not_supported from ..const import ATTR_VERSION _LOGGER = logging.getLogger(__name__) @@ -20,34 +21,52 @@ class APIHost(object): async def info(self, request): """Return host information.""" + if not self.host_controll.active: + raise HTTPServiceUnavailable() + host_info = await self.host_controll.info() if host_info: return web.json_response(host_info) - raise HTTPNotAcceptable() + return api_return_not_supported() async def reboot(self, request): """Reboot host.""" + if not self.host_controll.active: + raise HTTPServiceUnavailable() + if await self.host_controll.reboot(): - raise HTTPOk() - raise HTTPNotAcceptable() + return api_return_ok() + return api_return_not_supported() async def shutdown(self, request): """Poweroff host.""" + if not self.host_controll.active: + raise HTTPServiceUnavailable() + if await self.host_controll.shutdown(): - raise HTTPOk() - raise HTTPNotAcceptable() + return api_return_ok() + return api_return_not_supported() async def network_info(self, request): """Edit network settings.""" - raise HTTPNotAcceptable() + if not self.host_controll.active: + raise HTTPServiceUnavailable() + + return api_return_not_supported() async def network_update(self, request): """Edit network settings.""" - raise HTTPNotAcceptable() + if not self.host_controll.active: + raise HTTPServiceUnavailable() + + return api_return_not_supported() async def update(self, request): """Update host OS.""" + if not self.host_controll.active: + raise HTTPServiceUnavailable() + body = await request.json() or {} if await self.host_controll.host_update(body.get(ATTR_VERSION)): - raise HTTPOk() - raise HTTPNotAcceptable() + return api_return_ok() + return api_return_not_supported() diff --git a/hassio_api/hassio/api/supervisor.py b/hassio_api/hassio/api/supervisor.py index a4f7cf85a..c03eb59af 100644 --- a/hassio_api/hassio/api/supervisor.py +++ b/hassio_api/hassio/api/supervisor.py @@ -1,9 +1,9 @@ """Init file for HassIO supervisor rest api.""" import logging -from aiohttp import web -from aiohttp.web_exceptions import HTTPOk, HTTPNotAcceptable +from aiohttp.web_exceptions import HTTPServiceUnavailable +from .util import api_return_ok, api_return_not_supported from ..const import ATTR_VERSION, HASSIO_VERSION _LOGGER = logging.getLogger(__name__) @@ -20,13 +20,16 @@ class APISupervisor(object): async def info(self, request): """Return host information.""" - return web.json_response({ + return api_return_ok({ ATTR_VERSION: HASSIO_VERSION, }) async def update(self, request): """Update host OS.""" + if not self.host_controll.active: + raise HTTPServiceUnavailable() + body = await request.json() or {} if await self.host_controll.supervisor_update(body.get(ATTR_VERSION)): - raise HTTPOk() - raise HTTPNotAcceptable() + return api_return_ok() + return api_return_not_supported() diff --git a/hassio_api/hassio/api/util.py b/hassio_api/hassio/api/util.py new file mode 100644 index 000000000..d530269c0 --- /dev/null +++ b/hassio_api/hassio/api/util.py @@ -0,0 +1,30 @@ +"""Init file for HassIO util for rest api.""" +import logging + +from aiohttp import web + +from ..const import ( + JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR) + +_LOGGER = logging.getLogger(__name__) + + +def api_return_error(message=None): + """Return a API error message.""" + return web.json_response({ + JSON_RESULT: RESULT_ERROR, + JSON_MESSAGE: message, + }) + + +def api_return_ok(data=None): + """Return a API ok answer.""" + return web.json_response({ + JSON_RESULT: RESULT_OK, + JSON_DATA: data, + }) + + +def api_return_not_supported(): + """Return a api error with not supported.""" + return api_return_error("Function is not supported") diff --git a/hassio_api/hassio/const.py b/hassio_api/hassio/const.py index 0748f7973..573a3f645 100644 --- a/hassio_api/hassio/const.py +++ b/hassio_api/hassio/const.py @@ -24,4 +24,13 @@ HTTP_PORT = 9123 HOMEASSISTANT_IMAGE = 'homeassistant_image' HOMEASSISTANT_TAG = 'homeassistant_tag' +JSON_RESULT = 'result' +JSON_DATA = 'data' +JSON_MESSAGE = 'message' + +RESULT_ERROR = 'error' +RESULT_OK = 'ok' + ATTR_VERSION = 'version' +ATTR_NEED_UPDATE = 'need_update' +ATTR_NEXT_VERSION = 'next_version'