Skip to content

Commit

Permalink
Unify preset names and change 404 to 400
Browse files Browse the repository at this point in the history
  • Loading branch information
shauneccles committed Dec 30, 2023
1 parent f2b3c0f commit 160633f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 40 deletions.
36 changes: 12 additions & 24 deletions ledfx/api/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ async def get(self, effect_id) -> web.Response:
return web.json_response(data=response, status=400)

if effect_id in self._ledfx.config["ledfx_presets"].keys():
default = self._ledfx.config["ledfx_presets"][effect_id]
ledfx_presets = self._ledfx.config["ledfx_presets"][effect_id]
else:
default = {}
ledfx_presets = {}

if effect_id in self._ledfx.config["user_presets"].keys():
custom = self._ledfx.config["user_presets"][effect_id]
user_presets = self._ledfx.config["user_presets"][effect_id]
else:
custom = {}
user_presets = {}

response = {
"status": "success",
"effect": effect_id,
"default_presets": default,
"custom_presets": custom,
"ledfx_presets": ledfx_presets,
"user_presets": user_presets,
}
return web.json_response(data=response, status=200)

Expand All @@ -65,9 +65,7 @@ async def put(self, effect_id, request) -> web.Response:
if category not in ["ledfx_presets", "user_presets"]:
response = {
"status": "failed",
"reason": 'Category {} is not "ledfx_presets" or "user_presets"'.format(
category
),
"reason": f'Category {category} is not "ledfx_presets" or "user_presets"',
}
return web.json_response(data=response, status=400)

Expand All @@ -81,9 +79,7 @@ async def put(self, effect_id, request) -> web.Response:
if effect_id not in self._ledfx.config[category].keys():
response = {
"status": "failed",
"reason": "Effect {} does not exist in category {}".format(
effect_id, category
),
"reason": f"Effect {effect_id} does not exist in category {category}",
}
return web.json_response(data=response, status=400)

Expand All @@ -106,9 +102,7 @@ async def put(self, effect_id, request) -> web.Response:
if preset_id not in self._ledfx.config[category][effect_id].keys():
response = {
"status": "failed",
"reason": "Preset {} does not exist for effect {} in category {}".format(
preset_id, effect_id, category
),
"reason": f"Preset {preset_id} does not exist for effect {effect_id} in category {category}",
}
return web.json_response(data=response, status=400)

Expand Down Expand Up @@ -148,9 +142,7 @@ async def delete(self, effect_id, request) -> web.Response:
if category not in ["ledfx_presets", "user_presets"]:
response = {
"status": "failed",
"reason": 'Category {} is not "ledfx_presets" or "user_presets"'.format(
category
),
"reason": f'Category {category} is not "ledfx_presets" or "user_presets"',
}
return web.json_response(data=response, status=400)

Expand All @@ -173,9 +165,7 @@ async def delete(self, effect_id, request) -> web.Response:
if effect_id not in self._ledfx.config[category].keys():
response = {
"status": "failed",
"reason": "Effect {} does not exist in category {}".format(
effect_id, category
),
"reason": f"Effect {effect_id} does not exist in category {category}",
}
return web.json_response(data=response, status=400)

Expand All @@ -189,9 +179,7 @@ async def delete(self, effect_id, request) -> web.Response:
if preset_id not in self._ledfx.config[category][effect_id].keys():
response = {
"status": "failed",
"reason": "Preset {} does not exist for effect {} in category {}".format(
preset_id, effect_id, category
),
"reason": f"Preset {preset_id} does not exist for effect {effect_id} in category {category}",
}
return web.json_response(data=response, status=400)

Expand Down
25 changes: 9 additions & 16 deletions ledfx/api/virtual_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def get(self, virtual_id) -> web.Response:
"status": "failed",
"reason": f"Virtual with ID {virtual_id} not found",
}
return web.json_response(data=response, status=404)
return web.json_response(data=response, status=400)

if not virtual.active_effect:
response = {
Expand All @@ -48,8 +48,8 @@ async def get(self, virtual_id) -> web.Response:
"status": "success",
"virtual": virtual_id,
"effect": effect_id,
"default_presets": default,
"custom_presets": custom,
"ledfx_presets": default,
"user_presets": custom,
}

return web.json_response(data=response, status=200)
Expand Down Expand Up @@ -83,7 +83,7 @@ async def put(self, virtual_id, request) -> web.Response:
"status": "failed",
"reason": f"Virtual with ID {virtual_id} not found",
}
return web.json_response(data=response, status=404)
return web.json_response(data=response, status=400)

try:
data = await request.json()
Expand All @@ -100,18 +100,13 @@ async def put(self, virtual_id, request) -> web.Response:
}
return web.json_response(data=response, status=400)

if category not in ["default_presets", "custom_presets"]:
if category not in ["ledfx_presets", "user_presets"]:
response = {
"status": "failed",
"reason": f'Category {category} is not "ledfx_presets" or "user_presets"',
}
return web.json_response(data=response, status=400)

if category == "default_presets":
category = "ledfx_presets"
else:
category = "user_presets"

if effect_id is None:
response = {
"status": "failed",
Expand All @@ -136,9 +131,7 @@ async def put(self, virtual_id, request) -> web.Response:
if preset_id not in self._ledfx.config[category][effect_id].keys():
response = {
"status": "failed",
"reason": "Preset {} does not exist for effect {} in category {}".format(
preset_id, effect_id, category
),
"reason": f"Preset {preset_id} does not exist for effect {effect_id} in category {category}",
}
return web.json_response(data=response, status=400)

Expand Down Expand Up @@ -181,14 +174,14 @@ async def post(self, virtual_id, request) -> web.Response:
"status": "failed",
"reason": f"Virtual with ID {virtual_id} not found",
}
return web.json_response(data=response, status=404)
return web.json_response(data=response, status=400)

if not virtual.active_effect:
response = {
"status": "failed",
"reason": f"Virtual {virtual_id} has no active effect",
}
return web.json_response(data=response, status=404)
return web.json_response(data=response, status=400)

try:
data = await request.json()
Expand Down Expand Up @@ -241,7 +234,7 @@ async def delete(self, virtual_id) -> web.Response:
"status": "failed",
"reason": f"Virtual with ID {virtual_id} not found",
}
return web.json_response(data=response, status=404)
return web.json_response(data=response, status=400)

# Clear the effect
virtual.clear_effect()
Expand Down

0 comments on commit 160633f

Please sign in to comment.