Created
March 4, 2023 00:35
-
-
Save shauneccles/14585bad0869804376e19a3fcc24792f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from json import JSONDecodeError | |
from aiohttp import web | |
from ledfx.api import RestEndpoint | |
_LOGGER = logging.getLogger(__name__) | |
class DevicesEndpoint(RestEndpoint): | |
"""REST end-point for querying and managing devices""" | |
ENDPOINT_PATH = "/api/notify" | |
def __init__(self, ledfx): | |
self.icon = ledfx.icon | |
print(self.icon) | |
async def get(self) -> web.Response: | |
response = {"status": "success"} | |
return web.json_response(data=response, status=200) | |
async def post(self, request) -> web.Response: | |
try: | |
data = await request.json() | |
except JSONDecodeError: | |
response = { | |
"status": "failed", | |
"reason": "JSON Decoding failed", | |
} | |
return web.json_response(data=response, status=400) | |
title = data.get("title") | |
if title is None: | |
response = { | |
"status": "failed", | |
"reason": 'Required attribute "title" was not provided', | |
} | |
return web.json_response(data=response, status=400) | |
body = data.get("body") | |
if body is None: | |
response = { | |
"status": "failed", | |
"reason": 'Required attribute "body" was not provided', | |
} | |
return web.json_response(data=response, status=400) | |
if self.icon is None: | |
return web.json_response(data={"status":"failed","reason":"no tray icon"}, status=400) | |
if self.icon.HAS_NOTIFICATION: | |
self.icon.notify( | |
f"{title}\n{body}" | |
) | |
return web.json_response(data={"status":"success:"}, status=200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment