Implement more API functions
This commit is contained in:
parent
e3e150c805
commit
a8d0a7d886
84
api/views.py
84
api/views.py
|
@ -1,11 +1,12 @@
|
||||||
import functools
|
import functools
|
||||||
from json import dumps
|
from json import JSONDecodeError, dumps, loads
|
||||||
|
|
||||||
from klein import Klein
|
from klein import Klein
|
||||||
from twisted.web.server import Request
|
from twisted.web.server import Request
|
||||||
|
|
||||||
import main
|
import main
|
||||||
from modules import userinfo
|
from modules import userinfo
|
||||||
|
from utils.logging.log import warn
|
||||||
|
|
||||||
|
|
||||||
def login_required(func):
|
def login_required(func):
|
||||||
|
@ -38,28 +39,83 @@ class API(object):
|
||||||
|
|
||||||
app = Klein()
|
app = Klein()
|
||||||
|
|
||||||
@app.route("/test/", methods=["GET"])
|
@app.route("/", methods=["GET"])
|
||||||
@login_required
|
@login_required
|
||||||
def hello(self, request):
|
def hello(self, request):
|
||||||
return "Hello"
|
return "Hello"
|
||||||
|
|
||||||
@app.route("/who/<query>/", methods=["GET"])
|
@app.route("/who/<query>/", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def who(self, request, query):
|
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
|
# Expand the generator
|
||||||
return dumps({k: [x for x in v] for k, v in result.items()})
|
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
|
@login_required
|
||||||
def chans(self, request, query):
|
def chans(self, request):
|
||||||
query = query.split(",")
|
try:
|
||||||
result = userinfo.getChans(query)
|
data = loads(request.content.read())
|
||||||
return dumps({k: [x for x in v] for k, v in result.items()})
|
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
|
@login_required
|
||||||
def users(self, request, query):
|
def users(self, request):
|
||||||
query = [f"#{x}" for x in query.split(",")]
|
try:
|
||||||
result = userinfo.getUsers(query)
|
data = loads(request.content.read())
|
||||||
return dumps({k: [x for x in v] for k, v in result.items()})
|
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)
|
||||||
|
|
Loading…
Reference in New Issue