-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
563241f
commit aa3fb17
Showing
19 changed files
with
352 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""`kedro_viz.api.rest.responses.base` contains base | ||
response classes and utility functions for the REST endpoints""" | ||
|
||
# pylint: disable=missing-class-docstring | ||
|
||
import abc | ||
import logging | ||
from pydantic import BaseModel, ConfigDict | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class APINotFoundResponse(BaseModel): | ||
message: str | ||
|
||
|
||
class BaseAPIResponse(BaseModel, abc.ABC): | ||
model_config = ConfigDict(from_attributes=True) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
"""`kedro_viz.api.rest.responses.deploy` contains response classes | ||
and utility functions for the `/deploy` REST endpoint""" | ||
|
||
# pylint: disable=invalid-name | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from kedro_viz.api.rest.responses.nodes import get_node_metadata_response | ||
from kedro_viz.api.rest.responses.pipelines import get_pipeline_response | ||
from kedro_viz.api.rest.responses.utils import get_encoded_response | ||
from kedro_viz.data_access import data_access_manager | ||
from kedro_viz.models.flowchart import DataNodeMetadata | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def save_api_responses_to_fs(path: str, remote_fs: Any, is_all_previews_enabled: bool): | ||
"""Saves all Kedro Viz API responses to a directory.""" | ||
try: | ||
logger.debug( | ||
"""Saving/Uploading api files to %s""", | ||
path, | ||
) | ||
|
||
main_path = f"{path}/api/main" | ||
nodes_path = f"{path}/api/nodes" | ||
pipelines_path = f"{path}/api/pipelines" | ||
|
||
if "file" in remote_fs.protocol: | ||
remote_fs.makedirs(path, exist_ok=True) | ||
remote_fs.makedirs(nodes_path, exist_ok=True) | ||
remote_fs.makedirs(pipelines_path, exist_ok=True) | ||
|
||
save_api_main_response_to_fs(main_path, remote_fs) | ||
save_api_node_response_to_fs(nodes_path, remote_fs, is_all_previews_enabled) | ||
save_api_pipeline_response_to_fs(pipelines_path, remote_fs) | ||
|
||
except Exception as exc: # pragma: no cover | ||
logger.exception( | ||
"An error occurred while preparing data for saving. Error: %s", str(exc) | ||
) | ||
raise exc | ||
|
||
|
||
def save_api_main_response_to_fs(main_path: str, remote_fs: Any): | ||
"""Saves API /main response to a directory.""" | ||
try: | ||
write_api_response_to_fs(main_path, get_pipeline_response(), remote_fs) | ||
except Exception as exc: # pragma: no cover | ||
logger.exception("Failed to save default response. Error: %s", str(exc)) | ||
raise exc | ||
|
||
|
||
def save_api_pipeline_response_to_fs(pipelines_path: str, remote_fs: Any): | ||
"""Saves API /pipelines/{pipeline} response to a directory.""" | ||
for pipeline_id in data_access_manager.registered_pipelines.get_pipeline_ids(): | ||
try: | ||
write_api_response_to_fs( | ||
f"{pipelines_path}/{pipeline_id}", | ||
get_pipeline_response(pipeline_id), | ||
remote_fs, | ||
) | ||
except Exception as exc: # pragma: no cover | ||
logger.exception( | ||
"Failed to save pipeline data for pipeline ID %s. Error: %s", | ||
pipeline_id, | ||
str(exc), | ||
) | ||
raise exc | ||
|
||
|
||
def save_api_node_response_to_fs( | ||
nodes_path: str, remote_fs: Any, is_all_previews_enabled: bool | ||
): | ||
"""Saves API /nodes/{node} response to a directory.""" | ||
# Set if preview is enabled/disabled for all data nodes | ||
DataNodeMetadata.set_is_all_previews_enabled(is_all_previews_enabled) | ||
|
||
for node_id in data_access_manager.nodes.get_node_ids(): | ||
try: | ||
write_api_response_to_fs( | ||
f"{nodes_path}/{node_id}", get_node_metadata_response(node_id), remote_fs | ||
) | ||
except Exception as exc: # pragma: no cover | ||
logger.exception( | ||
"Failed to save node data for node ID %s. Error: %s", node_id, str(exc) | ||
) | ||
raise exc | ||
|
||
|
||
def write_api_response_to_fs(file_path: str, response: Any, remote_fs: Any): | ||
"""Get encoded responses and writes it to a file""" | ||
encoded_response = get_encoded_response(response) | ||
|
||
with remote_fs.open(file_path, "wb") as file: | ||
file.write(encoded_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
package/tests/test_api/test_rest/test_responses/test_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from kedro_viz.api.rest.responses.base import APINotFoundResponse | ||
|
||
|
||
def test_api_not_found_response_valid_message(): | ||
response = APINotFoundResponse(message="Resource not found") | ||
assert response.message == "Resource not found" | ||
|
||
# Test that the model is serializable to a dictionary | ||
serialized_response = response.model_dump() | ||
assert serialized_response == {"message": "Resource not found"} |
Oops, something went wrong.