@@ -4156,7 +4156,13 @@ def create_thumbnail(self, project_name, src_filepath):
41564156 "Failed to create thumbnail.{}" .format (details ))
41574157 return response .data ["id" ]
41584158
4159- def send_batch_operations (self , project_name , operations , can_fail = False ):
4159+ def send_batch_operations (
4160+ self ,
4161+ project_name ,
4162+ operations ,
4163+ can_fail = False ,
4164+ raise_on_fail = True
4165+ ):
41604166 """Post multiple CRUD operations to server.
41614167
41624168 When multiple changes should be made on server side this is the best
@@ -4169,28 +4175,32 @@ def send_batch_operations(self, project_name, operations, can_fail=False):
41694175 operations (list[dict[str, Any]]): Operations to be processed.
41704176 can_fail (bool): Server will try to process all operations even if
41714177 one of them fails.
4178+ raise_on_fail (bool): Raise exception if an operation fails.
4179+ You can handle failed operations on your own when set to
4180+ 'False'.
41724181
41734182 Raises:
4174- FailedOperations: When one or more operations fail.
4183+ ValueError: Operations can't be converted to json string.
4184+ FailedOperations: When output does not contain server operations
4185+ or 'raise_on_fail' is enabled and any operation fails.
4186+
4187+ Returns:
4188+ list[dict[str, Any]]: Operations result with process details.
41754189 """
41764190
41774191 if not operations :
4178- return
4192+ return []
41794193
41804194 body_by_id = {}
4195+ operations_body = []
41814196 for operation in operations :
41824197 if not operation :
41834198 continue
4199+
41844200 op_id = operation .get ("id" )
41854201 if not op_id :
41864202 op_id = create_entity_id ()
41874203 operation ["id" ] = op_id
4188- body_by_id [op_id ] = operation
4189-
4190- operations_body = []
4191- for operation in operations :
4192- if not operation :
4193- continue
41944204
41954205 try :
41964206 body = json .loads (
@@ -4203,33 +4213,35 @@ def send_batch_operations(self, project_name, operations, can_fail=False):
42034213 )
42044214 ))
42054215
4206- body_by_id [operation [ "id" ] ] = body
4216+ body_by_id [op_id ] = body
42074217 operations_body .append (body )
42084218
42094219 if not operations_body :
4210- return
4220+ return []
42114221
42124222 result = self .post (
42134223 "projects/{}/operations" .format (project_name ),
42144224 operations = operations_body ,
42154225 canFail = can_fail
42164226 )
42174227
4218- if result .get ("success" ):
4219- return
4220-
4221- if "operations" not in result :
4228+ op_results = result .get ("operations" )
4229+ if op_results is None :
42224230 raise FailedOperations (
42234231 "Operation failed. Content: {}" .format (str (result ))
42244232 )
42254233
4226- for op_result in result ["operations" ]:
4234+ if result .get ("success" ) or not raise_on_fail :
4235+ return op_results
4236+
4237+ for op_result in op_results :
42274238 if not op_result ["success" ]:
42284239 operation_id = op_result ["id" ]
42294240 raise FailedOperations ((
4230- "Operation \" {}\" failed with data:\n {}\n Error : {}."
4241+ "Operation \" {}\" failed with data:\n {}\n Detail : {}."
42314242 ).format (
42324243 operation_id ,
42334244 json .dumps (body_by_id [operation_id ], indent = 4 ),
4234- op_result ["error " ],
4245+ op_result ["detail " ],
42354246 ))
4247+ return op_results
0 commit comments