mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-06-26 20:06:32 +00:00

* Add API layout for snapshot * Update api * Add support for export/import docker images * Move restore into addon * Add restore to addon * Fix lint * fix lint * cleanup * init object * fix executor * cleanup * Change flow of init * Revert "Change flow of init" This reverts commit 6b3215e44c990d16f728f766df52b20c091de807. * allow restore from none * forward working * add size * add context for snapshot * add init function to set meta data * update local addon on load * add more validate and optimaze code * Optimaze code for restore data * add validate layer * Add more function to snapshot / cleanup others * finish snapshot function * Cleanup config / optimaze code * Finish snapshot on core * Some improvments first object for api * finish * fix lint p1 * fix lint p2 * fix lint p3 * fix async with * fix lint p4 * fix lint p5 * fix p6 * make staticmethod * fix schema * fix parse system data * fix bugs * fix get function * extend snapshot/restore * add type * fix lint * move to gz / xz is to slow * move to gz / xz is to slow p2 * Fix config folder * small compresslevel for more speed * fix lint * fix load * fix tar stream * fix tar stream p2 * fix parse * fix partial * fix start hass * fix rep * fix set * fix real * fix generator * Cleanup old image * add log * fix lint * fix lint p2 * fix load from tar
116 lines
3.1 KiB
Python
116 lines
3.1 KiB
Python
"""Init file for HassIO util for rest api."""
|
|
import json
|
|
import hashlib
|
|
import logging
|
|
|
|
from aiohttp import web
|
|
from aiohttp.web_exceptions import HTTPServiceUnavailable
|
|
import voluptuous as vol
|
|
from voluptuous.humanize import humanize_error
|
|
|
|
from ..const import (
|
|
JSON_RESULT, JSON_DATA, JSON_MESSAGE, RESULT_OK, RESULT_ERROR)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
def json_loads(data):
|
|
"""Extract json from string with support for '' and None."""
|
|
try:
|
|
return json.loads(data)
|
|
except json.JSONDecodeError:
|
|
return {}
|
|
|
|
|
|
def api_process(method):
|
|
"""Wrap function with true/false calls to rest api."""
|
|
async def wrap_api(api, *args, **kwargs):
|
|
"""Return api information."""
|
|
try:
|
|
answer = await method(api, *args, **kwargs)
|
|
except RuntimeError as err:
|
|
return api_return_error(message=str(err))
|
|
|
|
if isinstance(answer, dict):
|
|
return api_return_ok(data=answer)
|
|
if isinstance(answer, web.Response):
|
|
return answer
|
|
elif answer:
|
|
return api_return_ok()
|
|
return api_return_error()
|
|
|
|
return wrap_api
|
|
|
|
|
|
def api_process_hostcontrol(method):
|
|
"""Wrap HostControl calls to rest api."""
|
|
async def wrap_hostcontrol(api, *args, **kwargs):
|
|
"""Return host information."""
|
|
if not api.host_control.active:
|
|
raise HTTPServiceUnavailable()
|
|
|
|
try:
|
|
answer = await method(api, *args, **kwargs)
|
|
except RuntimeError as err:
|
|
return api_return_error(message=str(err))
|
|
|
|
if isinstance(answer, dict):
|
|
return api_return_ok(data=answer)
|
|
elif answer is None:
|
|
return api_return_error("Function is not supported")
|
|
elif answer:
|
|
return api_return_ok()
|
|
return api_return_error()
|
|
|
|
return wrap_hostcontrol
|
|
|
|
|
|
def api_process_raw(method):
|
|
"""Wrap function with raw output to rest api."""
|
|
async def wrap_api(api, *args, **kwargs):
|
|
"""Return api information."""
|
|
try:
|
|
message = await method(api, *args, **kwargs)
|
|
except RuntimeError as err:
|
|
message = str(err).encode()
|
|
|
|
return web.Response(body=message)
|
|
|
|
return wrap_api
|
|
|
|
|
|
def api_return_error(message=None):
|
|
"""Return a API error message."""
|
|
if message:
|
|
_LOGGER.error(message)
|
|
|
|
return web.json_response({
|
|
JSON_RESULT: RESULT_ERROR,
|
|
JSON_MESSAGE: message,
|
|
}, status=400)
|
|
|
|
|
|
def api_return_ok(data=None):
|
|
"""Return a API ok answer."""
|
|
return web.json_response({
|
|
JSON_RESULT: RESULT_OK,
|
|
JSON_DATA: data or {},
|
|
})
|
|
|
|
|
|
async def api_validate(schema, request):
|
|
"""Validate request data with schema."""
|
|
data = await request.json(loads=json_loads)
|
|
try:
|
|
data = schema(data)
|
|
except vol.Invalid as ex:
|
|
raise RuntimeError(humanize_error(data, ex)) from None
|
|
|
|
return data
|
|
|
|
|
|
def hash_password(password):
|
|
"""Hash and salt our passwords."""
|
|
key = ")*()*SALT_HASSIO2123{}6554547485HSKA!!*JSLAfdasda$".format(password)
|
|
return hashlib.sha256(key.encode()).hexdigest()
|