Skip to content

Commit

Permalink
add some typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ugomeda committed Feb 7, 2025
1 parent 2f16ab4 commit 0e413b3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 17 additions & 0 deletions inkplate_dashboard/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass
from typing import cast

from starlette.requests import Request

from inkplate_dashboard.chrome import ChromiumInstance
from inkplate_dashboard.config import DisplayConfiguration


@dataclass
class State:
chromium: ChromiumInstance
display: DisplayConfiguration


def get_state(request: Request) -> State:
return cast(State, request.state)
10 changes: 7 additions & 3 deletions inkplate_dashboard/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from starlette.responses import HTMLResponse, PlainTextResponse, Response

from inkplate_dashboard.display import generate_html
from inkplate_dashboard.state import get_state


def compile_styles() -> str:
Expand All @@ -24,15 +25,18 @@ async def get(self, request: Request) -> PlainTextResponse:

class DisplayHtmlEndpoint(HTTPEndpoint):
async def get(self, request: Request) -> HTMLResponse:
return HTMLResponse(await run_in_threadpool(generate_html, request.app.display))
return HTMLResponse(
await run_in_threadpool(generate_html, get_state(request).display)
)


class DisplayPngEndpoint(HTTPEndpoint):
async def get(self, request: Request) -> Response:
image, etag = await request.state.chromium.screenshot()
state = get_state(request)
image, etag = await state.chromium.screenshot()
headers = {
"etag": etag,
"Cache-Control": f"max-age={request.app.display.refresh_interval_sec}",
"Cache-Control": f"max-age={state.display.refresh_interval_sec}",
}

# Tell the display to avoid a refresh if the image did not change
Expand Down

0 comments on commit 0e413b3

Please sign in to comment.