This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
bulkRequest.go
436 lines (386 loc) · 12.3 KB
/
bulkRequest.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"reflect"
"time"
"github.com/kintone-labs/go-kintone"
)
const (
// ConstBulkRequestLimitRecordOption set option: record per bulkRequest
ConstBulkRequestLimitRecordOption = 100
// ConstBulkRequestLimitRequest kintone limited: The request count per bulkRequest
ConstBulkRequestLimitRequest = 20
// ConstRecordsLimitPerRequest kintone limited: The records count per request
ConstRecordsLimitPerRequest = 100
)
// BulkRequestItem item in the bulkRequest array
type BulkRequestItem struct {
Method string `json:"method"`
API string `json:"api"`
Payload interface{} `json:"payload,string"`
}
// BulkRequests BulkRequests structure
type BulkRequests struct {
Requests []*BulkRequestItem `json:"requests,string"`
}
// BulkRequestsError structure
type BulkRequestsError struct {
HTTPStatus string `json:"-"`
HTTPStatusCode int `json:"-"`
Message string `json:"message"` // Human readable message.
ID string `json:"id"` // A unique error ID.
Code string `json:"code"` // For machines.
Errors interface{} `json:"errors"`
}
// BulkRequestsErrors structure
type BulkRequestsErrors struct {
HTTPStatus string `json:"-"`
HTTPStatusCode int `json:"-"`
Results []*BulkRequestsError `json:"results"`
}
// DataResponseBulkPOST structure
type DataResponseBulkPOST struct {
Results []interface{} `json:"results,string"`
}
// DataRequestRecordsPOST structure
type DataRequestRecordsPOST struct {
App uint64 `json:"app,string"`
Records []*kintone.Record `json:"records"`
}
//DataRequestRecordPUT structure
type DataRequestRecordPUT struct {
ID uint64 `json:"id,string"`
Record *kintone.Record `json:"record,string"`
}
// DataRequestRecordPUTByKey structure
type DataRequestRecordPUTByKey struct {
UpdateKey *kintone.UpdateKey `json:"updateKey,string"`
Record *kintone.Record `json:"record,string"`
}
// DataRequestRecordsPUT - Data which will be update in the kintone app
// DataRequestRecordsPUT.Records - array include DataRequestRecordPUTByKey and DataRequestRecordPUT
type DataRequestRecordsPUT struct {
App uint64 `json:"app,string"`
Records []interface{} `json:"records"`
}
// SetRecord set data record for PUT method
func (recordsPut *DataRequestRecordsPUT) SetRecord(record *kintone.Record) {
recordPut := &DataRequestRecordPUT{ID: record.Id(), Record: record}
recordsPut.Records = append(recordsPut.Records, recordPut)
}
// SetRecordWithKey set data record for PUT method
func (recordsPut *DataRequestRecordsPUT) SetRecordWithKey(record *kintone.Record, keyCode string) {
updateKey := &kintone.UpdateKey{FieldCode: keyCode, Field: record.Fields[keyCode].(kintone.UpdateKeyField)}
delete(record.Fields, keyCode)
recordPut := &DataRequestRecordPUTByKey{UpdateKey: updateKey, Record: record}
recordsPut.Records = append(recordsPut.Records, recordPut)
}
// Request bulkRequest with multi method which included only one request
func (bulk *BulkRequests) Request(app *kintone.App) (*DataResponseBulkPOST, interface{}) {
data, _ := json.Marshal(bulk)
req, err := newRequest(app, "POST", "bulkRequest", bytes.NewReader(data))
if err != nil {
return nil, err
}
resp, err := Do(app, req)
if err != nil {
return nil, err
}
body, errParse := parseResponse(resp)
if errParse != nil {
return nil, errParse
}
resp1, err := bulk.Decode(body)
if err != nil {
return nil, kintone.ErrInvalidResponse
}
return resp1, nil
}
// Decode for BulkRequests
func (bulk *BulkRequests) Decode(b []byte) (*DataResponseBulkPOST, error) {
var rsp *DataResponseBulkPOST
err := json.Unmarshal(b, &rsp)
if err != nil {
return nil, errors.New("Invalid JSON format: " + err.Error())
}
return rsp, nil
}
// ImportDataUpdate import data with update
func (bulk *BulkRequests) ImportDataUpdate(app *kintone.App, recordData *kintone.Record, keyField string) error {
bulkReqLength := len(bulk.Requests)
if bulkReqLength > ConstBulkRequestLimitRequest {
return errors.New("The length of bulk request was too large, maximun is " + string(rune(ConstBulkRequestLimitRequest)) + " per request")
}
var dataPUT *DataRequestRecordsPUT
if bulkReqLength > 0 {
for i, bulkReqItem := range bulk.Requests {
if bulkReqItem.Method != "PUT" {
continue
}
// TODO: Check limit 100 record - kintone limit
dataPUT = bulkReqItem.Payload.(*DataRequestRecordsPUT)
if len(dataPUT.Records) == ConstRecordsLimitPerRequest {
continue
}
if keyField != "" {
dataPUT.SetRecordWithKey(recordData, keyField)
} else {
dataPUT.SetRecord(recordData)
}
bulk.Requests[i].Payload = dataPUT
return nil
}
}
recordsUpdate := make([]interface{}, 0)
var recordPUT interface{}
if keyField != "" {
updateKey := &kintone.UpdateKey{FieldCode: keyField, Field: recordData.Fields[keyField].(kintone.UpdateKeyField)}
delete(recordData.Fields, keyField)
recordPUT = &DataRequestRecordPUTByKey{UpdateKey: updateKey, Record: recordData}
} else {
recordPUT = &DataRequestRecordPUT{ID: recordData.Id(), Record: recordData}
}
recordsUpdate = append(recordsUpdate, recordPUT)
dataPUT = &DataRequestRecordsPUT{App: app.AppId, Records: recordsUpdate}
requestPUTRecords := &BulkRequestItem{"PUT", kintoneURLPath("records", app.GuestSpaceId), dataPUT}
bulk.Requests = append(bulk.Requests, requestPUTRecords)
return nil
}
// ImportDataInsert import data with insert only
func (bulk *BulkRequests) ImportDataInsert(app *kintone.App, recordData *kintone.Record) error {
bulkReqLength := len(bulk.Requests)
if bulkReqLength > ConstBulkRequestLimitRequest {
return errors.New("The length of bulk request was too large, maximun is " + string(rune(ConstBulkRequestLimitRequest)) + " per request")
}
var dataPOST *DataRequestRecordsPOST
if bulkReqLength > 0 {
for i, bulkReqItem := range bulk.Requests {
if bulkReqItem.Method != "POST" {
continue
}
dataPOST = bulkReqItem.Payload.(*DataRequestRecordsPOST)
if len(dataPOST.Records) == ConstRecordsLimitPerRequest {
continue
}
// TODO: Check limit 100 record - kintone limit
dataPOST.Records = append(dataPOST.Records, recordData)
bulk.Requests[i].Payload = dataPOST
return nil
}
}
recordsInsert := make([]*kintone.Record, 0)
recordsInsert = append(recordsInsert, recordData)
dataPOST = &DataRequestRecordsPOST{app.AppId, recordsInsert}
requestPostRecords := &BulkRequestItem{"POST", kintoneURLPath("records", app.GuestSpaceId), dataPOST}
bulk.Requests = append(bulk.Requests, requestPostRecords)
return nil
}
// kintoneURLPath get path URL of kintone api
func kintoneURLPath(apiName string, GuestSpaceID uint64) string {
var path string
if GuestSpaceID == 0 {
path = fmt.Sprintf("/k/v1/%s.json", apiName)
} else {
path = fmt.Sprintf("/k/guest/%d/v1/%s.json", GuestSpaceID, apiName)
}
return path
}
func newRequest(app *kintone.App, method, api string, body io.Reader) (*http.Request, error) {
path := kintoneURLPath(api, app.GuestSpaceId)
u := url.URL{
Scheme: "https",
Host: app.Domain,
Path: path,
}
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
if app.HasBasicAuth() {
req.SetBasicAuth(app.GetBasicAuthUser(), app.GetBasicAuthPassword())
}
if len(app.ApiToken) == 0 {
req.Header.Set("X-Cybozu-Authorization", base64.StdEncoding.EncodeToString([]byte(app.User+":"+app.Password)))
} else {
req.Header.Set("X-Cybozu-API-Token", app.ApiToken)
}
if len(app.GetUserAgentHeader()) != 0 {
req.Header.Set("User-Agent", app.GetUserAgentHeader())
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
// Do Request to webservice
func Do(app *kintone.App, req *http.Request) (*http.Response, error) {
if app.Client == nil {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
app.Client = &http.Client{Jar: jar}
}
if app.Timeout == time.Duration(0) {
app.Timeout = kintone.DEFAULT_TIMEOUT
}
type result struct {
resp *http.Response
err error
}
done := make(chan result, 1)
go func() {
resp, err := app.Client.Do(req)
done <- result{resp, err}
}()
type requestCanceler interface {
CancelRequest(*http.Request)
}
select {
case r := <-done:
return r.resp, r.err
case <-time.After(app.Timeout):
if canceller, ok := app.Client.Transport.(requestCanceler); ok {
canceller.CancelRequest(req)
} else {
go func() {
r := <-done
if r.err == nil {
r.resp.Body.Close()
}
}()
}
return nil, kintone.ErrTimeout
}
}
func isJSON(contentType string) bool {
mediatype, _, err := mime.ParseMediaType(contentType)
if err != nil {
return false
}
return mediatype == "application/json"
}
func parseResponse(resp *http.Response) ([]byte, interface{}) {
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
if !isJSON(resp.Header.Get("Content-Type")) {
return nil, &kintone.AppError{
HttpStatus: resp.Status,
HttpStatusCode: resp.StatusCode,
}
}
var appErrorBulkRequest BulkRequestsErrors
appErrorBulkRequest.HTTPStatus = resp.Status
appErrorBulkRequest.HTTPStatusCode = resp.StatusCode
json.Unmarshal(body, &appErrorBulkRequest)
if len(appErrorBulkRequest.Results) == 0 {
var appErrorRequest BulkRequestsError
appErrorRequest.HTTPStatus = resp.Status
appErrorRequest.HTTPStatusCode = resp.StatusCode
json.Unmarshal(body, &appErrorRequest)
return nil, &appErrorRequest
}
return nil, &appErrorBulkRequest
}
return body, nil
}
// ErrorResponse show error detail when the bulkRequest has error(s)
type ErrorResponse struct {
ID string
Code string
Status string
Message string
Errors interface{}
}
func (err *ErrorResponse) show(prefix string) {
fmt.Println("ID: ", err.ID)
fmt.Println("Code: ", err.Code)
if err.Status != "" {
fmt.Println("Status: ", err.Status)
}
fmt.Println("Message: ", err.Message)
fmt.Printf(prefix + "Errors detail: ")
if err.Errors != nil {
fmt.Printf("\n")
for indx, val := range err.Errors.(map[string]interface{}) {
fieldMessage := val.(map[string]interface{})
detailMessage := fieldMessage["messages"].([]interface{})
fmt.Printf("%v '%v': ", prefix, indx)
for i, mess := range detailMessage {
if i > 0 {
fmt.Printf(", ")
}
fmt.Printf(mess.(string))
}
fmt.Printf("\n")
}
fmt.Printf("\n")
} else {
fmt.Printf("(none)\n\n")
}
}
// HandelResponse for bulkRequest
func (bulk *BulkRequests) HandelResponse(rep *DataResponseBulkPOST, err interface{}, lastRowImport, rowNumber uint64) {
if err != nil {
fmt.Printf(" => ERROR OCCURRED\n")
CLIMessage := fmt.Sprintf("ERROR.\nFor error details, please read the details above.\n")
CLIMessage += fmt.Sprintf("Lines %d to %d of the imported file contain errors. Please fix the errors on the file, and re-import it with the flag \"-l %d\"\n", lastRowImport, rowNumber, lastRowImport)
method := map[string]string{"POST": "INSERT", "PUT": "UPDATE"}
methodOccuredError := ""
if reflect.TypeOf(err).String() != "*main.BulkRequestsErrors" {
if reflect.TypeOf(err).String() != "*main.BulkRequestsError" {
fmt.Printf("\n")
fmt.Println(err)
fmt.Printf("\n")
// Reset CLI Message
CLIMessage = ""
} else {
errorResp := &ErrorResponse{}
errorResp.Status = err.(*BulkRequestsError).HTTPStatus
errorResp.Message = err.(*BulkRequestsError).Message
errorResp.Errors = err.(*BulkRequestsError).Errors
errorResp.ID = err.(*BulkRequestsError).ID
errorResp.Code = err.(*BulkRequestsError).Code
errorResp.show("")
}
} else {
errorsResp := err.(*BulkRequestsErrors)
for idx, errorItem := range errorsResp.Results {
if errorItem.Code == "" {
continue
}
errorResp := &ErrorResponse{}
errorResp.ID = errorItem.ID
errorResp.Code = errorItem.Code
errorResp.Status = errorsResp.HTTPStatus
errorResp.Message = errorItem.Message
errorResp.Errors = errorItem.Errors
errorResp.show("")
methodOccuredError = method[bulk.Requests[idx].Method]
}
}
showTimeLog()
fmt.Printf("PROCESS STOPPED!\n\n")
if CLIMessage != "" {
fmt.Println(methodOccuredError, CLIMessage)
}
os.Exit(1)
}
fmt.Println(" => SUCCESS")
}
func showTimeLog() {
fmt.Printf("%v: ", time.Now().Format("[2006-01-02 15:04:05]"))
}