Fix GetCompanyByName

Change how request parameters are set
This commit is contained in:
Steven Polley 2018-07-08 09:50:05 -06:00
parent 7e94afac9c
commit 99bf114c75
3 changed files with 15 additions and 17 deletions

View File

@ -163,10 +163,11 @@ func (cw *Site) CompanyCount() (int, error) {
}
//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) {
//Will return a pointer to a Company
func (cw *Site) GetCompanyByName(companyName string) (*Company, error) {
req := NewRequest(cw, "/company/companies", "GET", nil)
req.Parameters["conditions"] = "name=\"" + companyName + "\""
req.URLValues.Add("conditions", "name=\""+companyName+"\"")
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -181,7 +182,8 @@ func (cw *Site) GetCompanyByName(companyName string) (*[]Company, error) {
return nil, fmt.Errorf("ConnectWise returned no results for %s", companyName)
}
return co, nil
//This endpoint always returns a JSON array, but given the condition we apply, we can safely just return the first and only item in the array
return &(*co)[0], nil
}
//GetCompanyByID expects the Connectwise Company ID and returns a pointer to a Company

View File

@ -136,7 +136,8 @@ func (cw *Site) GetAgreements() (*[]Agreement, error) {
//TBD: Pagination and filtering options still need to be considered
func (cw *Site) GetAgreementsByCompanyName(companyName string) (*[]Agreement, error) {
req := NewRequest(cw, "/finance/agreements", "GET", nil)
req.Parameters["conditions"] = "company/name=\"" + companyName + "\""
req.URLValues.Add("conditions", "company/name=\""+companyName+"\"")
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)

View File

@ -12,16 +12,15 @@ import (
type Request struct {
CW *Site
RestAction string
Parameters map[string]string
Method string //GET, POST, DELETE, etc
Body []byte //In a GET request, this is an instance of the struct that the expected json data is to be unmarshaled into
Method string //GET, POST, DELETE, etc
Body []byte //In a GET request, this is an instance of the struct that the expected json data is to be unmarshaled into
URLValues url.Values //Parameters sent to CW for filtering by conditions, page, sorting, etc.
}
//NewRequest is a function which takes the mandatory fields to perform a request to the CW API and returns a pointer to a Request struct
func NewRequest(cw *Site, restAction, method string, body []byte) *Request {
req := Request{CW: cw, RestAction: restAction, Method: method, Body: body}
req.Parameters = make(map[string]string)
req.URLValues = url.Values{}
return &req
}
@ -32,13 +31,9 @@ func (req *Request) Do() error {
return fmt.Errorf("could not build url %s: %s", req.RestAction, err)
}
if len(req.Parameters) > 0 {
parameters := url.Values{}
for key, value := range req.Parameters {
parameters.Add(key, value)
}
cwurl.RawQuery = parameters.Encode()
}
//Does it hurt running this even if it's empty?
cwurl.RawQuery = req.URLValues.Encode()
fmt.Println(cwurl.RawQuery)
client := &http.Client{}
jsonBuffer := bytes.NewReader(req.Body)