-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.go
165 lines (133 loc) · 3.64 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package api
import (
"bytes"
"context"
"fmt"
graphql "github.com/cli/shurcooL-graphql"
"io"
"net"
"net/http"
"net/url"
"strings"
)
const defaultUserAgent = "Humio-go-client/unknown"
type Client struct {
config Config
httpTransport *http.Transport
}
type Config struct {
Address *url.URL
UserAgent string
Token string
CACertificatePEM string
Insecure bool
ProxyOrganization string
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
}
func DefaultConfig() Config {
config := Config{}
return config
}
func (c *Client) Address() *url.URL {
return c.config.Address
}
func (c *Client) Token() string {
return c.config.Token
}
func (c *Client) CACertificate() string {
return c.config.CACertificatePEM
}
func (c *Client) Insecure() bool {
return c.config.Insecure
}
func (c *Client) Config() Config {
return c.config
}
func NewClient(config Config) *Client {
httpTransport := NewHttpTransport(config)
return NewClientWithTransport(config, httpTransport)
}
func NewClientWithTransport(config Config, httpTransport *http.Transport) *Client {
if config.Address != nil && !strings.HasSuffix(config.Address.Path, "/") {
config.Address.Path = config.Address.Path + "/"
}
if config.UserAgent == "" {
config.UserAgent = defaultUserAgent
}
return &Client{
config: config,
httpTransport: httpTransport,
}
}
func (c *Client) headers() map[string]string {
headers := map[string]string{}
if c.Token() != "" {
headers["Authorization"] = fmt.Sprintf("Bearer %s", c.Token())
}
if c.config.ProxyOrganization != "" {
headers["ProxyOrganization"] = c.config.ProxyOrganization
}
if c.config.UserAgent != "" {
headers["User-Agent"] = c.config.UserAgent
}
return headers
}
func (c *Client) newGraphQLClient() (*graphql.Client, error) {
httpClient := c.newHTTPClientWithHeaders(c.headers())
graphqlURL, err := c.Address().Parse("graphql")
if err != nil {
return nil, err
}
return graphql.NewClient(graphqlURL.String(), httpClient), nil
}
func (c *Client) Query(query interface{}, variables map[string]interface{}) error {
client, err := c.newGraphQLClient()
if err != nil {
return err
}
return client.Query(context.Background(), query, variables)
}
func (c *Client) Mutate(mutation interface{}, variables map[string]interface{}) error {
client, err := c.newGraphQLClient()
if err != nil {
return err
}
return client.Mutate(context.Background(), mutation, variables)
}
// JSONContentType is "application/json"
const JSONContentType string = "application/json"
const ZIPContentType string = "application/zip"
func (c *Client) HTTPRequest(httpMethod string, path string, body io.Reader) (*http.Response, error) {
return c.HTTPRequestContext(context.Background(), httpMethod, path, body, JSONContentType)
}
func (c *Client) HTTPRequestContext(ctx context.Context, httpMethod string, path string, body io.Reader, contentType string) (*http.Response, error) {
if body == nil {
body = bytes.NewReader(nil)
}
url, err := c.Address().Parse(path)
if err != nil {
return nil, err
}
req, reqErr := http.NewRequestWithContext(ctx, httpMethod, url.String(), body)
if reqErr != nil {
return nil, reqErr
}
headers := c.headers()
headers["Content-Type"] = contentType
var client = c.newHTTPClientWithHeaders(headers)
return client.Do(req)
}
func optBoolArg(v *bool) *graphql.Boolean {
var argPtr *graphql.Boolean
if v != nil {
argPtr = graphql.NewBoolean(graphql.Boolean(*v))
}
return argPtr
}
func optStringArg(v *string) *graphql.String {
var argPtr *graphql.String
if v != nil {
argPtr = graphql.NewString(graphql.String(*v))
}
return argPtr
}