2018-06-20 21:59:12 +00:00
|
|
|
package connectwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2018-07-07 00:26:11 +00:00
|
|
|
//Callback is a struct to hold the unmarshaled JSON data when making a call to the System API
|
2018-06-20 21:59:12 +00:00
|
|
|
type Callback struct {
|
2018-07-07 23:18:59 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
ObjectID int `json:"objectId"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Level string `json:"level"`
|
|
|
|
MemberID int `json:"memberId"`
|
|
|
|
InactiveFlag bool `json:"inactiveFlag"`
|
|
|
|
Info struct {
|
|
|
|
LastUpdated string `json:"lastUpdated"`
|
|
|
|
UpdatedBy string `json:"updatedBy"`
|
|
|
|
} `json:"_info"`
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 02:48:02 +00:00
|
|
|
//GetSystemMembers returns a slice of Member structs containing all the members (users) of connectwise
|
|
|
|
//TBD finish this, I don't have permissions with my API key to see the JSON data
|
2018-07-09 00:02:29 +00:00
|
|
|
func (cw *Site) GetSystemMembers() error {
|
|
|
|
req := cw.NewRequest("/system/members", "GET", nil)
|
|
|
|
err := req.Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("request failed for %s: %s", req.RestAction, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callbacks := &[]Callback{}
|
|
|
|
err = json.Unmarshal(req.Body, callbacks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-07 00:26:11 +00:00
|
|
|
//GetCallbacks returns a slice of Callback structs containing all the callbacks currently registered with ConnectWise
|
2018-07-07 00:29:53 +00:00
|
|
|
func (cw *Site) GetCallbacks() (*[]Callback, error) {
|
2018-07-08 21:16:59 +00:00
|
|
|
req := cw.NewRequest("/system/callbacks", "GET", nil)
|
2018-07-07 23:18:59 +00:00
|
|
|
err := req.Do()
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-07 23:18:59 +00:00
|
|
|
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-23 00:47:39 +00:00
|
|
|
|
2018-07-07 23:18:59 +00:00
|
|
|
callbacks := &[]Callback{}
|
|
|
|
err = json.Unmarshal(req.Body, callbacks)
|
2018-07-06 03:20:52 +00:00
|
|
|
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-07 23:18:59 +00:00
|
|
|
return callbacks, nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 00:26:11 +00:00
|
|
|
//NewCallback expects a Callback struct and will register a new callback with Connectwise
|
2018-07-06 03:20:52 +00:00
|
|
|
//TBD: This should return something useful, response body??
|
2018-07-07 23:18:59 +00:00
|
|
|
func (cw *Site) NewCallback(callback *Callback) (*Callback, error) {
|
|
|
|
jsonCallback, err := json.Marshal(callback)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-07 23:18:59 +00:00
|
|
|
return nil, 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
|
|
|
|
2018-07-08 21:16:59 +00:00
|
|
|
req := cw.NewRequest("/system/callbacks", "POST", jsonCallback)
|
2018-07-07 23:18:59 +00:00
|
|
|
err = req.Do()
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-07 23:18:59 +00:00
|
|
|
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-07 23:18:59 +00:00
|
|
|
callback = &Callback{}
|
|
|
|
err = json.Unmarshal(req.Body, callback)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-07 23:18:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
2018-07-06 03:20:52 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-07 23:18:59 +00:00
|
|
|
return callback, nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 00:26:11 +00:00
|
|
|
//DeleteCallback expects the ID of an existing callback and will unregister it with ConnectWise
|
2018-07-07 23:18:59 +00:00
|
|
|
//Does not return anything - CW gives an empty response body
|
2018-07-07 00:29:53 +00:00
|
|
|
func (cw *Site) DeleteCallback(callback int) error {
|
2018-07-08 21:16:59 +00:00
|
|
|
req := cw.NewRequest(fmt.Sprintf("/system/callbacks/%d", callback), "DELETE", nil)
|
2018-07-07 23:18:59 +00:00
|
|
|
err := req.Do()
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
2018-07-07 23:18:59 +00:00
|
|
|
return fmt.Errorf("request failed for %s: %s", req.RestAction, 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
|
|
|
}
|