From a1635ed17039f7edbeb4b69bb3f22876c2b18175 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Wed, 20 Jun 2018 15:59:12 -0600 Subject: [PATCH] Began work on webhooks/callback support. Added additional request types. TBD: refactor requests into a generic request type and instead pass whether it is a GET, POST, UPDATE, DELETE, etc request --- 3.0/connectwise/callbacks.go | 64 ++++++++++++++++++++++++++++++++++++ 3.0/connectwise/requests.go | 30 +++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 3.0/connectwise/callbacks.go diff --git a/3.0/connectwise/callbacks.go b/3.0/connectwise/callbacks.go new file mode 100644 index 0000000..1874204 --- /dev/null +++ b/3.0/connectwise/callbacks.go @@ -0,0 +1,64 @@ +package connectwise + +import ( + "bytes" + "encoding/json" + "fmt" + "net/url" +) + +type Callback struct { + Id int + Description string + Url string + ObjectId int + Type string + Level string + MemberId int + InactiveFlag bool +} + +func GetCallbacks(site *ConnectwiseSite) { + + //Build the request URL + var Url *url.URL + Url, err := url.Parse(site.Site) + check(err) + Url.Path += "/system/callbacks" + + body := GetRequest(site, Url) + fmt.Print(string(body)) + // check(json.Unmarshal(body, &ticket)) + +} + +func NewCallback(site *ConnectwiseSite, callback Callback) { + + var Url *url.URL + Url, err := url.Parse(site.Site) + check(err) + Url.Path += "/system/callbacks" + + jsonCallback, err := json.Marshal(callback) + check(err) + + jsonBuffer := bytes.NewReader(jsonCallback) + + body := PostRequest(site, Url, jsonBuffer) + + fmt.Print(string(body)) + +} + +func DeleteCallback(site *ConnectwiseSite, callback int) { + + var Url *url.URL + Url, err := url.Parse(site.Site) + check(err) + Url.Path += fmt.Sprintf("/system/callbacks/%d", callback) + + body := DeleteRequest(site, Url) + + fmt.Print(string(body)) + +} diff --git a/3.0/connectwise/requests.go b/3.0/connectwise/requests.go index 10baee2..a00fe39 100644 --- a/3.0/connectwise/requests.go +++ b/3.0/connectwise/requests.go @@ -2,6 +2,7 @@ package connectwise import ( "fmt" + "io" "io/ioutil" "log" "net/http" @@ -9,6 +10,7 @@ import ( ) //Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice +//TBD: Needs to accept 201 and 204 (returned for Create and Delete operations) func getHTTPResponseBody(resp *http.Response) []byte { if resp.StatusCode != http.StatusOK { out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body) @@ -35,3 +37,31 @@ func GetRequest(site *ConnectwiseSite, Url *url.URL) []byte { return getHTTPResponseBody(response) } + +//Takes a ConnectwiseSite and request URL, and returns the body of the response +func PostRequest(site *ConnectwiseSite, Url *url.URL, body io.Reader) []byte { + client := &http.Client{} + req, err := http.NewRequest("POST", Url.String(), body) + check(err) + req.Header.Set("Authorization", site.Auth) + req.Header.Set("Content-Type", "application/json") + response, err := client.Do(req) + check(err) + defer response.Body.Close() + + return getHTTPResponseBody(response) +} + +//Takes a ConnectwiseSite and request URL, and returns the body of the response +func DeleteRequest(site *ConnectwiseSite, Url *url.URL) []byte { + client := &http.Client{} + req, err := http.NewRequest("DELETE", Url.String(), nil) + check(err) + req.Header.Set("Authorization", site.Auth) + req.Header.Set("Content-Type", "application/json") + response, err := client.Do(req) + check(err) + defer response.Body.Close() + + return getHTTPResponseBody(response) +}