2019-07-30 22:52:44 +02:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
The REST endpoints.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from aiohttp import web
|
2019-08-01 16:17:14 +02:00
|
|
|
import datetime
|
2019-07-30 22:52:44 +02:00
|
|
|
import logging
|
2019-08-01 00:29:28 +02:00
|
|
|
import typing as T
|
2019-07-30 22:52:44 +02:00
|
|
|
|
2019-08-01 16:17:47 +02:00
|
|
|
from aiohttp_session import get_session, Session
|
2019-08-01 00:29:28 +02:00
|
|
|
|
|
|
|
from bot_z.async_operator import AsyncOperator
|
2019-08-01 16:17:14 +02:00
|
|
|
from api.async_bot import login, logout, checkin, checkout, status
|
2019-08-01 00:29:28 +02:00
|
|
|
from api import BASE_URI
|
2019-07-30 22:52:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
alog = logging.getLogger("api")
|
|
|
|
routes = web.RouteTableDef()
|
2019-08-01 16:12:55 +02:00
|
|
|
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
|
2019-08-01 00:29:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def get_set_operator(
|
|
|
|
request: web.Request, user: T.Text, password: T.Text
|
2019-08-01 16:12:55 +02:00
|
|
|
) -> T.Tuple[AsyncOperator, Session]:
|
2019-08-01 00:29:28 +02:00
|
|
|
session = await get_session(request)
|
|
|
|
if "async_operator" in session:
|
|
|
|
op = OPERATORS[session["async_operator"]]
|
|
|
|
else:
|
|
|
|
op = AsyncOperator(BASE_URI, name=user)
|
|
|
|
session["async_operator"] = user
|
|
|
|
OPERATORS[user] = op
|
|
|
|
return op, session
|
2019-07-30 22:52:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
@routes.post("/login")
|
|
|
|
async def login_handler(request: web.Request) -> web.Response:
|
|
|
|
data = await request.post()
|
|
|
|
user = data.get("username")
|
|
|
|
password = data.get("password")
|
|
|
|
if not user or not password:
|
|
|
|
return web.json_response({"error": "Missing username or password"}, status=404)
|
2019-08-01 00:29:28 +02:00
|
|
|
op, session = await get_set_operator(request, user, password)
|
2019-07-30 22:52:44 +02:00
|
|
|
alog.debug("login - user: %s, password: %s", user, password)
|
|
|
|
res = await login(op, user, password)
|
|
|
|
alog.debug("login result: %s", res)
|
2019-08-01 00:29:28 +02:00
|
|
|
if not res:
|
|
|
|
session.invalidate()
|
|
|
|
alog.info("Login failed; session invalidated.")
|
2019-07-30 22:52:44 +02:00
|
|
|
return web.json_response({"logged_in": res}, status=200)
|
|
|
|
|
|
|
|
|
|
|
|
@routes.post("/logout")
|
|
|
|
async def logout_handler(request: web.Request) -> web.Response:
|
|
|
|
alog.debug("logout")
|
2019-08-01 00:29:28 +02:00
|
|
|
session = await get_session(request)
|
|
|
|
op = OPERATORS.get(session.get("async_operator"))
|
|
|
|
if not op:
|
|
|
|
return web.json_response({"error": "No session"}, status=404)
|
2019-07-30 22:52:44 +02:00
|
|
|
res = await logout(op)
|
|
|
|
alog.debug("logout result: %s", res)
|
2019-08-01 16:17:47 +02:00
|
|
|
# FIX: assess if better to invalidate session and dump the browser instance.
|
2019-07-30 22:52:44 +02:00
|
|
|
return web.json_response({"logged_in": res}, status=200)
|
2019-08-01 16:17:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2019-08-01 16:17:14 +02:00
|
|
|
@routes.get("/movements")
|
|
|
|
async def movements_handle(request: web.Request) -> web.Response:
|
|
|
|
alog.debug("movements")
|
|
|
|
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 status(op)
|
|
|
|
alog.debug("movements result: %s", res)
|
|
|
|
if not res:
|
|
|
|
return web.json_response({"error": "No movements found"}, status=404)
|
|
|
|
resp_data = dict((i[1], i[0]) for i in res) # type: ignore
|
|
|
|
return web.json_response(resp_data, status=200)
|