Skip to content

Commit

Permalink
Structure validation on create
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgiordano committed Jul 31, 2024
1 parent c1014eb commit e14d17b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion griptapecli/commands/skatepark.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def register(
try:
response.raise_for_status()
except requests.exceptions.HTTPError as e:
click.echo(f"HTTP Error: {e}")
click.echo(f"HTTP Error: {e.response.json().get('detail')}")
return

structure_id = response.json()["structure_id"]
Expand Down
15 changes: 15 additions & 0 deletions griptapecli/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,20 @@ class Status(Enum):
output: Optional[dict] = Field(default=None)


class StructureInput(BaseModel):
directory: str
structure_config_file: str
env: dict = Field(default_factory=lambda: {})


class Structure(BaseModel):
directory: str = Field()
structure_config_file: str = Field()
env: dict = Field(default_factory=lambda: {})

def model_post_init(self, __context):
self._validate_structure()

@computed_field
@property
def structure_id(self) -> str:
Expand All @@ -129,6 +138,12 @@ def structure_config(self) -> StructureConfig:
with open(config_path, "r") as config_file:
return StructureConfig(**yaml.safe_load(config_file))

def _validate_structure(self):
try:
self.structure_config
except Exception as e:
raise ValueError(f"Invalid structure config: {e}")


class ListStructuresResponseModel(BaseModel):
structures: list[Structure] = Field(default_factory=lambda: [])
Expand Down
8 changes: 7 additions & 1 deletion griptapecli/core/skatepark.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ListStructuresResponseModel,
Log,
Structure,
StructureInput,
StructureRun,
StructureRunInput,
)
Expand All @@ -35,7 +36,12 @@


@app.post("/api/structures", status_code=status.HTTP_201_CREATED)
def create_structure(structure: Structure) -> Structure:
def create_structure(structureInput: StructureInput) -> Structure:
try:
structure = Structure(**structureInput.model_dump())
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))

logger.info(f"Creating structure: {structure}")

state.register_structure(structure)
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/core/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from griptapecli.core.models import Event, Structure, StructureRun


Expand All @@ -10,6 +12,8 @@ def test_run_model_init(self):

def test_structure_model_init(self):
assert Structure(
directory="directory",
structure_config_file="structure_config.yaml",
directory=os.getcwd(),
structure_config_file=os.path.join(
"tests", "unit", "core", "utils", "structure_config.yaml"
),
)
Empty file.
11 changes: 11 additions & 0 deletions tests/unit/core/utils/structure_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 1.0
runtime: python3
runtime_version: 3.11
build:
requirements_file: src/requirements.txt
cache_build_dependencies:
enabled: true
watched_files:
- requirements.txt
run:
main_file: src/app.py

0 comments on commit e14d17b

Please sign in to comment.