From e21f794196fbf9b993262610d858d85a87d54abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Thu, 18 Nov 2021 09:39:20 +0200 Subject: [PATCH] request: permit using no auth --- rest-request.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rest-request.go b/rest-request.go index e473c942..79be05c3 100644 --- a/rest-request.go +++ b/rest-request.go @@ -88,7 +88,7 @@ type StatusMessage struct { // NewClient initializes client for interacting with an instance of Grafana server; // apiKeyOrBasicAuth accepts either 'username:password' basic authentication credentials, -// or a Grafana API key +// or a Grafana API key. If it is an empty string then no authentication is used. func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) (*Client, error) { key := "" basicAuth := strings.Contains(apiKeyOrBasicAuth, ":") @@ -96,12 +96,15 @@ func NewClient(apiURL, apiKeyOrBasicAuth string, client *http.Client) (*Client, if err != nil { return nil, err } - if !basicAuth { - key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth) - } else { - parts := strings.Split(apiKeyOrBasicAuth, ":") - baseURL.User = url.UserPassword(parts[0], parts[1]) + if len(apiKeyOrBasicAuth) > 0 { + if !basicAuth { + key = fmt.Sprintf("Bearer %s", apiKeyOrBasicAuth) + } else { + parts := strings.Split(apiKeyOrBasicAuth, ":") + baseURL.User = url.UserPassword(parts[0], parts[1]) + } } + return &Client{baseURL: baseURL.String(), basicAuth: basicAuth, key: key, client: client}, nil } @@ -143,7 +146,7 @@ func (r *Client) doRequest(ctx context.Context, method, query, rawPath string, p return nil, 0, err } req = req.WithContext(ctx) - if !r.basicAuth { + if !r.basicAuth && len(r.key) > 0 { req.Header.Set("Authorization", r.key) } if r.customHeaders != nil {