-
Notifications
You must be signed in to change notification settings - Fork 13
/
clientGQL.go
117 lines (98 loc) · 3.26 KB
/
clientGQL.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
package opslevel
import (
"context"
"fmt"
"github.com/hashicorp/go-retryablehttp"
"github.com/hasura/go-graphql-client"
"golang.org/x/oauth2"
"net/http"
"os"
"strings"
)
type Client struct {
pageSize graphql.Int
client *graphql.Client
}
// Deprecated: Use NewGQLClient instead
func NewClient(apiToken string, options ...Option) *Client {
options = append(options, SetAPIToken(apiToken))
return NewGQLClient(options...)
}
func NewGQLClient(options ...Option) *Client {
settings := newClientSettings(options...)
httpToken := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("OPSLEVEL_API_TOKEN"), TokenType: "Bearer"},
)
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = settings.retries
retryClient.Logger = nil
standardClient := retryClient.StandardClient()
standardClient.Timeout = settings.timeout
standardClient.Transport = &oauth2.Transport{
Source: httpToken,
Base: standardClient.Transport,
}
var url string
if strings.Contains(settings.url, "/LOCAL_TESTING/") {
url = settings.url
} else {
url = fmt.Sprintf("%s/graphql", settings.url)
}
modifier := graphql.RequestModifier(
func(r *http.Request) {
for key, value := range settings.headers {
r.Header.Add(key, value)
}
})
return &Client{
pageSize: graphql.Int(settings.pageSize),
client: graphql.NewClient(url, standardClient).WithRequestModifier(modifier),
}
}
func (client *Client) InitialPageVariables() PayloadVariables {
return PayloadVariables{
"after": "",
"first": client.pageSize,
}
}
func (client *Client) InitialPageVariablesPointer() *PayloadVariables {
v := PayloadVariables{
"after": "",
"first": client.pageSize,
}
return &v
}
func (client *Client) Query(q interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.QueryCTX(context.Background(), q, variables, options...)
}
func (client *Client) QueryCTX(ctx context.Context, q interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.client.Query(ctx, q, variables, options...)
}
func (client *Client) Mutate(m interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.MutateCTX(context.Background(), m, variables, options...)
}
func (client *Client) MutateCTX(ctx context.Context, m interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.client.Mutate(ctx, m, variables, options...)
}
func (client *Client) ExecRaw(q string, variables map[string]interface{}, options ...graphql.Option) ([]byte, error) {
return client.ExecRawCTX(context.Background(), q, variables, options...)
}
func (client *Client) ExecRawCTX(ctx context.Context, q string, variables map[string]interface{}, options ...graphql.Option) ([]byte, error) {
return client.client.ExecRaw(ctx, q, variables, options...)
}
func (client *Client) Validate() error {
var q struct {
Account struct {
Id ID
}
}
err := client.Query(&q, nil)
// TODO: we should probably use a custom OpsLevelClientError type - https://www.digitalocean.com/community/tutorials/creating-custom-errors-in-go
if err != nil {
return fmt.Errorf("client validation error: %s", err.Error())
}
return nil
}
func WithName(name string) graphql.Option {
return graphql.OperationName(name)
}