http responses are entirely rewritten #6

Merged
kristy-fournier merged 6 commits from responseCodes into main 2026-01-26 12:31:24 -05:00
2 changed files with 75 additions and 61 deletions
Showing only changes of commit fce09edfc5 - Show all commits

View file

@ -38,7 +38,6 @@ async function getFromServer(bodyInfo, source="",password=adminPass) {
// the currently set password is always included in every request // the currently set password is always included in every request
bodyInfo["password"] = password; bodyInfo["password"] = password;
} }
// console.log(bodyInfo);
const response = await fetch("http://"+ip+"/"+source, { const response = await fetch("http://"+ip+"/"+source, {
method: "POST", method: "POST",
body: JSON.stringify(bodyInfo), body: JSON.stringify(bodyInfo),

View file

@ -13,6 +13,7 @@ portTheUserPicked=os.getenv("SERVER_PORT")
ERR_NO_ADMIN = ({"error":"no-admin","data":None},401) ERR_NO_ADMIN = ({"error":"no-admin","data":None},401)
ERR_200 = ({"error":"OK","data":None},200) ERR_200 = ({"error":"OK","data":None},200)
ERR_MISSING_ARGS = ({"error":"Request missing required arguments","data":None}),400
if args.admin: if args.admin:
ADMIN_PASS = hashlib.sha256(bytes(args.admin,'utf-8')).hexdigest() ADMIN_PASS = hashlib.sha256(bytes(args.admin,'utf-8')).hexdigest()
else: else:
@ -108,7 +109,7 @@ def playerControls():
global skipNow global skipNow
global partyMode global partyMode
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
if recieveData["control"] != None: try:
if recieveData["control"] == "play-pause": if recieveData["control"] == "play-pause":
if ADMIN_PASS == recieveData['password'] or controlPerms["PP"]: if ADMIN_PASS == recieveData['password'] or controlPerms["PP"]:
player.pause() player.pause()
@ -131,8 +132,8 @@ def playerControls():
return ERR_NO_ADMIN return ERR_NO_ADMIN
else: else:
return {"error":"Not a valid control","data":None},400 return {"error":"Not a valid control","data":None},400
else: except KeyError:
return {"error":"No control sent","data":None},400 return ERR_MISSING_ARGS
@app.route("/settings", methods=['POST']) @app.route("/settings", methods=['POST'])
def settingsControl(): def settingsControl():
@ -141,76 +142,90 @@ def settingsControl():
global partyMode global partyMode
global player global player
recieveData = request.get_json(force=True) recieveData = request.get_json(force=True)
if recieveData["setting"] == "volume": try:
if ADMIN_PASS == recieveData['password'] or controlPerms["VOL"]: if recieveData["setting"] == "volume":
volumeLevel = int(recieveData["level"]) if ADMIN_PASS == recieveData['password'] or controlPerms["VOL"]:
if(volumeLevel <= 100 and volumeLevel >= 0): volumeLevel = int(recieveData["level"])
volumePassed = player.audio_set_volume(volumeLevel) if(volumeLevel <= 100 and volumeLevel >= 0):
return {"error":"ok","data":{"volumePassed":volumePassed}},200 volumePassed = player.audio_set_volume(volumeLevel)
return {"error":"ok","data":{"volumePassed":volumePassed}},200
else:
return {"error":"Invalid volume level","data":None},422
else: else:
return {"error":"Invalid volume level","data":None},422 return ERR_NO_ADMIN
elif recieveData["setting"] == "partymode-toggle":
if ADMIN_PASS == recieveData['password'] or controlPerms["PM"]:
partyMode = not(partyMode)
return ERR_200
else:
return ERR_NO_ADMIN
elif recieveData["setting"] == "perms":
if ADMIN_PASS == recieveData["password"]:
controlPerms = recieveData["admin"]
return ERR_200
else:
return ERR_NO_ADMIN
elif recieveData["setting"] == "getsettings":
# probably should have made this a different request type or something but it works
return {"error":"ok","data":{"partymode":partyMode,"volume":player.audio_get_volume(),"admin":controlPerms}},200
else: else:
return ERR_NO_ADMIN return {"error":"Not a valid setting","data":None},400
elif recieveData["setting"] == "partymode-toggle": except:
if ADMIN_PASS == recieveData['password'] or controlPerms["PM"]: return ERR_MISSING_ARGS
partyMode = not(partyMode)
return ERR_200
else:
return ERR_NO_ADMIN
elif recieveData["setting"] == "perms":
if ADMIN_PASS == recieveData["password"]:
controlPerms = recieveData["admin"]
return ERR_200
else:
return ERR_NO_ADMIN
elif recieveData["setting"] == "getsettings":
# probably should have made this a different request type or something but it works
return {"error":"ok","data":{"partymode":partyMode,"volume":player.audio_get_volume(),"admin":controlPerms}},200
else:
return {"error":"Not a valid setting","data":None},422
@app.route("/search", methods=['POST']) @app.route("/search", methods=['POST'])
def searchSongDB(): def searchSongDB():
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
fileofDB = sql.connect("songDatabase.db") fileofDB = sql.connect("songDatabase.db")
songDatabase = fileofDB.cursor() songDatabase = fileofDB.cursor()
results = [] try:
if (recieveData['search'] == ""): results = []
songDatabase.execute("SELECT * FROM virtualSongs") if (recieveData['search'] == ""):
results = songDatabase.fetchall() songDatabase.execute("SELECT * FROM virtualSongs")
else: results = songDatabase.fetchall()
songDatabase.execute("SELECT * FROM virtualSongs WHERE virtualSongs MATCH ?",[recieveData['search']]) else:
results = songDatabase.fetchall() songDatabase.execute("SELECT * FROM virtualSongs WHERE virtualSongs MATCH ?",[recieveData['search']])
tempdata = {} results = songDatabase.fetchall()
# this is a temporary solution so i dont have to change the client tempdata = {}
for i in results: # this is a temporary solution so i dont have to change the client
tempdata[i[0]] = { for i in results:
"title": i[1], tempdata[i[0]] = {
"artist": i[2], "title": i[1],
"art": i[3], "artist": i[2],
"length": i[4], "art": i[3],
"lossless":i[5] "length": i[4],
} "lossless":i[5]
fileofDB.close() }
fileofDB.close()
return {"error":"ok","data":tempdata},200
except KeyError:
fileofDB.close()
return ERR_MISSING_ARGS
except sql.OperationalError:
fileofDB.close()
return ({"error":"Invalid search, sorry!","data":None},422)
return {"error":"ok","data":tempdata},200
@app.route("/songadd", methods=["POST"]) @app.route("/songadd", methods=["POST"])
def songadd(): def songadd():
recieveData=request.get_json(force=True) recieveData=request.get_json(force=True)
if (ADMIN_PASS == recieveData['password']) or controlPerms["AS"]: try:
# Password exists and is correct, or it's not restricted if (ADMIN_PASS == recieveData['password']) or controlPerms["AS"]:
# if (recieveData['song'] in playlist): # Password exists and is correct, or it's not restricted
# return {"error":"song-in-queue"} # if (recieveData['song'] in playlist):
# else: # return {"error":"song-in-queue"}
# Right now the above is disabled since i want to make it optional first # else:
# probably with a checkbox like the other admin controls # Right now the above is disabled since i want to make it optional first
if True: # probably with a checkbox like the other admin controls
queueSong(recieveData['song']) if True:
return ERR_200 queueSong(recieveData['song'])
else: return ERR_200
# the password is incorrect (technically a password not existing falls into the above case because controlPerms is never changed) else:
return ERR_NO_ADMIN # the password is incorrect (technically a password not existing falls into the above case because controlPerms is never changed)
return ERR_NO_ADMIN
except KeyError:
return ERR_MISSING_ARGS
@app.route("/playlist", methods=["POST"]) @app.route("/playlist", methods=["POST"])
def getPlaylist(): def getPlaylist():