From 99bf114c7583bd93b41a49c35a9cd3b3b445ee1e Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sun, 8 Jul 2018 09:50:05 -0600 Subject: [PATCH] Fix GetCompanyByName Change how request parameters are set --- 3.0/connectwise/company.go | 10 ++++++---- 3.0/connectwise/finance.go | 3 ++- 3.0/connectwise/requests.go | 19 +++++++------------ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/3.0/connectwise/company.go b/3.0/connectwise/company.go index c6837d7..8956cd8 100644 --- a/3.0/connectwise/company.go +++ b/3.0/connectwise/company.go @@ -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 diff --git a/3.0/connectwise/finance.go b/3.0/connectwise/finance.go index 8a9cdca..a385fc5 100644 --- a/3.0/connectwise/finance.go +++ b/3.0/connectwise/finance.go @@ -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) diff --git a/3.0/connectwise/requests.go b/3.0/connectwise/requests.go index 09a406c..cef59bc 100644 --- a/3.0/connectwise/requests.go +++ b/3.0/connectwise/requests.go @@ -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)