commit - 6456042b9dd325c6b9b1e246acac3a21bfae58fe
commit + 514f2cd100380e2d124fa33c56f4e9a9308b907e
blob - 3a84467cec091e7f675eb95959ffdcaac04a5ec6
blob + 3b165dbbfba9ae07b6bf6d4342b515dcc892d2ba
--- http.go
+++ http.go
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
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
}
}
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
}
}
return results.Err()
}
-
-func (c *Client) do(req *http.Request) (*http.Response, error) {
- req.SetBasicAuth(c.username, c.password)
- return c.Do(req)
-}