Skip to content

Commit 42be5bc

Browse files
committed
http: api: blueprints: implement validate
1 parent e8a8d06 commit 42be5bc

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/enapter/cli/http/api/blueprint_command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .blueprint_download_command import BlueprintDownloadCommand
66
from .blueprint_upload_command import BlueprintUploadCommand
7+
from .blueprint_validate_command import BlueprintValidateCommand
78

89

910
class BlueprintCommand(cli.Command):
@@ -17,6 +18,7 @@ def register(parent: cli.Subparsers) -> None:
1718
for command in [
1819
BlueprintDownloadCommand,
1920
BlueprintUploadCommand,
21+
BlueprintValidateCommand,
2022
]:
2123
command.register(subparsers)
2224

@@ -27,5 +29,7 @@ async def run(args: argparse.Namespace) -> None:
2729
await BlueprintDownloadCommand.run(args)
2830
case "upload":
2931
await BlueprintUploadCommand.run(args)
32+
case "validate":
33+
await BlueprintValidateCommand.run(args)
3034
case _:
3135
raise NotImplementedError(args.command_command)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import argparse
2+
import logging
3+
import pathlib
4+
5+
from enapter import cli, http
6+
7+
LOGGER = logging.getLogger(__name__)
8+
9+
10+
class BlueprintValidateCommand(cli.Command):
11+
12+
@staticmethod
13+
def register(parent: cli.Subparsers) -> None:
14+
parser = parent.add_parser(
15+
"validate", formatter_class=argparse.ArgumentDefaultsHelpFormatter
16+
)
17+
parser.add_argument(
18+
"path", type=pathlib.Path, help="Path to a directory or a zip file"
19+
)
20+
21+
@staticmethod
22+
async def run(args: argparse.Namespace) -> None:
23+
async with http.api.Client(http.api.Config.from_env()) as client:
24+
if args.path.is_dir():
25+
await client.blueprints.validate_directory(args.path)
26+
else:
27+
await client.blueprints.validate_file(args.path)

src/enapter/http/api/blueprints/client.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ async def upload_file(self, path: pathlib.Path) -> Blueprint:
2020
return await self.upload(data)
2121

2222
async def upload_directory(self, path: pathlib.Path) -> Blueprint:
23-
buffer = io.BytesIO()
24-
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
25-
for file_path in path.rglob("*"):
26-
zip_file.write(file_path, arcname=file_path.relative_to(path))
27-
return await self.upload(buffer.getvalue())
23+
data = await self._zip_directory(path)
24+
return await self.upload(data)
2825

2926
async def upload(self, data: bytes) -> Blueprint:
3027
url = "v3/blueprints/upload"
@@ -39,3 +36,29 @@ async def download(
3936
response = await self._client.get(url, params={"view": view.value})
4037
api.check_error(response)
4138
return response.content
39+
40+
async def validate_file(self, path: pathlib.Path) -> None:
41+
with path.open("rb") as file:
42+
data = file.read()
43+
await self.validate(data)
44+
45+
async def validate_directory(self, path: pathlib.Path) -> None:
46+
data = await self._zip_directory(path)
47+
await self.validate(data)
48+
49+
async def validate(self, data: bytes) -> None:
50+
url = "v3/blueprints/validate"
51+
response = await self._client.post(url, content=data)
52+
api.check_error(response)
53+
validation_errors = response.json().get("validation_errors", [])
54+
if validation_errors:
55+
raise api.MultiError(
56+
[api.Error(msg, code=None, details=None) for msg in validation_errors]
57+
)
58+
59+
async def _zip_directory(self, path: pathlib.Path) -> bytes:
60+
buffer = io.BytesIO()
61+
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
62+
for file_path in path.rglob("*"):
63+
zip_file.write(file_path, arcname=file_path.relative_to(path))
64+
return buffer.getvalue()

0 commit comments

Comments
 (0)