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"
|
|
|
|
"net/http"
|
2018-06-20 20:23:49 +00:00
|
|
|
"net/url"
|
2018-06-20 19:24:47 +00:00
|
|
|
)
|
|
|
|
|
2018-06-29 01:35:34 +00:00
|
|
|
//BuildURL will take a REST action such as "/companies/company/5" and then append the CW site to it and return a pointer to a url.URL
|
2018-07-06 03:20:52 +00:00
|
|
|
func (cw *ConnectwiseSite) BuildURL(restAction string) (*url.URL, error) {
|
2018-06-29 01:35:34 +00:00
|
|
|
var cwurl *url.URL
|
|
|
|
cwurl, err := url.Parse(cw.Site)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not %s as url: %g", cw.Site, err)
|
|
|
|
}
|
2018-06-29 01:35:34 +00:00
|
|
|
cwurl.Path += restAction
|
2018-06-23 00:47:39 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return cwurl, nil
|
2018-06-23 00:47:39 +00:00
|
|
|
}
|
|
|
|
|
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-07-06 03:20:52 +00:00
|
|
|
func getHTTPResponseBody(resp *http.Response) ([]byte, error) {
|
2018-06-22 20:14:21 +00:00
|
|
|
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusNoContent) {
|
2018-07-06 03:20:52 +00:00
|
|
|
return nil, fmt.Errorf("cw api returned unexpected http status %i - %s", resp.StatusCode, resp.Status)
|
2018-06-20 19:24:47 +00:00
|
|
|
}
|
2018-07-06 03:20:52 +00:00
|
|
|
defer resp.Body.Close()
|
2018-06-29 01:35:34 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read response body of request")
|
|
|
|
}
|
2018-06-29 01:35:34 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return body, nil
|
2018-06-20 19:24:47 +00:00
|
|
|
}
|
2018-06-20 20:23:49 +00:00
|
|
|
|
2018-06-29 01:35:34 +00:00
|
|
|
//GetRequest takes a ConnectwiseSite and request URL, and returns the body of the response
|
2018-07-06 03:20:52 +00:00
|
|
|
func (cw *ConnectwiseSite) GetRequest(cwurl *url.URL) ([]byte, error) {
|
2018-06-20 20:23:49 +00:00
|
|
|
client := &http.Client{}
|
2018-06-29 01:35:34 +00:00
|
|
|
req, err := http.NewRequest("GET", cwurl.String(), nil)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not construct http request: %g", 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)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not perform http get request: %g", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := getHTTPResponseBody(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get http response body: %g", err)
|
|
|
|
}
|
2018-06-20 20:23:49 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return body, nil
|
2018-06-20 20:23:49 +00:00
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-06-29 01:35:34 +00:00
|
|
|
//PostRequest takes a ConnectwiseSite and request URL, and returns the body of the response
|
2018-07-06 03:20:52 +00:00
|
|
|
func (cw *ConnectwiseSite) PostRequest(cwurl *url.URL, reqBody io.Reader) ([]byte, error) {
|
2018-06-20 21:59:12 +00:00
|
|
|
client := &http.Client{}
|
2018-07-06 03:20:52 +00:00
|
|
|
req, err := http.NewRequest("POST", cwurl.String(), reqBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not construct http request: %g", 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)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not perform http post request: %g", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := getHTTPResponseBody(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get http response body: %g", err)
|
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return body, nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 01:35:34 +00:00
|
|
|
//DeleteRequest takes a ConnectwiseSite and request URL, and returns the body of the response
|
2018-07-06 03:20:52 +00:00
|
|
|
func (cw *ConnectwiseSite) DeleteRequest(cwurl *url.URL) ([]byte, error) {
|
2018-06-20 21:59:12 +00:00
|
|
|
client := &http.Client{}
|
2018-06-29 01:35:34 +00:00
|
|
|
req, err := http.NewRequest("DELETE", cwurl.String(), nil)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not construct http request: %g", 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)
|
2018-07-06 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not perform http delete request: %g", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := getHTTPResponseBody(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get http response body: %g", err)
|
|
|
|
}
|
2018-06-20 21:59:12 +00:00
|
|
|
|
2018-07-06 03:20:52 +00:00
|
|
|
return body, nil
|
2018-06-20 21:59:12 +00:00
|
|
|
}
|