opentofu/vendor/github.com/apparentlymart/go-grafana-api/client.go
Martin Atkins 158a90b25b Grafana Provider, with Data Source and Dashboard resources (#6206)
* Grafana provider

* grafana_data_source resource.

Allows data sources to be created in Grafana. Supports all data source
types that are accepted in the current version of Grafana, and will
support any future ones that fit into the existing structure.

* Vendoring of apparentlymart/go-grafana-api

This is in anticipation of adding a Grafana provider plugin.

* grafana_dashboard resource

* Website documentation for the Grafana provider.
2016-05-20 10:20:17 +01:00

57 lines
1.1 KiB
Go

package gapi
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type Client struct {
key string
baseURL url.URL
*http.Client
}
//New creates a new grafana client
//auth can be in user:pass format, or it can be an api key
func New(auth, baseURL string) (*Client, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
key := ""
if strings.Contains(auth, ":") {
split := strings.Split(auth, ":")
u.User = url.UserPassword(split[0], split[1])
} else {
key = fmt.Sprintf("Bearer %s", auth)
}
return &Client{
key,
*u,
&http.Client{},
}, nil
}
func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, error) {
url := c.baseURL
url.Path = path
req, err := http.NewRequest(method, url.String(), body)
if err != nil {
return req, err
}
if c.key != "" {
req.Header.Add("Authorization", c.key)
}
if body == nil {
fmt.Println("request to ", url.String(), "with no body data")
} else {
fmt.Println("request to ", url.String(), "with body data", body.(*bytes.Buffer).String())
}
req.Header.Add("Content-Type", "application/json")
return req, err
}