-
Notifications
You must be signed in to change notification settings - Fork 21
/
http.go
63 lines (55 loc) · 1.23 KB
/
http.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
package dashboard
import (
"context"
"encoding/json"
"log"
"net/http"
"time"
gh "github.com/google/go-github/v50/github"
)
var allowRequestEvery = time.Second / 30
var throttle <-chan time.Time = time.Tick(allowRequestEvery)
func logHTTP(method, url string, f func()) {
log.Println("==> ", method, url)
start := time.Now()
f()
log.Println("==> ", method, url, "finished in", time.Since(start))
}
func get(url string, data interface{}) error {
<-throttle
var resp *http.Response
var err error
logHTTP("GET", url, func() {
resp, err = http.Get(url)
})
if err != nil {
return err
}
return json.NewDecoder(resp.Body).Decode(data)
}
func doGraphql(client *gh.Client, query string, variables map[string]interface{}, output interface{}) error {
req, err := githubClient.NewRequest(
"POST",
"/graphql",
map[string]interface{}{"query": query, "variables": variables},
)
if err != nil {
return err
}
logHTTP(req.Method, req.URL.String(), func() {
_, err = githubClient.Do(context.Background(), req, output)
})
return err
}
func getRetry(retries int, url string, data interface{}) error {
var err error
tries := 0
for tries <= retries {
tries += 1
err = get(url, data)
if err == nil {
break
}
}
return err
}