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

This commit is contained in:
Steven Polley 2018-06-20 15:59:12 -06:00
parent 480d28baa0
commit a1635ed170
2 changed files with 94 additions and 0 deletions

View File

@ -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))
}

View File

@ -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)
}