From 4eaca267d9534e15509b1b842ae5d600bc47663e Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Fri, 22 Jun 2018 18:47:39 -0600 Subject: [PATCH] Create generic BuildUrl function to reduce the need to go through all the net/url crap each time --- 3.0/connectwise/company.go | 7 ++----- 3.0/connectwise/connectwise.go | 1 + 3.0/connectwise/finance.go | 22 ++++------------------ 3.0/connectwise/requests.go | 9 +++++++++ 3.0/connectwise/service.go | 19 ++++--------------- 3.0/connectwise/system.go | 25 ++++++------------------- README.md | 8 ++++---- 7 files changed, 30 insertions(+), 61 deletions(-) diff --git a/3.0/connectwise/company.go b/3.0/connectwise/company.go index 7fc2272..e58df21 100644 --- a/3.0/connectwise/company.go +++ b/3.0/connectwise/company.go @@ -7,9 +7,6 @@ import ( "time" ) -//Used when more than one company is returned in a response. -type Companies []Company - type Company struct { ID int `json:"id"` Identifier string `json:"identifier"` @@ -148,9 +145,9 @@ type Company struct { } `json:"customFields"` } -func GetCompaniesByName(site *ConnectwiseSite, companyName string) *Companies { +func GetCompanyByName(site *ConnectwiseSite, companyName string) *[]Company { - companies := Companies{} + companies := []Company{} //Build the request URL var Url *url.URL diff --git a/3.0/connectwise/connectwise.go b/3.0/connectwise/connectwise.go index 4bdc722..b950a4e 100644 --- a/3.0/connectwise/connectwise.go +++ b/3.0/connectwise/connectwise.go @@ -19,6 +19,7 @@ func check(err error) { //Returns 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) *ConnectwiseSite { + //The auth string must be formatted in this way when used in requests to the API authString := fmt.Sprintf("%s+%s:%s", company, publicKey, privateKey) authString = base64.StdEncoding.EncodeToString([]byte(authString)) authString = fmt.Sprintf("Basic %s", authString) diff --git a/3.0/connectwise/finance.go b/3.0/connectwise/finance.go index 8482a9e..fd0c563 100644 --- a/3.0/connectwise/finance.go +++ b/3.0/connectwise/finance.go @@ -6,8 +6,6 @@ import ( "net/url" ) -type Agreements []Agreement - type Agreement struct { ID int `json:"id"` Name string `json:"name"` @@ -116,21 +114,13 @@ type Agreement struct { } `json:"billToSite,omitempty"` } -func GetAgreements(site *ConnectwiseSite) *Agreements { - - agreements := Agreements{} +func GetAgreements(site *ConnectwiseSite) *[]Agreement { //Build the request URL - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += "/finance/agreements" - parameters := url.Values{} - parameters.Add("conditions", "billCycleId=2") - parameters.Add("pageSize", "1000") - Url.RawQuery = parameters.Encode() + Url := BuildUrl(site, "/finance/agreements") body := GetRequest(site, Url) + agreements := []Agreement{} check(json.Unmarshal(body, &agreements)) return &agreements @@ -139,11 +129,7 @@ func GetAgreements(site *ConnectwiseSite) *Agreements { func GetBillingCycles(site *ConnectwiseSite) { - //Build the request URL - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += "/finance/billingCycles" + Url := BuildUrl(site, "/finance/billingCycles") body := GetRequest(site, Url) fmt.Print(string(body)) diff --git a/3.0/connectwise/requests.go b/3.0/connectwise/requests.go index 4869009..002b001 100644 --- a/3.0/connectwise/requests.go +++ b/3.0/connectwise/requests.go @@ -9,6 +9,15 @@ import ( "net/url" ) +func BuildUrl(site *ConnectwiseSite, restAction string) *url.URL { + var Url *url.URL + Url, err := url.Parse(site.Site) + check(err) + Url.Path += restAction + + return Url +} + //Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice //TBD: Needs to accept 201 and 204 (returned for Create and Delete operations) func getHTTPResponseBody(resp *http.Response) []byte { diff --git a/3.0/connectwise/service.go b/3.0/connectwise/service.go index d7392b8..c61758f 100644 --- a/3.0/connectwise/service.go +++ b/3.0/connectwise/service.go @@ -7,8 +7,6 @@ import ( "time" ) -type Tickets []Ticket - type Ticket struct { ID int `json:"id"` Summary string `json:"summary"` @@ -185,15 +183,10 @@ type TimeEntryReference struct { func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket { - ticket := Ticket{} - - //Build the request URL - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += fmt.Sprintf("/service/tickets/%d", ticketID) + Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d", ticketID)) body := GetRequest(site, Url) + ticket := Ticket{} check(json.Unmarshal(body, &ticket)) return &ticket @@ -201,14 +194,10 @@ func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket { func GetTicketTimeEntriesByID(site *ConnectwiseSite, ticketID int) *[]TimeEntryReference { - timeEntryReference := []TimeEntryReference{} - - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += fmt.Sprintf("/service/tickets/%d/timeentries", ticketID) + Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d/timeentries", ticketID)) body := GetRequest(site, Url) + timeEntryReference := []TimeEntryReference{} check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference return &timeEntryReference diff --git a/3.0/connectwise/system.go b/3.0/connectwise/system.go index 66a7fc0..8b773de 100644 --- a/3.0/connectwise/system.go +++ b/3.0/connectwise/system.go @@ -20,28 +20,20 @@ type Callback struct { func GetCallbacks(site *ConnectwiseSite) *[]Callback { - callbacks := []Callback{} - - //Build the request URL - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += "/system/callbacks" - + Url := BuildUrl(site, "/system/callbacks") body := GetRequest(site, Url) + + callbacks := []Callback{} check(json.Unmarshal(body, &callbacks)) return &callbacks } +//TBD: This should return something? func NewCallback(site *ConnectwiseSite, callback Callback) { - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += "/system/callbacks" - + Url := BuildUrl(site, "/system/callbacks") jsonCallback, err := json.Marshal(callback) check(err) @@ -54,13 +46,8 @@ func NewCallback(site *ConnectwiseSite, callback Callback) { func DeleteCallback(site *ConnectwiseSite, callback int) { - var Url *url.URL - Url, err := url.Parse(site.Site) - check(err) - Url.Path += fmt.Sprintf("/system/callbacks/%d", callback) - + Url := BuildUrl(site, fmt.Sprintf("/system/callbacks/%d", callback)) body := DeleteRequest(site, Url) - fmt.Print(string(body)) } diff --git a/README.md b/README.md index 0f5f551..d1a2278 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,14 @@ import ( const ( cwSite = "https://yourconnectwisesite.com/v4_6_release/apis/3.0" - cwAPIKeyPrivate = "ASDLFK4ah89ad" - cwAPIKey = "ASLDFKJ2342kl" - cwCompany = "yourcompanyname" + cwAPIKeyPrivate = "ASDLFK4ah89ad" //Put in either your private API key or account password if using user impersonation + cwAPIKey = "ASLDFKJ2342kl" //Put in either your public API key or account username if using user impersonation + cwCompany = "yourcompanyname" //The connectwise company name ) func main() { cw := connectwise.NewSite(cwSite, cwAPIKey, cwAPIKeyPrivate, cwCompany) - companyDataByID := connectwise.GetCompaniesByID(cw, 2) //Retrieves company ID 2 from CW and returns pointer to struct struct + companyDataByID := connectwise.GetCompanyByID(cw, 2) //Retrieves company ID 2 from CW and returns type pointer to a slice of Company's fmt.Println(*companyDataByID) } ```