-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
60 lines (49 loc) · 1017 Bytes
/
base.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package provider
import (
"bytes"
"io"
"net/http"
"github.com/jx11r/qnotifier/utils"
)
type Base interface {
Send() error
Fetch() ([]byte, error)
}
type Notifier struct {
Base
API string
Payload []byte
Webhook string
}
func (n *Notifier) Send() error {
req, err := http.NewRequest("POST", n.Webhook, bytes.NewBuffer(n.Payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (n *Notifier) Fetch() ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", n.API, nil)
if err != nil {
return nil, err
}
req.Header = http.Header{
"User-Agent": {"jx11r"},
"Authorization": {"token " + utils.Token["github"]},
"Accept": {"application/vnd.github.v3+json"},
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}