Implement more API functions

This commit is contained in:
Mark Veidemanis 2022-07-21 13:40:17 +01:00
parent 47a3f84c1c
commit f589c7fc16
1 changed files with 70 additions and 14 deletions

View File

@ -1,11 +1,12 @@
import functools
from json import dumps
from json import JSONDecodeError, dumps, loads
from klein import Klein
from twisted.web.server import Request
import main
from modules import userinfo
from utils.logging.log import warn
def login_required(func):
@ -38,28 +39,83 @@ class API(object):
app = Klein()
@app.route("/test/", methods=["GET"])
@app.route("/", methods=["GET"])
@login_required
def hello(self, request):
return "Hello"
@app.route("/who/<query>/", methods=["GET"])
@app.route("/who/<query>/", methods=["POST"])
@login_required
def who(self, request, query):
result = userinfo.getWho(query)
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "query" not in data:
return "No query provided"
result = userinfo.getWho(data["query"])
# Expand the generator
return dumps({k: [x for x in v] for k, v in result.items()})
@app.route("/chans/<query>/", methods=["GET"])
@app.route("/chans/", methods=["POST"])
@login_required
def chans(self, request, query):
query = query.split(",")
result = userinfo.getChans(query)
return dumps({k: [x for x in v] for k, v in result.items()})
def chans(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "net" not in data:
return "No net provided"
if "query" not in data:
return "No query provided"
if not data["query"]:
warn(f"No query provided: for chans {data}")
return dumps({})
result = userinfo.getChansSingle(data["net"], data["query"])
if not result:
return dumps({})
return dumps({"chans": [x for x in result]})
@app.route("/users/<query>/", methods=["GET"])
@app.route("/users/", methods=["POST"])
@login_required
def users(self, request, query):
query = [f"#{x}" for x in query.split(",")]
result = userinfo.getUsers(query)
return dumps({k: [x for x in v] for k, v in result.items()})
def users(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "net" not in data:
return "No net provided"
if "query" not in data:
return "No query provided"
if not data["query"]:
warn(f"No query provided for users: {data}")
return dumps({})
result = userinfo.getUsersSingle(data["net"], data["query"])
if not result:
return dumps({})
return dumps({"users": [x for x in result]})
@app.route("/online/", methods=["POST"])
@login_required
def online(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "net" not in data:
return "No net provided"
if "query" not in data:
return "No users provided"
if not data["query"]:
warn(f"No users provided: for online {data}")
return dumps({})
net = data["net"]
usermap = {}
for user in data["query"]:
channels = userinfo.getChansSingle(net, [user])
if channels:
usermap[user] = True
else:
usermap[user] = False
return dumps(usermap)