-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
108 lines (92 loc) · 2.22 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
package opslevel
import (
"fmt"
"os"
"runtime"
"strings"
"time"
)
type ClientSettings struct {
url string
token string
timeout time.Duration
retries int
headers map[string]string
pageSize int // Only Used by GQL
}
type Option func(*ClientSettings)
func newClientSettings(options ...Option) *ClientSettings {
settings := &ClientSettings{
url: "https://app.opslevel.com",
token: os.Getenv("OPSLEVEL_API_TOKEN"),
timeout: time.Second * 10,
retries: 10,
pageSize: 100,
headers: map[string]string{
"User-Agent": buildUserAgent(""),
"GraphQL-Visibility": "public",
},
}
for _, opt := range options {
opt(settings)
}
return settings
}
func SetAPIToken(apiToken string) Option {
return func(c *ClientSettings) {
c.token = apiToken
}
}
func SetURL(url string) Option {
return func(c *ClientSettings) {
c.url = strings.TrimRight(url, "/")
}
}
func SetHeader(key string, value string) Option {
return func(c *ClientSettings) {
c.headers[key] = value
}
}
func SetHeaders(headers map[string]string) Option {
return func(c *ClientSettings) {
for key, value := range headers {
c.headers[key] = value
}
}
}
func SetUserAgentExtra(extra string) Option {
return SetHeader("User-Agent", buildUserAgent(extra))
}
func SetTimeout(amount time.Duration) Option {
return func(c *ClientSettings) {
c.timeout = amount
}
}
func SetMaxRetries(amount int) Option {
return func(c *ClientSettings) {
c.retries = amount
}
}
func SetAPIVisibility(visibility string) Option {
return SetHeader("GraphQL-Visibility", visibility)
}
func SetPageSize(size int) Option {
return func(c *ClientSettings) {
c.pageSize = size
}
}
/*
Return a string suitable for use as a User-Agent header.
The string will be of the form:
<agent_name>/<agent_version> go/<go_ver> <plat_name>/<plat_ver> client/<code_extras> user/<user_extras>
*/
func buildUserAgent(extra string) string {
base := fmt.Sprintf("opslevel-go/%s go/%s %s/%s", clientVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH)
if extra != "" {
base = fmt.Sprintf("%s client/%s", base, extra)
}
if value, present := os.LookupEnv("OPSLEVEL_USER_AGENT_EXTRAS"); present {
base = fmt.Sprintf("%s user/%s", base, value)
}
return base
}