diff --git a/internal/glance/widget-custom-api.go b/internal/glance/widget-custom-api.go index 1cbbfc6..28a20e0 100644 --- a/internal/glance/widget-custom-api.go +++ b/internal/glance/widget-custom-api.go @@ -36,8 +36,12 @@ type CustomAPIRequest struct { Body any `yaml:"body"` MockResponse string `yaml:"mock-response"` SkipJSONValidation bool `yaml:"skip-json-validation"` - bodyReader io.ReadSeeker `yaml:"-"` - httpRequest *http.Request `yaml:"-"` + BasicAuth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"basic-auth"` + bodyReader io.ReadSeeker `yaml:"-"` + httpRequest *http.Request `yaml:"-"` } type customAPIWidget struct { @@ -190,6 +194,10 @@ func (req *CustomAPIRequest) initialize() error { httpReq.Header.Add(key, value) } + if req.BasicAuth.Username != "" || req.BasicAuth.Password != "" { + httpReq.SetBasicAuth(req.BasicAuth.Username, req.BasicAuth.Password) + } + req.httpRequest = httpReq return nil @@ -708,6 +716,11 @@ var customAPITemplateFuncs = func() template.FuncMap { return req }, + "withBasicAuth": func(username, password string, req *CustomAPIRequest) *CustomAPIRequest { + req.BasicAuth.Username = username + req.BasicAuth.Password = password + return req + }, "getResponse": func(req *CustomAPIRequest) *customAPIResponseData { err := req.initialize() if err != nil {