@@ -49,8 +49,10 @@ class GRPCDesignsServiceV0(GRPCDesignsService): # pragma: no cover
4949 @protect_grpc
5050 def __init__ (self , channel : grpc .Channel ): # noqa: D102
5151 from ansys .api .dbu .v0 .designs_pb2_grpc import DesignsStub
52+ from ansys .api .geometry .v0 .commands_pb2_grpc import CommandsStub
5253
53- self .stub = DesignsStub (channel )
54+ self .designs_stub = DesignsStub (channel )
55+ self .commands_stub = CommandsStub (channel )
5456
5557 @protect_grpc
5658 def open (self , ** kwargs ) -> dict : # noqa: D102
@@ -63,7 +65,7 @@ def open(self, **kwargs) -> dict: # noqa: D102
6365 )
6466
6567 # Call the gRPC service
66- _ = self .stub .Open (request )
68+ _ = self .designs_stub .Open (request )
6769
6870 # Return the response - formatted as a dictionary
6971 return {}
@@ -76,7 +78,7 @@ def new(self, **kwargs) -> dict: # noqa: D102
7678 request = NewRequest (name = kwargs ["name" ])
7779
7880 # Call the gRPC service
79- response = self .stub .New (request )
81+ response = self .designs_stub .New (request )
8082
8183 # Return the response - formatted as a dictionary
8284 return {
@@ -90,7 +92,7 @@ def close(self, **kwargs) -> dict: # noqa: D102
9092 request = build_grpc_id (id = kwargs ["design_id" ])
9193
9294 # Call the gRPC service
93- _ = self .stub .Close (request )
95+ _ = self .designs_stub .Close (request )
9496
9597 # Return the response - formatted as a dictionary
9698 return {}
@@ -101,7 +103,7 @@ def put_active(self, **kwargs) -> dict: # noqa: D102
101103 request = build_grpc_id (id = kwargs ["design_id" ])
102104
103105 # Call the gRPC service
104- _ = self .stub .PutActive (request )
106+ _ = self .designs_stub .PutActive (request )
105107
106108 # Return the response - formatted as a dictionary
107109 return {}
@@ -118,7 +120,7 @@ def save_as(self, **kwargs) -> dict: # noqa: D102
118120 )
119121
120122 # Call the gRPC service
121- _ = self .stub .SaveAs (request )
123+ _ = self .designs_stub .SaveAs (request )
122124
123125 # Return the response - formatted as a dictionary
124126 return {}
@@ -136,7 +138,7 @@ def download_export(self, **kwargs) -> dict: # noqa: D102
136138 )
137139
138140 # Call the gRPC service
139- response = self .stub .DownloadExportFile (request )
141+ response = self .designs_stub .DownloadExportFile (request )
140142
141143 # Return the response - formatted as a dictionary
142144 data = bytes ()
@@ -156,7 +158,7 @@ def stream_download_export(self, **kwargs) -> dict: # noqa: D102
156158 )
157159
158160 # Call the gRPC service
159- response = self .stub .StreamDownloadExportFile (request )
161+ response = self .designs_stub .StreamDownloadExportFile (request )
160162
161163 # Return the response - formatted as a dictionary
162164 data = bytes ()
@@ -175,7 +177,7 @@ def insert(self, **kwargs) -> dict: # noqa: D102
175177 )
176178
177179 # Call the gRPC service
178- _ = self .stub .Insert (request )
180+ _ = self .designs_stub .Insert (request )
179181
180182 # Return the response - formatted as a dictionary
181183 return {}
@@ -185,7 +187,7 @@ def get_active(self, **kwargs) -> dict: # noqa: D102
185187 from google .protobuf .empty_pb2 import Empty
186188
187189 # Call the gRPC service
188- response = self .stub .GetActive (request = Empty ())
190+ response = self .designs_stub .GetActive (request = Empty ())
189191
190192 # Return the response - formatted as a dictionary
191193 if response :
@@ -194,3 +196,63 @@ def get_active(self, **kwargs) -> dict: # noqa: D102
194196 "main_part_id" : response .main_part .id ,
195197 "name" : response .name ,
196198 }
199+
200+ @protect_grpc
201+ def upload_file (self , ** kwargs ) -> dict : # noqa: D102
202+ from ansys .api .geometry .v0 .commands_pb2 import UploadFileRequest
203+
204+ # Create the request - assumes all inputs are valid and of the proper type
205+ request = UploadFileRequest (
206+ data = kwargs ["data" ],
207+ file_name = kwargs ["file_name" ],
208+ open = kwargs ["open_file" ],
209+ import_options = kwargs ["import_options" ].to_dict (),
210+ )
211+
212+ # Call the gRPC service
213+ response = self .commands_stub .UploadFile (request )
214+
215+ # Return the response - formatted as a dictionary
216+ return {"file_path" : response .file_path }
217+
218+ @protect_grpc
219+ def upload_file_stream (self , ** kwargs ) -> dict : # noqa: D102
220+ from pathlib import Path
221+ from typing import TYPE_CHECKING , Generator
222+
223+ from ansys .api .geometry .v0 .commands_pb2 import UploadFileRequest
224+
225+ import ansys .geometry .core .connection .defaults as pygeom_defaults
226+
227+ if TYPE_CHECKING : # pragma: no cover
228+ from ansys .geometry .core .misc .options import ImportOptions
229+
230+ def request_generator (
231+ file_path : Path , open_file : bool , import_options : "ImportOptions"
232+ ) -> Generator [UploadFileRequest , None , None ]:
233+ """Generate requests for streaming file upload."""
234+ msg_buffer = 5 * 1024 # 5KB - for additional message data
235+ if pygeom_defaults .MAX_MESSAGE_LENGTH - msg_buffer < 0 : # pragma: no cover
236+ raise ValueError ("MAX_MESSAGE_LENGTH is too small for file upload." )
237+
238+ chunk_size = pygeom_defaults .MAX_MESSAGE_LENGTH - msg_buffer
239+ with Path .open (file_path , "rb" ) as file :
240+ while chunk := file .read (chunk_size ):
241+ yield UploadFileRequest (
242+ data = chunk ,
243+ file_name = file_path .name ,
244+ open = open_file ,
245+ import_options = import_options .to_dict (),
246+ )
247+
248+ # Call the gRPC service
249+ response = self .commands_stub .StreamFileUpload (
250+ request_generator (
251+ file_path = kwargs ["file_path" ],
252+ open_file = kwargs ["open_file" ],
253+ import_options = kwargs ["import_options" ],
254+ )
255+ )
256+
257+ # Return the response - formatted as a dictionary
258+ return {"file_path" : response .file_path }
0 commit comments