forked from ton-connect/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher.go
40 lines (36 loc) · 891 Bytes
/
pusher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/tonkeeper/bridge/config"
"net/http"
)
type WebhookData struct {
Topic string `json:"topic"`
Hash string `json:"hash"`
}
func SendWebhook(clientID string, body WebhookData) error {
if config.Config.WebhookURL == "" {
return nil
}
postBody, _ := json.Marshal(body)
req, err := http.NewRequest(http.MethodPost, config.Config.WebhookURL+"/"+clientID, bytes.NewReader(postBody))
if err != nil {
log.Errorf("failed to init request: %v", err)
return err
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("failed send request: %v", err)
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("bad status code: %v", res.StatusCode)
return err
}
return nil
}