This repository has been archived on 2019-06-27. You can view files and clone it, but cannot push or open issues/pull-requests.
am/amclient/client.go

57 lines
1.0 KiB
Go

package amclient
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type Client struct {
reportURL string
}
// addr is the full address, for example: https://x.io/report.
func New(reportURL string) Client {
return Client{reportURL}
}
func (client Client) post(values url.Values) error {
cl := http.Client{Timeout: 30 * time.Second}
resp, err := cl.PostForm(client.reportURL, values)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("POST failed: [%d] %s", resp.StatusCode, body)
}
return nil
}
func (cl Client) Ping() error {
return cl.post(url.Values{
"action": []string{"ping"},
})
}
func (cl Client) Log(text string) error {
return cl.post(url.Values{
"action": []string{"log"},
"text": []string{text},
})
}
func (cl Client) Alert(text string) error {
return cl.post(url.Values{
"action": []string{"alert"},
"text": []string{text},
})
}