refactor: share download result status response
This commit is contained in:
@@ -265,19 +265,22 @@ class DownloadService:
|
||||
self.cancel_download_fn(download_id)
|
||||
return self.web.json_response({"success": True})
|
||||
|
||||
def _json_result_with_success_status(self, result):
|
||||
"""Return a JSON result using HTTP status 200/400 from its success flag."""
|
||||
status = 200 if result.get("success") else 400
|
||||
return self.web.json_response(result, status=status)
|
||||
|
||||
async def pause_download(self, request):
|
||||
"""Pause an aria2 download."""
|
||||
download_id = request.match_info["download_id"]
|
||||
result = self.pause_download_fn(download_id)
|
||||
status = 200 if result.get("success") else 400
|
||||
return self.web.json_response(result, status=status)
|
||||
return self._json_result_with_success_status(result)
|
||||
|
||||
async def resume_download(self, request):
|
||||
"""Resume an aria2 download."""
|
||||
download_id = request.match_info["download_id"]
|
||||
result = self.resume_download_fn(download_id)
|
||||
status = 200 if result.get("success") else 400
|
||||
return self.web.json_response(result, status=status)
|
||||
return self._json_result_with_success_status(result)
|
||||
|
||||
async def clear_completed_downloads(self, request):
|
||||
"""Clear completed and failed downloads from progress memory."""
|
||||
@@ -294,14 +297,12 @@ class DownloadService:
|
||||
"""Start the aria2 daemon without starting a download."""
|
||||
settings = await self.get_override_settings_from_request(request)
|
||||
result = await self.asyncio.to_thread(self.start_aria2_daemon, settings)
|
||||
status = 200 if result.get("success") else 400
|
||||
return self.web.json_response(result, status=status)
|
||||
return self._json_result_with_success_status(result)
|
||||
|
||||
async def aria2_stop(self, request):
|
||||
"""Stop the aria2 daemon started by Model Resolver."""
|
||||
result = await self.asyncio.to_thread(self.stop_aria2_daemon)
|
||||
status = 200 if result.get("success") else 400
|
||||
return self.web.json_response(result, status=status)
|
||||
return self._json_result_with_success_status(result)
|
||||
|
||||
async def aria2_install(self, request):
|
||||
"""Install aria2 and persist the selected download backend."""
|
||||
|
||||
@@ -330,6 +330,7 @@ async def test_download_control_routes_preserve_progress_and_status_codes():
|
||||
pause_handler = handlers[("POST", "/model_resolver/pause/{download_id}")]
|
||||
response = await pause_handler(_request(download_id="pause-1"))
|
||||
assert response.status == 200
|
||||
assert json.loads(response.text) == {"success": True}
|
||||
values["pause_download"].assert_called_once_with("pause-1")
|
||||
|
||||
values["pause_download"].return_value = {"success": False, "error": "not aria2"}
|
||||
@@ -339,8 +340,14 @@ async def test_download_control_routes_preserve_progress_and_status_codes():
|
||||
resume_handler = handlers[("POST", "/model_resolver/resume/{download_id}")]
|
||||
response = await resume_handler(_request(download_id="resume-1"))
|
||||
assert response.status == 200
|
||||
assert json.loads(response.text) == {"success": True}
|
||||
values["resume_download"].assert_called_once_with("resume-1")
|
||||
|
||||
values["resume_download"].return_value = {"success": False, "error": "not aria2"}
|
||||
response = await resume_handler(_request(download_id="resume-2"))
|
||||
assert response.status == 400
|
||||
assert json.loads(response.text) == {"success": False, "error": "not aria2"}
|
||||
|
||||
clear_handler = handlers[("POST", "/model_resolver/clear_completed_downloads")]
|
||||
response = await clear_handler(_request())
|
||||
assert json.loads(response.text) == {"success": True}
|
||||
@@ -362,12 +369,29 @@ async def test_aria2_routes_use_override_settings_and_map_failures():
|
||||
assert json.loads(response.text) == {"status": "running"}
|
||||
assert values["get_aria2_status"].call_count == 2
|
||||
|
||||
values["start_aria2_daemon"].return_value = {
|
||||
"success": True,
|
||||
"message": "started",
|
||||
}
|
||||
response = await start_handler(_request())
|
||||
assert response.status == 200
|
||||
assert json.loads(response.text) == {"success": True, "message": "started"}
|
||||
|
||||
values["start_aria2_daemon"].return_value = {
|
||||
"success": False,
|
||||
"error": "cannot start",
|
||||
}
|
||||
response = await start_handler(_request())
|
||||
assert response.status == 400
|
||||
assert json.loads(response.text) == {"success": False, "error": "cannot start"}
|
||||
|
||||
values["stop_aria2_daemon"].return_value = {
|
||||
"success": True,
|
||||
"message": "stopped",
|
||||
}
|
||||
response = await stop_get(_request(method="GET"))
|
||||
assert response.status == 200
|
||||
assert json.loads(response.text) == {"success": True, "message": "stopped"}
|
||||
|
||||
values["stop_aria2_daemon"].return_value = {
|
||||
"success": False,
|
||||
@@ -375,6 +399,7 @@ async def test_aria2_routes_use_override_settings_and_map_failures():
|
||||
}
|
||||
response = await stop_get(_request(method="GET"))
|
||||
assert response.status == 400
|
||||
assert json.loads(response.text) == {"success": False, "error": "not running"}
|
||||
|
||||
response = await install_handler(_request({"force": True}))
|
||||
assert json.loads(response.text)["success"] is True
|
||||
|
||||
Reference in New Issue
Block a user