Created method for generic GET request to CW API to handle all the HTTP garbage. Updated methods to use new GetRequest method.
This commit is contained in:
@ -5,10 +5,11 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
//Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice
|
||||
func getHTTPResponseBody(resp *http.Response) (body []byte) {
|
||||
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)
|
||||
log.Fatal(out)
|
||||
@ -20,3 +21,17 @@ func getHTTPResponseBody(resp *http.Response) (body []byte) {
|
||||
return body
|
||||
}
|
||||
}
|
||||
|
||||
//Takes a ConnectwiseSite and request URL, and returns the body of the response
|
||||
func GetRequest(site *ConnectwiseSite, Url *url.URL) []byte {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", 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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user