-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoql.go
51 lines (46 loc) · 1005 Bytes
/
coql.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
package zoho
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"net/http"
)
type selectResponse[T any] struct {
Data []T `json:"data"`
}
func Select[V any](query string) ([]V, error) {
b, err := json.Marshal(&selectQuery{Query: query})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://www.zohoapis.eu/crm/v4/coql", bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Zoho-oauthtoken "+auth.AccessToken)
req.Header.Set("Content-Type", "text/plain")
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode == http.StatusNoContent {
return nil, nil
}
if r.StatusCode != http.StatusOK {
b, err = io.ReadAll(r.Body)
if err != nil {
return nil, err
}
log.Println(string(b))
return nil, errors.New(r.Status)
}
var res selectResponse[V]
err = json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return nil, err
}
return res.Data, nil
}