2018-06-20 19:24:47 +00:00
|
|
|
package connectwise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-06-20 21:59:12 +00:00
|
|
|
"io"
|
2018-06-20 19:24:47 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-06-20 20:23:49 +00:00
|
|
|
"net/url"
|
2018-06-20 19:24:47 +00:00
|
|
|
)
|
|
|
|
|
2018-06-23 03:14:08 +00:00
|
|
|
func (cw *ConnectwiseSite) BuildUrl(restAction string) *url.URL {
|
2018-06-23 00:47:39 +00:00
|
|
|
var Url *url.URL
|
2018-06-23 03:14:08 +00:00
|
|
|
Url, err := url.Parse(cw.Site)
|
2018-06-23 00:47:39 +00:00
|
|
|
check(err)
|
|
|
|
Url.Path += restAction
|
|
|
|
|
|
|
|
return Url
|
|
|
|
}
|
|
|
|
|
2018-06-20 19:24:47 +00:00
|
|
|
//Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice
|
2018-06-20 21:59:12 +00:00
|
|
|
//TBD: Needs to accept 201 and 204 (returned for Create and Delete operations)
|
2018-06-20 20:23:49 +00:00
|
|
|
func getHTTPResponseBody(resp *http.Response) []byte {
|
2018-06-22 20:14:21 +00:00
|
|
|
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusNoContent) {
|
2018-06-20 19:24:47 +00:00
|
|
|
out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body)
|
|
|
|
log.Fatal(out)
|
|
|
|
return make([]byte, 0)
|
|
|
|
} else {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 20:23:49 +00:00
|
|
|
|
|
|
|
//Takes a ConnectwiseSite and request URL, and returns the body of the response
|
2018-06-23 03:14:08 +00:00
|
|
|
func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
|
2018-06-20 20:23:49 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", Url.String(), nil)
|
|
|
|
check(err)
|
2018-06-23 03:14:08 +00:00
|
|
|
req.Header.Set("Authorization", cw.Auth)
|
2018-06-20 20:23:49 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
response, err := client.Do(req)
|
|
|
|
check(err)
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
return getHTTPResponseBody(response)
|
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
|
|
|
//Takes a ConnectwiseSite and request URL, and returns the body of the response
|
2018-06-23 03:14:08 +00:00
|
|
|
func (cw *ConnectwiseSite) PostRequest(Url *url.URL, body io.Reader) []byte {
|
2018-06-20 21:59:12 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("POST", Url.String(), body)
|
|
|
|
check(err)
|
2018-06-23 03:14:08 +00:00
|
|
|
req.Header.Set("Authorization", cw.Auth)
|
2018-06-20 21:59:12 +00:00
|
|
|
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
|
2018-06-23 03:14:08 +00:00
|
|
|
func (cw *ConnectwiseSite) DeleteRequest(Url *url.URL) []byte {
|
2018-06-20 21:59:12 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("DELETE", Url.String(), nil)
|
|
|
|
check(err)
|
2018-06-23 03:14:08 +00:00
|
|
|
req.Header.Set("Authorization", cw.Auth)
|
2018-06-20 21:59:12 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
response, err := client.Do(req)
|
|
|
|
check(err)
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
return getHTTPResponseBody(response)
|
|
|
|
}
|