Updated routes and session logic improved.
This commit is contained in:
parent
7d6b445678
commit
11e5378300
72
api/rest.py
72
api/rest.py
|
@ -13,7 +13,7 @@ from aiohttp_session import get_session, Session
|
||||||
|
|
||||||
from bot_z.async_operator import AsyncOperator
|
from bot_z.async_operator import AsyncOperator
|
||||||
from api.async_bot import login, logout, checkin, checkout, status
|
from api.async_bot import login, logout, checkin, checkout, status
|
||||||
from api import BASE_URI
|
from api import BASE_URI, DEBUG
|
||||||
|
|
||||||
|
|
||||||
alog = logging.getLogger("api")
|
alog = logging.getLogger("api")
|
||||||
|
@ -25,22 +25,39 @@ async def get_set_operator(
|
||||||
request: web.Request, user: T.Text, password: T.Text
|
request: web.Request, user: T.Text, password: T.Text
|
||||||
) -> T.Tuple[AsyncOperator, Session]:
|
) -> T.Tuple[AsyncOperator, Session]:
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
|
op = None
|
||||||
if "async_operator" in session:
|
if "async_operator" in session:
|
||||||
op = OPERATORS[session["async_operator"]]
|
op = OPERATORS.get(session["async_operator"])
|
||||||
else:
|
base_uri = request.app["base_uri"]
|
||||||
op = AsyncOperator(BASE_URI, name=user)
|
debug = request.app["debug"]
|
||||||
|
headless = request.app["headless"]
|
||||||
|
if op is None:
|
||||||
|
op = AsyncOperator(base_uri, name=user, headless=headless, debug=debug)
|
||||||
session["async_operator"] = user
|
session["async_operator"] = user
|
||||||
OPERATORS[user] = op
|
OPERATORS[user] = op
|
||||||
return op, session
|
return op, session
|
||||||
|
|
||||||
|
|
||||||
|
@routes.get("/")
|
||||||
|
@routes.get("/login")
|
||||||
|
@routes.get("/badge")
|
||||||
|
async def routing_handler(request: web.Request) -> web.Response:
|
||||||
|
alog.debug("(%s) %s", request.method, request.path)
|
||||||
|
session = await get_session(request)
|
||||||
|
op = session.get("async_operator")
|
||||||
|
_logged_in = True if op else False
|
||||||
|
alog.info("%s - is%s in session", request.path, " NOT" if not _logged_in else "")
|
||||||
|
return web.json_response({"logged_in": _logged_in})
|
||||||
|
|
||||||
|
|
||||||
@routes.post("/login")
|
@routes.post("/login")
|
||||||
async def login_handler(request: web.Request) -> web.Response:
|
async def login_handler(request: web.Request) -> web.Response:
|
||||||
data = await request.post()
|
data = await request.json()
|
||||||
user = data.get("username")
|
user = data.get("username")
|
||||||
password = data.get("password")
|
password = data.get("password")
|
||||||
if not user or not password:
|
if not user or not password:
|
||||||
return web.json_response({"error": "Missing username or password"}, status=404)
|
alog.debug("login - missing username or password: %s", data)
|
||||||
|
return web.json_response({"error": "Missing username or password"}, status=403)
|
||||||
op, session = await get_set_operator(request, user, password)
|
op, session = await get_set_operator(request, user, password)
|
||||||
alog.debug("login - user: %s, password: %s", user, password)
|
alog.debug("login - user: %s, password: %s", user, password)
|
||||||
res = await login(op, user, password)
|
res = await login(op, user, password)
|
||||||
|
@ -55,12 +72,16 @@ async def login_handler(request: web.Request) -> web.Response:
|
||||||
async def logout_handler(request: web.Request) -> web.Response:
|
async def logout_handler(request: web.Request) -> web.Response:
|
||||||
alog.debug("logout")
|
alog.debug("logout")
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
op = OPERATORS.get(session.get("async_operator"))
|
op_key = session.get("async_operator")
|
||||||
|
op = OPERATORS.get(op_key)
|
||||||
if not op:
|
if not op:
|
||||||
return web.json_response({"error": "No session"}, status=404)
|
return web.json_response(
|
||||||
|
{"error": "No session", "logged_in": False}, status=403
|
||||||
|
)
|
||||||
res = await logout(op)
|
res = await logout(op)
|
||||||
alog.debug("logout result: %s", res)
|
alog.debug("logout result: %s", res)
|
||||||
# FIX: assess if better to invalidate session and dump the browser instance.
|
# FIX: assess if better to invalidate session and dump the browser instance.
|
||||||
|
del OPERATORS[op_key]
|
||||||
return web.json_response({"logged_in": res}, status=200)
|
return web.json_response({"logged_in": res}, status=200)
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,10 +91,12 @@ async def checkin_handler(request: web.Request) -> web.Response:
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
op = OPERATORS.get(session.get("async_operator"))
|
op = OPERATORS.get(session.get("async_operator"))
|
||||||
if not op:
|
if not op:
|
||||||
return web.json_response({"error": "No session"}, status=404)
|
return web.json_response(
|
||||||
|
{"error": "No session", "logged_in": False}, status=403
|
||||||
|
)
|
||||||
res = await checkin(op)
|
res = await checkin(op)
|
||||||
alog.debug("checkin result: %s", res)
|
alog.debug("checkin result: %s", res)
|
||||||
return web.json_response({"checked_in": res}, status=200)
|
return web.json_response({"checked_in": res, "logged_in": True}, status=200)
|
||||||
|
|
||||||
|
|
||||||
@routes.post("/checkout")
|
@routes.post("/checkout")
|
||||||
|
@ -82,10 +105,12 @@ async def checkout_handler(request: web.Request) -> web.Response:
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
op = OPERATORS.get(session.get("async_operator"))
|
op = OPERATORS.get(session.get("async_operator"))
|
||||||
if not op:
|
if not op:
|
||||||
return web.json_response({"error": "No session"}, status=404)
|
return web.json_response(
|
||||||
|
{"error": "No session", "logged_in": False}, status=403
|
||||||
|
)
|
||||||
res = await checkout(op)
|
res = await checkout(op)
|
||||||
alog.debug("checkout result: %s", res)
|
alog.debug("checkout result: %s", res)
|
||||||
return web.json_response({"checked_in": res}, status=200)
|
return web.json_response({"checked_in": res, "logged_in": True}, status=200)
|
||||||
|
|
||||||
|
|
||||||
@routes.get("/movements")
|
@routes.get("/movements")
|
||||||
|
@ -94,10 +119,27 @@ async def movements_handle(request: web.Request) -> web.Response:
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
op = OPERATORS.get(session.get("async_operator"))
|
op = OPERATORS.get(session.get("async_operator"))
|
||||||
if not op:
|
if not op:
|
||||||
return web.json_response({"error": "No session"}, status=404)
|
return web.json_response(
|
||||||
|
{"error": "No session", "logged_in": False}, status=403
|
||||||
|
)
|
||||||
res = await status(op)
|
res = await status(op)
|
||||||
alog.debug("movements result: %s", res)
|
alog.debug("movements result: %s", res)
|
||||||
if not res:
|
if not res:
|
||||||
return web.json_response({"error": "No movements found"}, status=404)
|
return web.json_response(
|
||||||
resp_data = dict((i[1], i[0]) for i in res) # type: ignore
|
{"error": "No movements found", "logged_in": True}, status=404
|
||||||
|
)
|
||||||
|
movements = []
|
||||||
|
for r in res:
|
||||||
|
if r and len(r) == 2:
|
||||||
|
movements.append({"time": r[1], "type": r[0]})
|
||||||
|
resp_data = {"movements": movements}
|
||||||
|
resp_data["logged_in"] = True # type: ignore
|
||||||
return web.json_response(resp_data, status=200)
|
return web.json_response(resp_data, status=200)
|
||||||
|
|
||||||
|
|
||||||
|
@routes.get("/status")
|
||||||
|
async def status_handle(request: web.Request) -> web.Response:
|
||||||
|
alog.debug("status")
|
||||||
|
session = await get_session(request)
|
||||||
|
_logged_in = session.get("async_operator") in OPERATORS
|
||||||
|
return web.json_response({"logged_in": _logged_in}, status=200)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user