Skip to content

Commit

Permalink
updates to result objects
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Mar 21, 2024
1 parent 828739b commit 193b3af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
20 changes: 18 additions & 2 deletions editor-api/archive_editor/data_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass, asdict, field
from pydantic import BaseModel as Base, create_model, ConfigDict
from pydantic import BaseModel as Base, create_model, ConfigDict, field_validator
from typing import Union, Dict, List, Tuple
from fastapi import HTTPException
from biosimulators_utils.combine.data_model import CombineArchive
from biosimulators_utils.sedml.data_model import SedDocument, Model, Simulation

Expand Down Expand Up @@ -31,7 +32,22 @@ class SerializedParametersBase(BaseModel):
pass


class SimulationEditConfirmation(BaseModel):
class ResultConfirmation(BaseModel):
message: str


class UnsuccessfulSimulationEditConfirmation(ResultConfirmation):
message: str = "Could not successfully edit the simulation"
exception: Exception
error: HTTPException

@field_validator('error')
@classmethod
def raise_error(cls, error: HTTPException):
return error(status_code=500, detail=str(cls.exception))


class SuccessfulSimulationEditConfirmation(BaseModel):
message: str = "Simulation edited successfully."
download_link: str

Expand Down
14 changes: 11 additions & 3 deletions editor-api/archive_editor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from tempfile import TemporaryDirectory, mkdtemp
import shutil # For copying files
from archive_editor.api import ArchiveEditorApi
from archive_editor.data_model import SimulationEditConfirmation, SimulationEditResult
from archive_editor.data_model import (
SuccessfulSimulationEditConfirmation,
UnsuccessfulSimulationEditConfirmation,
SimulationEditResult
)


app = FastAPI(title="editor-api", version="1.0.0")
Expand Down Expand Up @@ -51,9 +55,13 @@ async def edit_simulation(
edited_file_path = os.path.join(edited_files_storage, f"{new_omex_filename}.omex")
shutil.move(file_path, edited_file_path)

return SimulationEditConfirmation(download_link=f"/download/{edited_file_path}")
return SuccessfulSimulationEditConfirmation(download_link=f"/download/{edited_file_path}")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
result = UnsuccessfulSimulationEditConfirmation(
error=HTTPException(status_code=500, detail=str(e)),
exception=e)
# raise HTTPException(status_code=500, detail=str(e))
raise result.error


@app.get("/download/{file_identifier}")
Expand Down

0 comments on commit 193b3af

Please sign in to comment.