package amclient import ( "fmt" "io/ioutil" "log" "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 { if cl.reportURL == "" { log.Printf("AM ping") return nil } return cl.post(url.Values{ "action": []string{"ping"}, }) } func (cl Client) Log(text string, args ...interface{}) error { text = fmt.Sprintf(text, args...) if cl.reportURL == "" { log.Printf("AM log: %s", text) return nil } return cl.post(url.Values{ "action": []string{"log"}, "text": []string{text}, }) } func (cl Client) Alert(text string, args ...interface{}) error { text = fmt.Sprintf(text, args...) if cl.reportURL == "" { log.Printf("AM alert: %s", text) return nil } return cl.post(url.Values{ "action": []string{"alert"}, "text": []string{text}, }) }