mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-11 08:28:11 -05:00
fix(tools/looker): Refactor run-inline-query logic to helper function (#1497)
Inline queries are directed to an undocumented endpoint so that they can be tracked in Looker System Activity separately. The logic is to try the undocumented endpoint first, and if there is any error, fall back to the documented endpoint. This was done in multiple places. This PR combines that logic into one helper function, reducing the duplication of code.
This commit is contained in:
@@ -284,3 +284,29 @@ func RunInlineQuery2(l *v4.LookerSDK, request RequestRunInlineQuery2, options *r
|
||||
err := l.AuthSession.Do(&result, "POST", "/4.0", "/queries/run_inline", nil, request, options)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func RunInlineQuery(ctx context.Context, sdk *v4.LookerSDK, wq *v4.WriteQuery, format string, options *rtl.ApiSettings) (string, error) {
|
||||
logger, err := util.LoggerFromContext(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get logger from ctx: %s", err)
|
||||
}
|
||||
req := v4.RequestRunInlineQuery{
|
||||
Body: *wq,
|
||||
ResultFormat: format,
|
||||
}
|
||||
req2 := RequestRunInlineQuery2{
|
||||
Query: *wq,
|
||||
RenderOpts: RenderOptions{
|
||||
Format: format,
|
||||
},
|
||||
QueryApiClientCtx: QueryApiClientContext{
|
||||
Name: "MCP Toolbox",
|
||||
},
|
||||
}
|
||||
resp, err := RunInlineQuery2(sdk, req2, options)
|
||||
if err != nil {
|
||||
logger.DebugContext(ctx, "error querying with new endpoint, trying again with original", err)
|
||||
resp, err = sdk.RunInlineQuery(req, options)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -127,27 +127,11 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
||||
}
|
||||
req := v4.RequestRunInlineQuery{
|
||||
Body: *wq,
|
||||
ResultFormat: "json",
|
||||
}
|
||||
req2 := lookercommon.RequestRunInlineQuery2{
|
||||
Query: *wq,
|
||||
RenderOpts: lookercommon.RenderOptions{
|
||||
Format: "json",
|
||||
},
|
||||
QueryApiClientCtx: lookercommon.QueryApiClientContext{
|
||||
Name: "MCP Toolbox",
|
||||
},
|
||||
}
|
||||
resp, err := lookercommon.RunInlineQuery2(sdk, req2, t.ApiSettings)
|
||||
resp, err := lookercommon.RunInlineQuery(ctx, sdk, wq, "json", t.ApiSettings)
|
||||
if err != nil {
|
||||
logger.DebugContext(ctx, "error querying with new endpoint, trying again with original", err)
|
||||
resp, err = sdk.RunInlineQuery(req, t.ApiSettings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error making query request: %s", err)
|
||||
}
|
||||
return nil, fmt.Errorf("error making query request: %s", err)
|
||||
}
|
||||
|
||||
logger.DebugContext(ctx, "resp = ", resp)
|
||||
|
||||
var data []any
|
||||
|
||||
@@ -126,26 +126,9 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting sdk: %w", err)
|
||||
}
|
||||
req := v4.RequestRunInlineQuery{
|
||||
Body: *wq,
|
||||
ResultFormat: "sql",
|
||||
}
|
||||
req2 := lookercommon.RequestRunInlineQuery2{
|
||||
Query: *wq,
|
||||
RenderOpts: lookercommon.RenderOptions{
|
||||
Format: "sql",
|
||||
},
|
||||
QueryApiClientCtx: lookercommon.QueryApiClientContext{
|
||||
Name: "MCP Toolbox",
|
||||
},
|
||||
}
|
||||
resp, err := lookercommon.RunInlineQuery2(sdk, req2, t.ApiSettings)
|
||||
resp, err := lookercommon.RunInlineQuery(ctx, sdk, wq, "sql", t.ApiSettings)
|
||||
if err != nil {
|
||||
logger.DebugContext(ctx, "error querying with new endpoint, trying again with original", err)
|
||||
resp, err = sdk.RunInlineQuery(req, t.ApiSettings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error making query_sql request: %s", err)
|
||||
}
|
||||
return nil, fmt.Errorf("error making query request: %s", err)
|
||||
}
|
||||
logger.DebugContext(ctx, "resp = ", resp)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user