Use dedicated endpoints

This commit is contained in:
Ludeeus 2022-06-10 07:41:56 +00:00
parent 20a6c53c2d
commit 11995e73ca
4 changed files with 34 additions and 12 deletions

View File

@ -478,11 +478,21 @@ class AddonModel(CoreSysAttributes, ABC):
"""Return True if an icon exists.""" """Return True if an icon exists."""
return self.path_icon.exists() return self.path_icon.exists()
@property
def with_dark_icon(self) -> bool:
"""Return True if an dark icon exists."""
return self.path_dark_icon.exists()
@property @property
def with_logo(self) -> bool: def with_logo(self) -> bool:
"""Return True if a logo exists.""" """Return True if a logo exists."""
return self.path_logo.exists() return self.path_logo.exists()
@property
def with_dark_logo(self) -> bool:
"""Return True if a dark logo exists."""
return self.path_dark_logo.exists()
@property @property
def with_changelog(self) -> bool: def with_changelog(self) -> bool:
"""Return True if a changelog exists.""" """Return True if a changelog exists."""

View File

@ -382,7 +382,9 @@ class RestAPI(CoreSysAttributes):
web.post("/addons/{addon}/rebuild", api_addons.rebuild), web.post("/addons/{addon}/rebuild", api_addons.rebuild),
web.get("/addons/{addon}/logs", api_addons.logs), web.get("/addons/{addon}/logs", api_addons.logs),
web.get("/addons/{addon}/icon", api_addons.icon), web.get("/addons/{addon}/icon", api_addons.icon),
web.get("/addons/{addon}/dark_icon", api_addons.dark_icon),
web.get("/addons/{addon}/logo", api_addons.logo), web.get("/addons/{addon}/logo", api_addons.logo),
web.get("/addons/{addon}/dark_logo", api_addons.dark_logo),
web.get("/addons/{addon}/changelog", api_addons.changelog), web.get("/addons/{addon}/changelog", api_addons.changelog),
web.get("/addons/{addon}/documentation", api_addons.documentation), web.get("/addons/{addon}/documentation", api_addons.documentation),
web.post("/addons/{addon}/stdin", api_addons.stdin), web.post("/addons/{addon}/stdin", api_addons.stdin),

View File

@ -69,7 +69,6 @@ from ..const import (
ATTR_NETWORK_RX, ATTR_NETWORK_RX,
ATTR_NETWORK_TX, ATTR_NETWORK_TX,
ATTR_OPTIONS, ATTR_OPTIONS,
ATTR_PREFER_DARK,
ATTR_PRIVILEGED, ATTR_PRIVILEGED,
ATTR_PROTECTED, ATTR_PROTECTED,
ATTR_PWNED, ATTR_PWNED,
@ -114,12 +113,6 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
SCHEMA_VERSION = vol.Schema({vol.Optional(ATTR_VERSION): str}) SCHEMA_VERSION = vol.Schema({vol.Optional(ATTR_VERSION): str})
SCHEMA_DARK = vol.Schema(
{
vol.Optional(ATTR_PREFER_DARK, default=False): bool,
}
)
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
SCHEMA_OPTIONS = vol.Schema( SCHEMA_OPTIONS = vol.Schema(
{ {
@ -470,8 +463,17 @@ class APIAddons(CoreSysAttributes):
if not addon.with_icon: if not addon.with_icon:
raise APIError(f"No icon found for add-on {addon.slug}!") raise APIError(f"No icon found for add-on {addon.slug}!")
body = await api_validate(SCHEMA_DARK, request) with addon.path_icon.open("rb") as png:
if body[ATTR_PREFER_DARK] and addon.path_dark_icon.exists(): return png.read()
@api_process_raw(CONTENT_TYPE_PNG)
async def dark_icon(self, request: web.Request) -> bytes:
"""Return dark icon from add-on."""
addon = self._extract_addon(request)
if not addon.with_icon:
raise APIError(f"No icon found for add-on {addon.slug}!")
if addon.with_dark_icon:
with addon.path_dark_icon.open("rb") as png: with addon.path_dark_icon.open("rb") as png:
return png.read() return png.read()
@ -485,8 +487,17 @@ class APIAddons(CoreSysAttributes):
if not addon.with_logo: if not addon.with_logo:
raise APIError(f"No logo found for add-on {addon.slug}!") raise APIError(f"No logo found for add-on {addon.slug}!")
body = await api_validate(SCHEMA_DARK, request) with addon.path_logo.open("rb") as png:
if body[ATTR_PREFER_DARK] and addon.path_dark_logo.exists(): return png.read()
@api_process_raw(CONTENT_TYPE_PNG)
async def dark_logo(self, request: web.Request) -> bytes:
"""Return dark logo from add-on."""
addon = self._extract_addon(request)
if not addon.with_logo:
raise APIError(f"No logo found for add-on {addon.slug}!")
if addon.with_dark_logo:
with addon.path_dark_logo.open("rb") as png: with addon.path_dark_logo.open("rb") as png:
return png.read() return png.read()

View File

@ -255,7 +255,6 @@ ATTR_PASSWORD = "password"
ATTR_PORT = "port" ATTR_PORT = "port"
ATTR_PORTS = "ports" ATTR_PORTS = "ports"
ATTR_PORTS_DESCRIPTION = "ports_description" ATTR_PORTS_DESCRIPTION = "ports_description"
ATTR_PREFER_DARK = "prefer_dark_icon"
ATTR_PREFIX = "prefix" ATTR_PREFIX = "prefix"
ATTR_PRIMARY = "primary" ATTR_PRIMARY = "primary"
ATTR_PRIORITY = "priority" ATTR_PRIORITY = "priority"