-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobit.go
73 lines (57 loc) · 1.33 KB
/
gobit.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
61
62
63
64
65
66
67
68
69
70
71
72
73
package goebi
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/kyokomi/goebi/goebi/notice"
)
// TODO: とりあえずgobrake参考
var defaultHTTPClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, 3*time.Second)
},
ResponseHeaderTimeout: 5 * time.Second,
},
Timeout: 10 * time.Second,
}
// Client is an errbit client.
type Client struct {
client *http.Client
noticeURL string
options Options
}
// New errbitのClientを生成します
func New(opt Options) *Client {
c := Client{}
c.client = defaultHTTPClient
c.noticeURL = opt.createNoticeBaseURL()
c.options = opt
return &c
}
// SendNotice エラー通知します
func (c Client) SendNotice(n notice.Notice) error {
data, err := json.Marshal(n)
if err != nil {
return err
}
u := c.options.createNoticeBaseURL()
res, err := c.client.Post(u, "application/json", bytes.NewReader(data))
if err != nil {
return err
}
defer res.Body.Close()
if !isSuccess(res.StatusCode) {
data, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("error response code %d %s", res.StatusCode, string(data))
}
return nil
}
func isSuccess(statusCode int) bool {
return statusCode >= 200 && statusCode < 300
}