58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package amclient
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type Client struct {
|
|
addr string
|
|
apiKey string
|
|
}
|
|
|
|
// addr is the full address, for example: https://x.io/report.
|
|
func New(addr, apiKey string) Client {
|
|
return Client{addr, apiKey}
|
|
}
|
|
|
|
func (cl Client) post(values url.Values) error {
|
|
resp, err := http.PostForm(cl.addr, 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{
|
|
"key": []string{cl.apiKey},
|
|
"action": []string{"ping"},
|
|
})
|
|
}
|
|
|
|
func (cl Client) Log(text string) error {
|
|
return cl.post(url.Values{
|
|
"key": []string{cl.apiKey},
|
|
"action": []string{"log"},
|
|
"text": []string{text},
|
|
})
|
|
}
|
|
|
|
func (cl Client) Alert(text string) error {
|
|
return cl.post(url.Values{
|
|
"key": []string{cl.apiKey},
|
|
"action": []string{"alert"},
|
|
"text": []string{text},
|
|
})
|
|
}
|