From 7e94afac9c34cbf8d6f28276ee22fe72fbd2365c Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sun, 8 Jul 2018 00:59:32 -0600 Subject: [PATCH] Add CompanyCount method and Count struct to hold return data --- 3.0/connectwise/company.go | 17 +++++++++++++++++ 3.0/connectwise/connectwise.go | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/3.0/connectwise/company.go b/3.0/connectwise/company.go index f87d802..c6837d7 100644 --- a/3.0/connectwise/company.go +++ b/3.0/connectwise/company.go @@ -145,6 +145,23 @@ type Company struct { } `json:"customFields"` } +//CompanyCount returns the number of companies in ConnectWise +func (cw *Site) CompanyCount() (int, error) { + req := NewRequest(cw, "/company/companies/count", "GET", nil) + err := req.Do() + if err != nil { + return 0, fmt.Errorf("request failed for %s: %s", req.RestAction, err) + } + + count := &Count{} + err = json.Unmarshal(req.Body, count) + if err != nil { + return 0, fmt.Errorf("failed to unmarshal body into struct: %s", err) + } + + return count.Count, 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) { diff --git a/3.0/connectwise/connectwise.go b/3.0/connectwise/connectwise.go index 57f2c5d..bc705c3 100644 --- a/3.0/connectwise/connectwise.go +++ b/3.0/connectwise/connectwise.go @@ -11,6 +11,10 @@ type Site struct { Auth string } +type Count struct { + Count int `json:"count"` +} + //NewSite returns a pointer to a ConnectwiseSite struct with the site and auth string available for use in API requests func NewSite(site string, publicKey string, privateKey string, company string) *Site { //The auth string must be formatted in this way when used in requests to the API