1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00
gorocket/hooks.go
Thomas Buchinger c0295b8976 Do not exit the program on failed API Calls, return an error instead
Using log.Fatal() calls os.Exit(1) after the log message is printed.
os.Exit cannot be catched and exits the program without giving the
user a chance to recover from the error.
2022-03-07 13:19:29 +01:00

64 lines
1.1 KiB
Go

package gorocket
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type HookMessage struct {
Text string `json:"text"`
Attachments []HookAttachment `json:"attachments"`
}
type HookAttachment struct {
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
Color string `json:"color"`
}
type HookResponse struct {
Success bool `json:"success"`
}
func (c *Client) Hooks(msg *HookMessage, token string) (*HookResponse, error) {
opt, _ := json.Marshal(msg)
url := fmt.Sprintf("%s/hooks/%s", c.baseURL, token)
req, err := http.NewRequest("POST",
url,
bytes.NewBuffer(opt))
fmt.Println(url)
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if err != nil {
log.Println("Request error")
return nil, err
}
res, err := c.HTTPClient.Do(req)
defer res.Body.Close()
if err != nil {
log.Println(err)
return nil, err
}
resp := HookResponse{}
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, err
}
return &resp, nil
}