-
Notifications
You must be signed in to change notification settings - Fork 0
/
carmaclient.go
151 lines (122 loc) · 3.04 KB
/
carmaclient.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package carmaclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/moonwalker/logger"
)
const (
timeout = 30 * time.Second
carmaAuthHeader = "X-Carma-Authentication-Token"
)
type CarmaClient struct {
HTTPClient *http.Client
carmaApiToken string
carmaOrganization int64
carmaRESTURL string
carmaControllerURL string
log logger.Logger
common service
Triggers *TriggersService
Lists *ListsService
Properties *PropertiesService
}
type service struct {
client *CarmaClient
}
type carmaResponse struct {
statusCode int
data []byte
err error
msg string
}
type carmaError struct {
Error string `json:"error"`
}
func NewCarmaClient(organization int64, token, restURL, controllerURL string, log logger.Logger) (client *CarmaClient, err error) {
client = &CarmaClient{
HTTPClient: &http.Client{
Timeout: timeout,
},
carmaApiToken: token,
carmaOrganization: organization,
carmaRESTURL: restURL,
carmaControllerURL: controllerURL,
log: log,
}
client.common.client = client
client.Triggers = (*TriggersService)(&client.common)
client.Lists = (*ListsService)(&client.common)
client.Properties = (*PropertiesService)(&client.common)
return
}
func (c CarmaClient) catchError(carmaResp *carmaResponse) {
carmaError := &carmaError{}
err := json.Unmarshal(carmaResp.data, carmaError)
if err != nil {
return
}
carmaResp.err = errors.New(carmaError.Error)
}
func (c CarmaClient) carmaRequest(endpoint string, method string, body interface{}) (carmaResp carmaResponse) {
carmaResp.err = nil
logData := make(map[string]interface{})
url := fmt.Sprintf("%s/%d/%s", c.carmaRESTURL, c.carmaOrganization, endpoint)
logData["query"] = endpoint
logData["URL"] = url
logData["Body"] = body
client := http.Client{
Timeout: 10 * time.Second,
}
b := new(bytes.Buffer)
if body != nil {
json.NewEncoder(b).Encode(body)
}
req, err := http.NewRequest(method, url, b)
if err != nil {
carmaResp.err = err
if c.log != nil {
logData["error"] = err
c.log.Debug("Carma Request", logData)
}
return
}
req.Header[carmaAuthHeader] = []string{c.carmaApiToken}
req.Header["Accept"] = []string{"application/json"}
req.Header["Content-Type"] = []string{"application/json"}
resp, err := client.Do(req)
if err != nil {
carmaResp.err = err
if c.log != nil {
logData["error"] = err
c.log.Debug("Carma Request", logData)
}
return
}
defer resp.Body.Close()
carmaResp.statusCode = resp.StatusCode
respBody, err := ioutil.ReadAll(resp.Body)
carmaResp.err = err
carmaResp.data = respBody
if carmaResp.err != nil {
c.catchError(&carmaResp)
}
var jsonResp interface{}
err = json.Unmarshal(respBody, &jsonResp)
if err == nil {
if len(respBody) > 10000 {
logData["Response"] = fmt.Sprintf("response data to large for log (>%d byte)", 10000)
} else {
logData["Response"] = respBody
}
}
logData["StatusCode"] = resp.StatusCode
if c.log != nil {
c.log.Debug("Carma Request", logData)
}
return
}