-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira.go
60 lines (52 loc) · 1.03 KB
/
jira.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 main
import (
"encoding/json"
"io"
"net/http"
"time"
)
type Jira struct {
client *http.Client
domain string
email string
token string
}
func NewJira(domain, email, token string) *Jira {
return &Jira{
client: &http.Client{
Timeout: 30 * time.Second,
},
domain: domain,
email: email,
token: token,
}
}
func (j *Jira) request(method, uri string, body io.Reader, v interface{}) (*http.Response, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
req.Header = map[string][]string{
"Accept": {"application/json"},
"Content-Type": {"application/json"},
}
req.SetBasicAuth(j.email, j.token)
res, err := j.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if v == nil {
return res, nil
}
if w, ok := v.(io.Writer); ok {
// Give *bytes.Buffer to get raw bytes instead of decoded struct
io.Copy(w, res.Body)
} else {
err := json.NewDecoder(res.Body).Decode(v)
if err != nil {
return res, nil
}
}
return res, nil
}