@@ -105,10 +105,11 @@ async def create_data_source(
105105 config : Dict [str , Any ],
106106) -> List [TextContent ]:
107107 """Create a new data source."""
108+ from ..response_formatter import format_success , format_error , gsql_has_error
109+
108110 try :
109111 conn = get_connection ()
110112
111- # Build the CREATE DATA_SOURCE command based on type
112113 config_str = ", " .join ([f'{ k } ="{ v } "' for k , v in config .items ()])
113114
114115 gsql_cmd = f"CREATE DATA_SOURCE { data_source_type .upper ()} { data_source_name } "
@@ -118,21 +119,37 @@ async def create_data_source(
118119 result = await conn .gsql (gsql_cmd )
119120 result_str = str (result ) if result else ""
120121
121- from ..response_formatter import gsql_has_error
122122 if gsql_has_error (result_str ):
123- message = f"Failed: Could not create data source '{ data_source_name } ':\n { result_str } "
124- else :
125- message = f"Success: Data source '{ data_source_name } ' of type '{ data_source_type } ' created successfully:\n { result_str } "
123+ return format_error (
124+ operation = "create_data_source" ,
125+ error = Exception (f"Could not create data source:\n { result_str } " ),
126+ context = {"data_source_name" : data_source_name , "data_source_type" : data_source_type },
127+ )
128+
129+ return format_success (
130+ operation = "create_data_source" ,
131+ summary = f"Data source '{ data_source_name } ' of type '{ data_source_type } ' created successfully" ,
132+ data = {"data_source_name" : data_source_name , "result" : result_str },
133+ suggestions = [
134+ f"View data source: get_data_source(data_source_name='{ data_source_name } ')" ,
135+ "List all data sources: get_all_data_sources()" ,
136+ ],
137+ )
126138 except Exception as e :
127- message = f"Failed to create data source due to: { str (e )} "
128- return [TextContent (type = "text" , text = message )]
139+ return format_error (
140+ operation = "create_data_source" ,
141+ error = e ,
142+ context = {"data_source_name" : data_source_name },
143+ )
129144
130145
131146async def update_data_source (
132147 data_source_name : str ,
133148 config : Dict [str , Any ],
134149) -> List [TextContent ]:
135150 """Update an existing data source."""
151+ from ..response_formatter import format_success , format_error , gsql_has_error
152+
136153 try :
137154 conn = get_connection ()
138155
@@ -142,95 +159,165 @@ async def update_data_source(
142159 result = await conn .gsql (gsql_cmd )
143160 result_str = str (result ) if result else ""
144161
145- from ..response_formatter import gsql_has_error
146162 if gsql_has_error (result_str ):
147- message = f"Failed: Could not update data source '{ data_source_name } ':\n { result_str } "
148- else :
149- message = f"Success: Data source '{ data_source_name } ' updated successfully:\n { result_str } "
163+ return format_error (
164+ operation = "update_data_source" ,
165+ error = Exception (f"Could not update data source:\n { result_str } " ),
166+ context = {"data_source_name" : data_source_name },
167+ )
168+
169+ return format_success (
170+ operation = "update_data_source" ,
171+ summary = f"Data source '{ data_source_name } ' updated successfully" ,
172+ data = {"data_source_name" : data_source_name , "result" : result_str },
173+ )
150174 except Exception as e :
151- message = f"Failed to update data source due to: { str (e )} "
152- return [TextContent (type = "text" , text = message )]
175+ return format_error (
176+ operation = "update_data_source" ,
177+ error = e ,
178+ context = {"data_source_name" : data_source_name },
179+ )
153180
154181
155182async def get_data_source (
156183 data_source_name : str ,
157184) -> List [TextContent ]:
158185 """Get information about a data source."""
186+ from ..response_formatter import format_success , format_error , gsql_has_error
187+
159188 try :
160189 conn = get_connection ()
161190
162191 result = await conn .gsql (f"SHOW DATA_SOURCE { data_source_name } " )
163192 result_str = str (result ) if result else ""
164193
165- from ..response_formatter import gsql_has_error
166194 if gsql_has_error (result_str ):
167- message = f"Failed: Could not retrieve data source '{ data_source_name } ':\n { result_str } "
168- else :
169- message = f"Success: Data source '{ data_source_name } ':\n { result_str } "
195+ return format_error (
196+ operation = "get_data_source" ,
197+ error = Exception (f"Could not retrieve data source:\n { result_str } " ),
198+ context = {"data_source_name" : data_source_name },
199+ )
200+
201+ return format_success (
202+ operation = "get_data_source" ,
203+ summary = f"Data source '{ data_source_name } ' details" ,
204+ data = {"data_source_name" : data_source_name , "details" : result_str },
205+ )
170206 except Exception as e :
171- message = f"Failed to get data source due to: { str (e )} "
172- return [TextContent (type = "text" , text = message )]
207+ return format_error (
208+ operation = "get_data_source" ,
209+ error = e ,
210+ context = {"data_source_name" : data_source_name },
211+ )
173212
174213
175214async def drop_data_source (
176215 data_source_name : str ,
177216) -> List [TextContent ]:
178217 """Drop a data source."""
218+ from ..response_formatter import format_success , format_error , gsql_has_error
219+
179220 try :
180221 conn = get_connection ()
181222
182223 result = await conn .gsql (f"DROP DATA_SOURCE { data_source_name } " )
183224 result_str = str (result ) if result else ""
184225
185- from ..response_formatter import gsql_has_error
186226 if gsql_has_error (result_str ):
187- message = f"Failed: Could not drop data source '{ data_source_name } ':\n { result_str } "
188- else :
189- message = f"Success: Data source '{ data_source_name } ' dropped successfully:\n { result_str } "
227+ return format_error (
228+ operation = "drop_data_source" ,
229+ error = Exception (f"Could not drop data source:\n { result_str } " ),
230+ context = {"data_source_name" : data_source_name },
231+ )
232+
233+ return format_success (
234+ operation = "drop_data_source" ,
235+ summary = f"Data source '{ data_source_name } ' dropped successfully" ,
236+ data = {"data_source_name" : data_source_name , "result" : result_str },
237+ suggestions = ["List remaining: get_all_data_sources()" ],
238+ metadata = {"destructive" : True },
239+ )
190240 except Exception as e :
191- message = f"Failed to drop data source due to: { str (e )} "
192- return [TextContent (type = "text" , text = message )]
241+ return format_error (
242+ operation = "drop_data_source" ,
243+ error = e ,
244+ context = {"data_source_name" : data_source_name },
245+ )
193246
194247
195248async def get_all_data_sources (** kwargs ) -> List [TextContent ]:
196249 """Get all data sources."""
250+ from ..response_formatter import format_success , format_error , gsql_has_error
251+
197252 try :
198253 conn = get_connection ()
199254
200255 result = await conn .gsql ("SHOW DATA_SOURCE *" )
201256 result_str = str (result ) if result else ""
202257
203- from ..response_formatter import gsql_has_error
204258 if gsql_has_error (result_str ):
205- message = f"Failed: Could not retrieve data sources:\n { result_str } "
206- else :
207- message = f"Success: All data sources:\n { result_str } "
259+ return format_error (
260+ operation = "get_all_data_sources" ,
261+ error = Exception (f"Could not retrieve data sources:\n { result_str } " ),
262+ context = {},
263+ )
264+
265+ return format_success (
266+ operation = "get_all_data_sources" ,
267+ summary = "All data sources retrieved" ,
268+ data = {"details" : result_str },
269+ suggestions = ["Create a data source: create_data_source(...)" ],
270+ )
208271 except Exception as e :
209- message = f"Failed to get data sources due to: { str (e )} "
210- return [TextContent (type = "text" , text = message )]
272+ return format_error (
273+ operation = "get_all_data_sources" ,
274+ error = e ,
275+ context = {},
276+ )
211277
212278
213279async def drop_all_data_sources (
214280 confirm : bool = False ,
215281) -> List [TextContent ]:
216282 """Drop all data sources."""
283+ from ..response_formatter import format_success , format_error , gsql_has_error
284+
217285 if not confirm :
218- return [TextContent (type = "text" , text = "Error: Drop all data sources requires confirm=True. This is a destructive operation." )]
286+ return format_error (
287+ operation = "drop_all_data_sources" ,
288+ error = ValueError ("Confirmation required" ),
289+ context = {},
290+ suggestions = [
291+ "Set confirm=True to proceed with this destructive operation" ,
292+ "This will drop ALL data sources" ,
293+ ],
294+ )
219295
220296 try :
221297 conn = get_connection ()
222298
223299 result = await conn .gsql ("DROP DATA_SOURCE *" )
224300 result_str = str (result ) if result else ""
225301
226- from ..response_formatter import gsql_has_error
227302 if gsql_has_error (result_str ):
228- message = f"Failed: Could not drop all data sources:\n { result_str } "
229- else :
230- message = f"Success: All data sources dropped successfully:\n { result_str } "
303+ return format_error (
304+ operation = "drop_all_data_sources" ,
305+ error = Exception (f"Could not drop all data sources:\n { result_str } " ),
306+ context = {},
307+ )
308+
309+ return format_success (
310+ operation = "drop_all_data_sources" ,
311+ summary = "All data sources dropped successfully" ,
312+ data = {"result" : result_str },
313+ metadata = {"destructive" : True },
314+ )
231315 except Exception as e :
232- message = f"Failed to drop all data sources due to: { str (e )} "
233- return [TextContent (type = "text" , text = message )]
316+ return format_error (
317+ operation = "drop_all_data_sources" ,
318+ error = e ,
319+ context = {},
320+ )
234321
235322
236323async def preview_sample_data (
@@ -240,23 +327,36 @@ async def preview_sample_data(
240327 graph_name : Optional [str ] = None ,
241328) -> List [TextContent ]:
242329 """Preview sample data from a file."""
330+ from ..response_formatter import format_success , format_error , gsql_has_error
331+
243332 try :
244333 conn = get_connection (graph_name = graph_name )
245334
246- gsql_cmd = f"""
247- USE GRAPH { conn .graphname }
248- SHOW DATA_SOURCE { data_source_name } FILE "{ file_path } " LIMIT { num_rows }
249- """
335+ gsql_cmd = (
336+ f" USE GRAPH { conn .graphname } \n "
337+ f' SHOW DATA_SOURCE { data_source_name } FILE "{ file_path } " LIMIT { num_rows } '
338+ )
250339
251340 result = await conn .gsql (gsql_cmd )
252341 result_str = str (result ) if result else ""
253342
254- from ..response_formatter import gsql_has_error
255343 if gsql_has_error (result_str ):
256- message = f"Failed: Could not preview data from '{ file_path } ':\n { result_str } "
257- else :
258- message = f"Success: Sample data preview from '{ file_path } ' (first { num_rows } rows):\n { result_str } "
344+ return format_error (
345+ operation = "preview_sample_data" ,
346+ error = Exception (f"Could not preview data:\n { result_str } " ),
347+ context = {"data_source_name" : data_source_name , "file_path" : file_path },
348+ )
349+
350+ return format_success (
351+ operation = "preview_sample_data" ,
352+ summary = f"Sample data from '{ file_path } ' (first { num_rows } rows)" ,
353+ data = {"data_source_name" : data_source_name , "file_path" : file_path , "preview" : result_str },
354+ metadata = {"graph_name" : conn .graphname },
355+ )
259356 except Exception as e :
260- message = f"Failed to preview sample data due to: { str (e )} "
261- return [TextContent (type = "text" , text = message )]
357+ return format_error (
358+ operation = "preview_sample_data" ,
359+ error = e ,
360+ context = {"data_source_name" : data_source_name , "file_path" : file_path },
361+ )
262362
0 commit comments