-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
55 lines (46 loc) · 1.08 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
package monday
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"github.com/grokify/goauth"
"github.com/grokify/mogo/net/httputilmore"
)
const (
MondayAPIURL = "https://api.monday.com/v2"
)
type Client struct {
httpClient *http.Client
}
func NewClient(token string) Client {
return Client{httpClient: goauth.NewClientAuthzTokenSimple("", token)}
}
func (c *Client) DoJSON(data []byte) (*http.Response, error) {
if c.httpClient == nil {
return nil, errors.New("no auth token")
}
req, err := http.NewRequest(
http.MethodPost,
MondayAPIURL,
bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Add(httputilmore.HeaderContentType, httputilmore.ContentTypeAppJSONUtf8)
return c.httpClient.Do(req)
}
func (c *Client) DoGraphQLString(gql string) (*http.Response, error) {
req := QueryRequest{Query: gql}
data, err := json.Marshal(req)
if err != nil {
return nil, err
}
return c.DoJSON(data)
}
func (c *Client) DoGraphQL(gql Query) (*http.Response, error) {
return c.DoGraphQLString(gql.String())
}
type QueryRequest struct {
Query string `json:"query"`
}