-
Notifications
You must be signed in to change notification settings - Fork 0
/
cirrus.go
63 lines (51 loc) · 1.36 KB
/
cirrus.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 cirrus
import (
"fmt"
"github.com/Khan/genqlient/graphql"
"net/http"
"os"
)
// NewClient returns a new Cirrus API client.
func NewClient(cirrusToken string) graphql.Client {
var err error
defer func() {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
var httpClient http.Client
if cirrusToken == "" {
// based on cirrus: https://cirrus-ci.org/api/#authorization
// if you only need read access to public repositories of your organization you can skip this step and don't provide Authorization header.
httpClient = http.Client{
Transport: &anonymousTransport{
wrapped: http.DefaultTransport,
},
}
} else {
httpClient = http.Client{
Transport: &authedTransport{
key: cirrusToken,
wrapped: http.DefaultTransport,
},
}
}
graphqlClient := graphql.NewClient("https://api.cirrus-ci.com/graphql", &httpClient)
return graphqlClient
}
type authedTransport struct {
key string
wrapped http.RoundTripper
}
func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "bearer "+t.key)
return t.wrapped.RoundTrip(req)
}
type anonymousTransport struct {
wrapped http.RoundTripper
}
func (t *anonymousTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.wrapped.RoundTrip(req)
}
//go:generate go run github.com/Khan/genqlient genqlient.yaml