Skip to content

Commit 7ffe6b3

Browse files
committed
Only send data for update|read|delete_data when there is data to send
1 parent d01ce49 commit 7ffe6b3

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

restapi/api_object.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,16 @@ func (obj *APIObject) readObject() error {
339339
getPath = fmt.Sprintf("%s?%s", obj.getPath, obj.queryString)
340340
}
341341

342-
b := []byte{}
343-
readData, _ := json.Marshal(obj.readData)
344-
if string(readData) != "" {
342+
send := ""
343+
if len(obj.readData) > 0 {
344+
readData, _ := json.Marshal(obj.readData)
345+
send = string(readData)
345346
if obj.debug {
346-
log.Printf("api_object.go: Using read data '%s'", string(readData))
347+
log.Printf("api_object.go: Using read data '%s'", send)
347348
}
348-
b = readData
349349
}
350350

351-
resultString, err := obj.apiClient.sendRequest(obj.readMethod, strings.Replace(getPath, "{id}", obj.id, -1), string(b))
351+
resultString, err := obj.apiClient.sendRequest(obj.readMethod, strings.Replace(getPath, "{id}", obj.id, -1), send)
352352
if err != nil {
353353
if strings.Contains(err.Error(), "unexpected response code '404'") {
354354
log.Printf("api_object.go: 404 error while refreshing state for '%s' at path '%s'. Removing from state.", obj.id, obj.getPath)
@@ -390,14 +390,16 @@ func (obj *APIObject) updateObject() error {
390390
return fmt.Errorf("cannot update an object unless the ID has been set")
391391
}
392392

393-
b, _ := json.Marshal(obj.data)
394-
395-
updateData, _ := json.Marshal(obj.updateData)
396-
if string(updateData) != "{}" {
393+
send := ""
394+
if len(obj.updateData) > 0 {
395+
updateData, _ := json.Marshal(obj.updateData)
396+
send = string(updateData)
397397
if obj.debug {
398-
log.Printf("api_object.go: Using update data '%s'", string(updateData))
398+
log.Printf("api_object.go: Using update data '%s'", send)
399399
}
400-
b = updateData
400+
} else {
401+
b, _ := json.Marshal(obj.data)
402+
send = string(b)
401403
}
402404

403405
putPath := obj.putPath
@@ -408,7 +410,7 @@ func (obj *APIObject) updateObject() error {
408410
putPath = fmt.Sprintf("%s?%s", obj.putPath, obj.queryString)
409411
}
410412

411-
resultString, err := obj.apiClient.sendRequest(obj.updateMethod, strings.Replace(putPath, "{id}", obj.id, -1), string(b))
413+
resultString, err := obj.apiClient.sendRequest(obj.updateMethod, strings.Replace(putPath, "{id}", obj.id, -1), send)
412414
if err != nil {
413415
return err
414416
}
@@ -441,16 +443,16 @@ func (obj *APIObject) deleteObject() error {
441443
deletePath = fmt.Sprintf("%s?%s", obj.deletePath, obj.queryString)
442444
}
443445

444-
b := []byte{}
445-
destroyData, _ := json.Marshal(obj.destroyData)
446-
if string(destroyData) != "{}" {
446+
send := ""
447+
if len(obj.destroyData) > 0 {
448+
destroyData, _ := json.Marshal(obj.destroyData)
449+
send = string(destroyData)
447450
if obj.debug {
448451
log.Printf("api_object.go: Using destroy data '%s'", string(destroyData))
449452
}
450-
b = destroyData
451453
}
452454

453-
_, err := obj.apiClient.sendRequest(obj.destroyMethod, strings.Replace(deletePath, "{id}", obj.id, -1), string(b))
455+
_, err := obj.apiClient.sendRequest(obj.destroyMethod, strings.Replace(deletePath, "{id}", obj.id, -1), send)
454456
if err != nil {
455457
return err
456458
}

0 commit comments

Comments
 (0)