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:
Dr. Strangelove
2025-09-26 14:11:50 -04:00
committed by GitHub
parent 67d8221a2e
commit 62af39d751
3 changed files with 31 additions and 38 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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)