From 49f5f41d30a0436ee0845ee64ac1c982d8c3ae8d Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 1 Aug 2019 16:17:47 +0200 Subject: [PATCH] Add /checkin and /checkout api. --- api/rest.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/api/rest.py b/api/rest.py index 2b58af3..cba8773 100644 --- a/api/rest.py +++ b/api/rest.py @@ -9,7 +9,7 @@ import datetime import logging import typing as T -from aiohttp_session import get_session +from aiohttp_session import get_session, Session from bot_z.async_operator import AsyncOperator from api.async_bot import login, logout, checkin, checkout, status @@ -60,8 +60,34 @@ async def logout_handler(request: web.Request) -> web.Response: return web.json_response({"error": "No session"}, status=404) res = await logout(op) alog.debug("logout result: %s", res) - # FIX: assess if better to invalidate session and dump the browser session. + # FIX: assess if better to invalidate session and dump the browser instance. return web.json_response({"logged_in": res}, status=200) + + +@routes.post("/checkin") +async def checkin_handler(request: web.Request) -> web.Response: + alog.debug("checkin") + session = await get_session(request) + op = OPERATORS.get(session.get("async_operator")) + if not op: + return web.json_response({"error": "No session"}, status=404) + res = await checkin(op) + alog.debug("checkin result: %s", res) + return web.json_response({"checked_in": res}, status=200) + + +@routes.post("/checkout") +async def checkout_handler(request: web.Request) -> web.Response: + alog.debug("checkout") + session = await get_session(request) + op = OPERATORS.get(session.get("async_operator")) + if not op: + return web.json_response({"error": "No session"}, status=404) + res = await checkout(op) + alog.debug("checkout result: %s", res) + return web.json_response({"checked_in": res}, status=200) + + @routes.get("/movements") async def movements_handle(request: web.Request) -> web.Response: alog.debug("movements")