commit 514f2cd100380e2d124fa33c56f4e9a9308b907e
parent 6456042b9dd325c6b9b1e246acac3a21bfae58fe
Author: Oliver Lowe <o@olowe.co>
Date: Fri, 7 Jan 2022 19:37:02 +1100
Export NewRequest
So that people using the package can make their own requests
for anything unimplemented by the package itself.
Diffstat:
| M | http.go | | | 65 | +++++++++++++++++++++++++++++++++-------------------------------- |
1 file changed, 33 insertions(+), 32 deletions(-)
diff --git a/http.go b/http.go
@@ -25,25 +25,9 @@ type result struct {
var ErrNoObject = errors.New("no such object")
-func (res results) Err() error {
- if len(res.Results) == 0 {
- return nil
- }
- var errs []string
- for _, r := range res.Results {
- if len(r.Errors) == 0 {
- continue
- }
- errs = append(errs, strings.Join(r.Errors, ", "))
- }
- if len(errs) == 0 {
- return nil
- }
- return errors.New(strings.Join(errs, ", "))
-}
-
-func newRequest(method, host, path string, body io.Reader) (*http.Request, error) {
- url := "https://" + host + versionPrefix + path
+// NewRequest returns an authenticated HTTP request with appropriate header
+// for sending to an Icinga2 server.
+func NewRequest(method, url, username, password string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
@@ -59,31 +43,52 @@ func newRequest(method, host, path string, body io.Reader) (*http.Request, error
default:
return nil, fmt.Errorf("new request: unsupported method %s", req.Method)
}
+ req.SetBasicAuth(username, password)
return req, nil
}
+func (res results) Err() error {
+ if len(res.Results) == 0 {
+ return nil
+ }
+ var errs []string
+ for _, r := range res.Results {
+ if len(r.Errors) == 0 {
+ continue
+ }
+ errs = append(errs, strings.Join(r.Errors, ", "))
+ }
+ if len(errs) == 0 {
+ return nil
+ }
+ return errors.New(strings.Join(errs, ", "))
+}
+
func (c *Client) get(path string) (*http.Response, error) {
- req, err := newRequest(http.MethodGet, c.addr, path, nil)
+ url := "https://" + c.addr + versionPrefix + path
+ req, err := NewRequest(http.MethodGet, url, c.username, c.password, nil)
if err != nil {
return nil, err
}
- return c.do(req)
+ return c.Do(req)
}
func (c *Client) post(path string, body io.Reader) (*http.Response, error) {
- req, err := newRequest(http.MethodPost, c.addr, path, body)
+ url := "https://" + c.addr + versionPrefix + path
+ req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
if err != nil {
return nil, err
}
- return c.do(req)
+ return c.Do(req)
}
func (c *Client) put(path string, body io.Reader) error {
- req, err := newRequest(http.MethodPut, c.addr, path, body)
+ url := "https://" + c.addr + versionPrefix + path
+ req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
if err != nil {
return err
}
- resp, err := c.do(req)
+ resp, err := c.Do(req)
if err != nil {
return err
}
@@ -99,11 +104,12 @@ func (c *Client) put(path string, body io.Reader) error {
}
func (c *Client) delete(path string) error {
- req, err := newRequest(http.MethodDelete, c.addr, path, nil)
+ url := "https://" + c.addr + versionPrefix + path
+ req, err := NewRequest(http.MethodPost, url, c.username, c.password, body)
if err != nil {
return err
}
- resp, err := c.do(req)
+ resp, err := c.Do(req)
if err != nil {
return err
}
@@ -119,8 +125,3 @@ func (c *Client) delete(path string) error {
}
return results.Err()
}
-
-func (c *Client) do(req *http.Request) (*http.Response, error) {
- req.SetBasicAuth(c.username, c.password)
- return c.Do(req)
-}