-
Notifications
You must be signed in to change notification settings - Fork 5
/
graphclient.go
45 lines (35 loc) · 1.17 KB
/
graphclient.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
package requests
import (
"context"
cst "github.com/DelineaXPM/dsv-cli/constants"
"github.com/DelineaXPM/dsv-cli/errors"
"github.com/DelineaXPM/dsv-cli/format"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"github.com/shurcooL/graphql"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o ../tests/fake/fake_graph_client.go . GraphClient
type GraphClient interface {
DoRequest(uri string, query interface{}, variables map[string]interface{}) ([]byte, *errors.ApiError)
}
type graphClient struct{}
func NewGraphClient() GraphClient {
return &graphClient{}
}
func (c *graphClient) DoRequest(uri string, query interface{}, variables map[string]interface{}) ([]byte, *errors.ApiError) {
src := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: viper.GetString(cst.NounToken),
},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := graphql.NewClient(uri, httpClient)
if err := client.Query(context.Background(), query, variables); err != nil {
return nil, errors.NewS(err.Error())
}
resp, err := format.JsonMarshal(&query)
if err != nil {
return nil, errors.New(err).Grow("Failed to marshal response")
}
return resp, nil
}