@@ -44,10 +44,8 @@ class GRPCDesignsServiceV1(GRPCDesignsService): # pragma: no cover
4444 @protect_grpc
4545 def __init__ (self , channel : grpc .Channel ): # noqa: D102
4646 from ansys .api .discovery .v1 .design .designdoc_pb2_grpc import DesignDocStub
47- from ansys .api .discovery .v1 .commands .file_pb2_grpc import FileStub
4847
49- self .designs_stub = DesignDocStub (channel )
50- self .file_stub = FileStub (channel )
48+ self .stub = DesignDocStub (channel )
5149
5250 @protect_grpc
5351 def open (self , ** kwargs ) -> dict : # noqa: D102
@@ -92,35 +90,58 @@ def get_active(self, **kwargs) -> dict: # noqa: D102
9290 @protect_grpc
9391 def upload_file (self , ** kwargs ) -> dict : # noqa: D102
9492 from ansys .api .discovery .v1 .commands .file_pb2 import OpenRequest
93+ from pathlib import Path
94+ from typing import TYPE_CHECKING , Generator
95+
96+ import ansys .geometry .core .connection .defaults as pygeom_defaults
97+
98+ if TYPE_CHECKING : # pragma: no cover
99+ from ansys .geometry .core .misc .options import ImportOptions
100+
101+ def request_generator (
102+ file_path : Path , open_file : bool , import_options : "ImportOptions"
103+ ) -> Generator [OpenRequest , None , None ]:
104+ """Generate requests for streaming file upload."""
105+ msg_buffer = 5 * 1024 # 5KB - for additional message data
106+ if pygeom_defaults .MAX_MESSAGE_LENGTH - msg_buffer < 0 : # pragma: no cover
107+ raise ValueError ("MAX_MESSAGE_LENGTH is too small for file upload." )
108+
109+ chunk_size = pygeom_defaults .MAX_MESSAGE_LENGTH - msg_buffer
110+ with Path .open (file_path , "rb" ) as file :
111+ while chunk := file .read (chunk_size ):
112+ yield OpenRequest (
113+ data = chunk ,
114+ file_name = file_path .name ,
115+ open = open_file ,
116+ import_options = import_options .to_dict (),
117+ )
118+
95119
96- # Create the request - assumes all inputs are valid and of the proper type
97- request = OpenRequest (
98- data = kwargs ["data" ],
99- #file_name=kwargs["file_name"],
100- #open=kwargs["open_file"],
101- import_options = kwargs ["import_options" ].to_dict (),
102- )
103120
104121 # Call the gRPC service
105- response = self .file_stub .Open (request )
122+ response = self .commands_stub .UploadFile (request_generator (
123+ file_path = kwargs ["file_path" ],
124+ open_file = kwargs ["open_file" ],
125+ import_options = kwargs ["import_options" ],
126+ ))
106127
107128 # Return the response - formatted as a dictionary
108- return {"file_path" : response .file_path }
129+ return {"file_path" : response .file_path }
109130
110131 @protect_grpc
111132 def upload_file_stream (self , ** kwargs ) -> dict : # noqa: D102
112- from ansys .api .discovery .v1 .commands .file_pb2 import OpenRequest
133+ from ansys .api .discovery .v1 .commands .file_pb2 import UploadFileRequest
113134
114135 # Create the request - assumes all inputs are valid and of the proper type
115- request = OpenRequest (
136+ request = UploadFileRequest (
116137 data = kwargs ["data" ],
117138 file_name = kwargs ["file_name" ],
118139 open = kwargs ["open_file" ],
119140 import_options = kwargs ["import_options" ].to_dict (),
120141 )
121142
122143 # Call the gRPC service
123- response = self .file_stub . Open (request )
144+ response = self .commands_stub . UploadFile (request )
124145
125146 # Return the response - formatted as a dictionary
126147 return {"file_path" : response .file_path }
0 commit comments