2022-11-03 13:44:37 -05:00
|
|
|
package client
|
2022-06-23 07:48:16 -05:00
|
|
|
|
|
|
|
import (
|
2023-10-16 09:40:04 -05:00
|
|
|
"context"
|
2022-06-23 07:48:16 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
2024-03-11 11:22:33 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data/utils/maputil"
|
2023-01-30 02:38:51 -06:00
|
|
|
|
2023-10-20 08:54:27 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
2024-02-01 13:16:07 -06:00
|
|
|
|
2024-03-11 11:22:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/promlib/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/promlib/utils"
|
2022-06-23 07:48:16 -05:00
|
|
|
)
|
|
|
|
|
2024-03-11 11:22:33 -05:00
|
|
|
// CreateTransportOptions creates options for the http client.
|
2024-02-07 04:01:09 -06:00
|
|
|
func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, logger log.Logger) (*sdkhttpclient.Options, error) {
|
2023-10-16 09:40:04 -05:00
|
|
|
opts, err := settings.HTTPClientOptions(ctx)
|
2022-06-23 07:48:16 -05:00
|
|
|
if err != nil {
|
2023-02-02 10:58:10 -06:00
|
|
|
return nil, fmt.Errorf("error getting HTTP options: %w", err)
|
2022-06-23 07:48:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonData, err := utils.GetJsonData(settings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
|
|
}
|
|
|
|
httpMethod, _ := maputil.GetStringOptional(jsonData, "httpMethod")
|
|
|
|
|
|
|
|
opts.Middlewares = middlewares(logger, httpMethod)
|
|
|
|
|
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewares(logger log.Logger, httpMethod string) []sdkhttpclient.Middleware {
|
|
|
|
middlewares := []sdkhttpclient.Middleware{
|
|
|
|
// TODO: probably isn't needed anymore and should by done by http infra code
|
|
|
|
middleware.CustomQueryParameters(logger),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed to control GET vs POST method of the requests
|
|
|
|
if strings.ToLower(httpMethod) == "get" {
|
|
|
|
middlewares = append(middlewares, middleware.ForceHttpGet(logger))
|
|
|
|
}
|
|
|
|
|
|
|
|
return middlewares
|
|
|
|
}
|