From c6b82d473c360d883a37f344524f11abeaeecfe8 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Fri, 6 Jul 2018 19:40:12 -0600 Subject: [PATCH] Update company.go to use new request feature. --- 3.0/connectwise/company.go | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/3.0/connectwise/company.go b/3.0/connectwise/company.go index 0f6688e..d708c10 100644 --- a/3.0/connectwise/company.go +++ b/3.0/connectwise/company.go @@ -3,7 +3,6 @@ package connectwise import ( "encoding/json" "fmt" - "net/url" "time" ) @@ -146,40 +145,23 @@ type Company struct { } `json:"customFields"` } -//Unmarshal is a method which exists for each struct for data returned by or posted to the ConnectWise API -//It is responsible for Unmarshaling the JSON data into the struct -func (co *Company) Unmarshal(data *[]byte) error { - err := json.Unmarshal(*data, co) - if err != nil { - return fmt.Errorf("failed to unmarshal data into company struct: %s", err) - } - return nil -} - //GetCompanyByName expects an exact match, perhaps an improvement could be made to support wildcard characters. //Will return a pointer to a slice of Company's. func (cw *Site) GetCompanyByName(companyName string) (*[]Company, error) { - restAction := "/company/companies" - cwurl, err := cw.BuildURL(restAction) + req := NewRequest(cw, "/company/companies", "GET", nil) + req.Parameters["conditions"] = "name=\"" + companyName + "\"" + err := req.Do() if err != nil { - return nil, fmt.Errorf("could not build url %s: %s", restAction, err) + return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err) } - parameters := url.Values{} - parameters.Add("conditions", "name=\""+companyName+"\"") - cwurl.RawQuery = parameters.Encode() - - body, err := cw.GetRequest(cwurl) - if err != nil { - return nil, fmt.Errorf("could not get request %s: %s", cwurl, err) - } - companies := []Company{} - err = json.Unmarshal(body, &companies) + co := &[]Company{} + err = json.Unmarshal(req.Body, co) if err != nil { return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err) } - return &companies, nil + return co, nil } //GetCompanyByID expects the Connectwise Company ID and returns a pointer to a Company @@ -192,7 +174,7 @@ func (cw *Site) GetCompanyByID(companyID int) (*Company, error) { } co := &Company{} - err = co.Unmarshal(&req.Body) + err = json.Unmarshal(req.Body, co) if err != nil { return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err) }