@@ -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