-
Notifications
You must be signed in to change notification settings - Fork 1
/
daptin_client_impl.go
210 lines (154 loc) · 5 KB
/
daptin_client_impl.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package daptin_go_client
import (
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
)
type daptinClientImpl struct {
endpoint string
authToken string
restyClient *resty.Client
debug bool
}
func (d daptinClientImpl) SetDebug(b bool) {
d.debug = b
}
func (d daptinClientImpl) nextRequest() *resty.Request {
request := d.restyClient.NewRequest().
SetAuthToken(d.authToken).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json")
if d.debug {
request.EnableTrace()
}
return request
}
func (d daptinClientImpl) FindOne(tableName string, referenceId string, parameters DaptinQueryParameters) (JsonApiObject, error) {
request := d.nextRequest()
var responseObject JsonApiObject
url := d.endpoint + "/api/" + tableName + "/" + referenceId
if parameters != nil {
for key, value := range parameters {
url = url + key + "=" + fmt.Sprintf("%v", value) + "&"
}
}
response, err := request.Get(url)
if d.debug {
d.LogTraceInfo(err, response)
}
if err != nil {
return nil, err
}
bodyBytes := response.Body()
err = json.Unmarshal(bodyBytes, &responseObject)
return ToJsonApiObject(responseObject["data"].(map[string]interface{})), err
}
func (d daptinClientImpl) FindAll(tableName string, parameters DaptinQueryParameters) ([]JsonApiObject, error) {
request := d.nextRequest()
var responseObject JsonApiObject
url := d.endpoint + "/api/" + tableName + "?"
for key, parameter := range parameters {
url = url + key + "=" + fmt.Sprintf("%v", parameter) + "&"
}
response, err := request.Get(url)
if d.debug {
d.LogTraceInfo(err, response)
}
if err != nil {
return nil, err
}
bodyBytes := response.Body()
err = json.Unmarshal(bodyBytes, &responseObject)
return ToJsonApiObjectArray(responseObject["data"].([]interface{})), err
}
func ToJsonApiObjectArray(object []interface{}) []JsonApiObject {
out := make([]JsonApiObject, len(object))
for i, o := range object {
out[i] = ToJsonApiObject(o.(map[string]interface{}))
}
return out
}
func ToJsonApiObject(object map[string]interface{}) JsonApiObject {
return object
}
func (d daptinClientImpl) Create(tableName string, attributes JsonApiObject) (JsonApiObject, error) {
request := d.nextRequest()
var responseObject JsonApiObject
response, err := request.SetBody(attributes).Post(d.endpoint + "/api/" + tableName)
if d.debug {
d.LogTraceInfo(err, response)
}
if err != nil {
return nil, err
}
bodyBytes := response.Body()
err = json.Unmarshal(bodyBytes, &responseObject)
return ToJsonApiObject(responseObject["data"].(map[string]interface{})), err
}
func (d daptinClientImpl) Update(tableName, referenceId string, object JsonApiObject) (JsonApiObject, error) {
request := d.nextRequest()
var responseObject JsonApiObject
response, err := request.SetBody(object).Patch(d.endpoint + "/api/" + tableName + "/" + referenceId)
if d.debug {
d.LogTraceInfo(err, response)
}
if err != nil {
return nil, err
}
bodyBytes := response.Body()
err = json.Unmarshal(bodyBytes, &responseObject)
return ToJsonApiObject(responseObject["data"].(map[string]interface{})), err
}
func (d daptinClientImpl) Delete(tableName string, referenceId string) error {
request := d.nextRequest()
//var responseObject JsonApiObject
response, err := request.Delete(d.endpoint + "/api/" + tableName + "/" + referenceId)
if d.debug {
d.LogTraceInfo(err, response)
}
return err
}
func (d daptinClientImpl) Execute(actionName string, tableName string, attributes JsonApiObject) ([]DaptinActionResponse, error) {
request := d.nextRequest()
var responseObject []DaptinActionResponse
actionAttributes := map[string]interface{}{
"Name": actionName,
"OnType": tableName,
"Attributes": attributes,
}
response, err := request.SetBody(actionAttributes).Post(d.endpoint + "/action/" + tableName + "/" + actionName)
if d.debug {
d.LogTraceInfo(err, response)
}
if err != nil {
return nil, err
}
bodyBytes := response.Body()
err = json.Unmarshal(bodyBytes, &responseObject)
return responseObject, err
}
func (d daptinClientImpl) LogTraceInfo(err error, resp *resty.Response) {
// Explore response object
fmt.Println("Response Info:")
fmt.Println("Error :", err)
fmt.Println("Status Code:", resp.StatusCode())
fmt.Println("Status :", resp.Status())
fmt.Println("Proto :", resp.Proto())
fmt.Println("Time :", resp.Time())
fmt.Println("Received At:", resp.ReceivedAt())
fmt.Println("Body :\n", resp)
fmt.Println()
// Explore trace info
fmt.Println("Request Trace Info:")
ti := resp.Request.TraceInfo()
fmt.Println("DNSLookup :", ti.DNSLookup)
fmt.Println("ConnTime :", ti.ConnTime)
fmt.Println("TCPConnTime :", ti.TCPConnTime)
fmt.Println("TLSHandshake :", ti.TLSHandshake)
fmt.Println("ServerTime :", ti.ServerTime)
fmt.Println("ResponseTime :", ti.ResponseTime)
fmt.Println("TotalTime :", ti.TotalTime)
fmt.Println("IsConnReused :", ti.IsConnReused)
fmt.Println("IsConnWasIdle:", ti.IsConnWasIdle)
fmt.Println("ConnIdleTime :", ti.ConnIdleTime)
}