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:
parent
3c8e0a424f
commit
4e97924700
@ -3,7 +3,6 @@ package connectwise
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -162,17 +161,7 @@ func GetCompaniesByName(site *ConnectwiseSite, companyName string) *Companies {
|
|||||||
parameters.Add("conditions", "name=\""+companyName+"\"")
|
parameters.Add("conditions", "name=\""+companyName+"\"")
|
||||||
Url.RawQuery = parameters.Encode()
|
Url.RawQuery = parameters.Encode()
|
||||||
|
|
||||||
//Build and make the request
|
body := GetRequest(site, Url)
|
||||||
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()
|
|
||||||
|
|
||||||
body := getHTTPResponseBody(response)
|
|
||||||
check(json.Unmarshal(body, &companies))
|
check(json.Unmarshal(body, &companies))
|
||||||
|
|
||||||
return &companies
|
return &companies
|
||||||
@ -188,17 +177,7 @@ func GetCompanyByID(site *ConnectwiseSite, companyID int) *Company {
|
|||||||
check(err)
|
check(err)
|
||||||
Url.Path += fmt.Sprintf("/company/companies/%d", companyID)
|
Url.Path += fmt.Sprintf("/company/companies/%d", companyID)
|
||||||
|
|
||||||
//Build and make the request
|
body := GetRequest(site, Url)
|
||||||
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()
|
|
||||||
|
|
||||||
body := getHTTPResponseBody(response)
|
|
||||||
fmt.Print(string(body))
|
fmt.Print(string(body))
|
||||||
check(json.Unmarshal(body, &company))
|
check(json.Unmarshal(body, &company))
|
||||||
|
|
||||||
|
@ -5,10 +5,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice
|
//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 {
|
if resp.StatusCode != http.StatusOK {
|
||||||
out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body)
|
out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body)
|
||||||
log.Fatal(out)
|
log.Fatal(out)
|
||||||
@ -20,3 +21,17 @@ func getHTTPResponseBody(resp *http.Response) (body []byte) {
|
|||||||
return body
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user