-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
59 lines (49 loc) · 1.05 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"os"
)
var (
ErrNoGithubToken = errors.New("No GITHUB_TOKEN specified")
GraphURL = "https://api.github.com/graphql"
)
type Client struct {
client *http.Client
token string
}
type Query struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
func (c *Client) Execute(str string, args map[string]interface{}, target interface{}) error {
jsonQuery, err := json.Marshal(Query{
Query: str,
Variables: args,
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", GraphURL, bytes.NewBuffer(jsonQuery))
if err != nil {
return err
}
req.Header.Set("Authorization", "bearer "+c.token)
resp, err := c.client.Do(req)
if err != nil {
return err
}
return json.NewDecoder(resp.Body).Decode(target)
}
func NewClient() (*Client, error) {
token := os.Getenv("GITHUB_TOKEN")
if len(token) == 0 {
return nil, ErrNoGithubToken
}
return &Client{
token: token,
client: &http.Client{},
}, nil
}