forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
61 lines (51 loc) · 1.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
package opslevel
import (
"context"
"fmt"
"github.com/machinebox/graphql"
)
const defaultURL = "https://api.opslevel.com/graphql"
func NewClient(authToken string, options ...option) *Client {
client := &Client{
url: defaultURL,
bearerToken: fmt.Sprintf("Bearer %s", authToken),
}
for _, opt := range options {
opt(client)
}
client.graphqlClient = graphql.NewClient(client.url)
return client
}
type option func(*Client)
func SetURL(url string) option {
return func(c *Client) {
c.url = url
}
}
type Client struct {
url string
bearerToken string
graphqlClient *graphql.Client
}
func (c *Client) Do(ctx context.Context, query string, params map[string]interface{}, res interface{}) error {
req := graphql.NewRequest(query)
req.Header.Set("Authorization", c.bearerToken)
for key, value := range params {
req.Var(key, value)
}
return c.graphqlClient.Run(ctx, req, res)
}
func handleGraphqlErrs(errs []graphqlError) error {
if len(errs) == 0 {
return nil
}
var errMsg string
for _, err := range errs {
errMsg += fmt.Sprintf("%s path: %s", err.Message, err.Path)
}
return fmt.Errorf("could not create tag: %s", errMsg)
}
type graphqlError struct {
Path []string
Message string
}