-
Notifications
You must be signed in to change notification settings - Fork 41
/
issue.go
40 lines (33 loc) · 861 Bytes
/
issue.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"
"net/http"
)
// IssueAPI requires implementing common API for querying and posting issues
// regardless of service that's being used.
type IssueAPI interface {
getIssue(repo string, todo Todo) (map[string]interface{}, error)
postIssue(repo string, todo Todo, body string) (Todo, error)
getHost() string
}
// QueryHTTP makes an API query
func QueryHTTP(req *http.Request) (map[string]interface{}, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return nil, fmt.Errorf("API error: %s", buf.String())
}
var v map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, err
}
return v, err
}