api: refactor ccujack state keeping
This commit is contained in:
parent
07c72c752f
commit
b623082c4a
5 changed files with 22 additions and 36 deletions
|
|
@ -9,7 +9,6 @@ from fastapi.responses import RedirectResponse
|
|||
from contextlib import asynccontextmanager
|
||||
from simple_openid_connect.client import OpenidClient
|
||||
from simple_openid_connect.data import TokenSuccessResponse, RpInitiatedLogoutRequest
|
||||
from cachetools import TTLCache
|
||||
from aiohttp import BasicAuth
|
||||
|
||||
from dooris_api import deps, models, exceptions, app_config
|
||||
|
|
@ -37,12 +36,11 @@ async def lifespan(app: FastAPI):
|
|||
scope=app_cfg.openid_scope,
|
||||
)
|
||||
|
||||
app.extra["cache"] = TTLCache(maxsize=64, ttl=30 * 60)
|
||||
|
||||
app.extra["ccujack"] = CCUJackClient(
|
||||
"https://hmdooris-ccu.ccchh.net:2122",
|
||||
auth=BasicAuth("dooris", os.environ["HMDOORIS_PW"]),
|
||||
)
|
||||
await app.extra["ccujack"].find_locks()
|
||||
|
||||
yield
|
||||
|
||||
|
|
@ -178,7 +176,10 @@ async def logout(
|
|||
):
|
||||
deps.clear_auth_state(resp)
|
||||
return oidc_client.initiate_logout(
|
||||
RpInitiatedLogoutRequest(id_token_hint=current_user.raw_id_token, post_logout_redirect_uri=f"{app_config.get().base_url}/")
|
||||
RpInitiatedLogoutRequest(
|
||||
id_token_hint=current_user.raw_id_token,
|
||||
post_logout_redirect_uri=f"{app_config.get().base_url}/",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -188,19 +189,11 @@ async def logout(
|
|||
responses={status.HTTP_401_UNAUTHORIZED: {"model": models.HttpProblemDetail}},
|
||||
)
|
||||
async def list_locks(
|
||||
ccujack: deps.CCUJackClient, cache: deps.Cache
|
||||
ccujack: deps.CCUJackClient
|
||||
) -> List[models.Lock]:
|
||||
# discover locks from ccujack
|
||||
CACHE_KEY = "ccu-find-locks"
|
||||
if CACHE_KEY in cache:
|
||||
locks = cache[CACHE_KEY]
|
||||
else:
|
||||
locks = await ccujack.find_locks()
|
||||
cache[CACHE_KEY] = locks
|
||||
|
||||
# assemble result objects
|
||||
result = []
|
||||
for i_lock, lock_channels in locks:
|
||||
for i_lock, lock_channels in ccujack.locks:
|
||||
status_data = dict()
|
||||
for i_channel, channel_params in lock_channels:
|
||||
for i_param in channel_params:
|
||||
|
|
@ -244,3 +237,12 @@ async def list_locks(
|
|||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@app.patch(
|
||||
"/api/locks/{name}",
|
||||
tags=["locks"],
|
||||
responses={status.HTTP_401_UNAUTHORIZED: {"model": models.HttpProblemDetail}},
|
||||
)
|
||||
async def operate_lock(name: str):
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue