Update company.go to use new request feature.
This commit is contained in:
parent
8c79fef71a
commit
c6b82d473c
@ -3,7 +3,6 @@ package connectwise
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -146,40 +145,23 @@ type Company struct {
|
|||||||
} `json:"customFields"`
|
} `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.
|
//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.
|
//Will return a pointer to a slice of Company's.
|
||||||
func (cw *Site) GetCompanyByName(companyName string) (*[]Company, error) {
|
func (cw *Site) GetCompanyByName(companyName string) (*[]Company, error) {
|
||||||
restAction := "/company/companies"
|
req := NewRequest(cw, "/company/companies", "GET", nil)
|
||||||
cwurl, err := cw.BuildURL(restAction)
|
req.Parameters["conditions"] = "name=\"" + companyName + "\""
|
||||||
|
err := req.Do()
|
||||||
if err != nil {
|
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{}
|
co := &[]Company{}
|
||||||
parameters.Add("conditions", "name=\""+companyName+"\"")
|
err = json.Unmarshal(req.Body, co)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
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
|
//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{}
|
co := &Company{}
|
||||||
err = co.Unmarshal(&req.Body)
|
err = json.Unmarshal(req.Body, co)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user