Update company.go to use new request feature.

This commit is contained in:
Steven Polley 2018-07-06 19:40:12 -06:00
parent 8c79fef71a
commit c6b82d473c
1 changed files with 8 additions and 26 deletions

View File

@ -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)
}