2018-06-20 21:59:12 +00:00
|
|
|
package connectwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Callback struct {
|
2018-06-22 20:14:21 +00:00
|
|
|
ID int
|
2018-06-20 21:59:12 +00:00
|
|
|
Description string
|
2018-07-06 01:57:24 +00:00
|
|
|
URL string
|
2018-06-20 21:59:12 +00:00
|
|
|
ObjectId int
|
|
|
|
Type string
|
|
|
|
Level string
|
|
|
|
MemberId int
|
|
|
|
InactiveFlag bool
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
func (cw *ConnectwiseSite) GetCallbacks() (*[]Callback, error) {
|
|
|
|
restAction := "/system/callbacks"
|
|
|
|
cwurl, err := cw.BuildURL(restAction)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return nil, fmt.Errorf("could not build url %s: %s", restAction, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-23 00:47:39 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
body, err := cw.GetRequest(cwurl)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return nil, fmt.Errorf("could not get request %s: %s", cwurl, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-23 00:47:39 +00:00
|
|
|
callbacks := []Callback{}
|
2018-07-06 03:20:52 +00:00
|
|
|
err = json.Unmarshal(body, &callbacks)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-22 20:14:21 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return &callbacks, nil
|
2018-06-20 21:59:12 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
//TBD: This should return something useful, response body??
|
|
|
|
func (cw *ConnectwiseSite) NewCallback(callback Callback) error {
|
|
|
|
restAction := "/system/callbacks"
|
|
|
|
cwurl, err := cw.BuildURL(restAction)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return fmt.Errorf("could not build url %s: %s", restAction, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
|
|
|
jsonCallback, err := json.Marshal(callback)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return fmt.Errorf("could not marshal json data: %s", err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
|
|
|
jsonBuffer := bytes.NewReader(jsonCallback)
|
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
_, err = cw.PostRequest(cwurl, jsonBuffer)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return fmt.Errorf("could not post request %s: %s", cwurl, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
//TBD: This should return something useful, response body??
|
|
|
|
func (cw *ConnectwiseSite) DeleteCallback(callback int) error {
|
|
|
|
restAction := fmt.Sprintf("/system/callbacks/%d", callback)
|
|
|
|
cwurl, err := cw.BuildURL(restAction)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return fmt.Errorf("could not build url %s: %s", restAction, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
|
|
|
_, err = cw.DeleteRequest(cwurl)
|
|
|
|
if err != nil {
|
2018-07-06 14:37:53 +00:00
|
|
|
return fmt.Errorf("could not delete request %s: %s", cwurl, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|