pep.py/handlers/apiIsOnlineHandler.py

49 lines
1.3 KiB
Python
Raw Normal View History

2016-06-02 20:33:39 +00:00
import json
2016-10-02 20:48:14 +00:00
from common.web import requestsManager
from constants import exceptions
2016-06-02 20:33:39 +00:00
from objects import glob
2016-10-02 20:48:14 +00:00
class handler(requestsManager.asyncRequestHandler):
2016-06-02 20:33:39 +00:00
def asyncGet(self):
statusCode = 400
data = {"message": "unknown error"}
try:
# Check arguments
2016-08-24 03:10:29 +00:00
if "u" not in self.request.arguments and "id" not in self.request.arguments:
raise exceptions.invalidArgumentsException()
2016-06-02 20:33:39 +00:00
# Get online staus
username = None
userID = None
if "u" in self.request.arguments:
username = self.get_argument("u").lower().replace(" ", "_")
else:
try:
userID = int(self.get_argument("id"))
except:
raise exceptions.invalidArgumentsException()
2016-09-02 15:45:10 +00:00
if username is None and userID is None:
2016-06-02 20:33:39 +00:00
data["result"] = False
else:
2016-09-02 15:45:10 +00:00
if username is not None:
data["result"] = True if glob.tokens.getTokenFromUsername(username, safe=True) is not None else False
else:
2016-09-02 15:45:10 +00:00
data["result"] = True if glob.tokens.getTokenFromUserID(userID) is not None else False
2016-06-02 20:33:39 +00:00
# Status code and message
statusCode = 200
data["message"] = "ok"
except exceptions.invalidArgumentsException:
statusCode = 400
data["message"] = "missing required arguments"
finally:
# Add status code to data
data["status"] = statusCode
# Send response
self.write(json.dumps(data))
2016-06-02 20:33:39 +00:00
self.set_status(statusCode)