BotZ/api/rest.py

38 lines
1.0 KiB
Python

# -*- encoding: utf-8 -*-
"""
The REST endpoints.
"""
from aiohttp import web
import logging
from api.async_bot import login, logout, checkin, checkout
alog = logging.getLogger("api")
routes = web.RouteTableDef()
@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)
op = request.app["async_operator"]
alog.debug("login - user: %s, password: %s", user, password)
res = await login(op, user, password)
alog.debug("login result: %s", res)
return web.json_response({"logged_in": res}, status=200)
@routes.post("/logout")
async def logout_handler(request: web.Request) -> web.Response:
alog.debug("logout")
op = request.app["async_operator"]
res = await logout(op)
alog.debug("logout result: %s", res)
return web.json_response({"logged_in": res}, status=200)