Restructure - using pointer receivers instead of passing *ConnectwiseSite to every single function

This commit is contained in:
Steven Polley 2018-06-22 21:14:08 -06:00
parent ed6df77eb5
commit 3a78d5428a
5 changed files with 35 additions and 44 deletions

View File

@ -145,36 +145,28 @@ type Company struct {
} `json:"customFields"` } `json:"customFields"`
} }
func GetCompanyByName(site *ConnectwiseSite, companyName string) *[]Company { func (cw *ConnectwiseSite) GetCompanyByName(companyName string) *[]Company {
companies := []Company{} companies := []Company{}
//Build the request URL Url := cw.BuildUrl("/company/companies")
var Url *url.URL
Url, err := url.Parse(site.Site)
check(err)
Url.Path += "/company/companies"
parameters := url.Values{} parameters := url.Values{}
parameters.Add("conditions", "name=\""+companyName+"\"") parameters.Add("conditions", "name=\""+companyName+"\"")
Url.RawQuery = parameters.Encode() Url.RawQuery = parameters.Encode()
body := GetRequest(site, Url) body := cw.GetRequest(Url)
check(json.Unmarshal(body, &companies)) check(json.Unmarshal(body, &companies))
return &companies return &companies
} }
func GetCompanyByID(site *ConnectwiseSite, companyID int) *Company { func (cw *ConnectwiseSite) GetCompanyByID(companyID int) *Company {
company := Company{} company := Company{}
//Build the request URL Url := cw.BuildUrl(fmt.Sprintf("/company/companies/%d", companyID))
var Url *url.URL
Url, err := url.Parse(site.Site)
check(err)
Url.Path += fmt.Sprintf("/company/companies/%d", companyID)
body := GetRequest(site, Url) body := cw.GetRequest(Url)
fmt.Print(string(body)) fmt.Print(string(body))
check(json.Unmarshal(body, &company)) check(json.Unmarshal(body, &company))

View File

@ -113,12 +113,12 @@ type Agreement struct {
} `json:"billToSite,omitempty"` } `json:"billToSite,omitempty"`
} }
func GetAgreements(site *ConnectwiseSite) *[]Agreement { func (cw *ConnectwiseSite) GetAgreements() *[]Agreement {
//Build the request URL //Build the request URL
Url := BuildUrl(site, "/finance/agreements") Url := cw.BuildUrl("/finance/agreements")
body := GetRequest(site, Url) body := cw.GetRequest(Url)
agreements := []Agreement{} agreements := []Agreement{}
check(json.Unmarshal(body, &agreements)) check(json.Unmarshal(body, &agreements))
@ -126,11 +126,11 @@ func GetAgreements(site *ConnectwiseSite) *[]Agreement {
} }
func GetBillingCycles(site *ConnectwiseSite) { func (cw *ConnectwiseSite) GetBillingCycles() {
Url := BuildUrl(site, "/finance/billingCycles") Url := cw.BuildUrl("/finance/billingCycles")
body := GetRequest(site, Url) body := cw.GetRequest(Url)
fmt.Print(string(body)) fmt.Print(string(body))
// check(json.Unmarshal(body, &ticket)) // check(json.Unmarshal(body, &ticket))
} }

View File

@ -9,9 +9,9 @@ import (
"net/url" "net/url"
) )
func BuildUrl(site *ConnectwiseSite, restAction string) *url.URL { func (cw *ConnectwiseSite) BuildUrl(restAction string) *url.URL {
var Url *url.URL var Url *url.URL
Url, err := url.Parse(site.Site) Url, err := url.Parse(cw.Site)
check(err) check(err)
Url.Path += restAction Url.Path += restAction
@ -34,11 +34,11 @@ func getHTTPResponseBody(resp *http.Response) []byte {
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //Takes a ConnectwiseSite and request URL, and returns the body of the response
func GetRequest(site *ConnectwiseSite, Url *url.URL) []byte { func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", Url.String(), nil) req, err := http.NewRequest("GET", Url.String(), nil)
check(err) check(err)
req.Header.Set("Authorization", site.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req) response, err := client.Do(req)
check(err) check(err)
@ -48,11 +48,11 @@ func GetRequest(site *ConnectwiseSite, Url *url.URL) []byte {
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //Takes a ConnectwiseSite and request URL, and returns the body of the response
func PostRequest(site *ConnectwiseSite, Url *url.URL, body io.Reader) []byte { func (cw *ConnectwiseSite) PostRequest(Url *url.URL, body io.Reader) []byte {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("POST", Url.String(), body) req, err := http.NewRequest("POST", Url.String(), body)
check(err) check(err)
req.Header.Set("Authorization", site.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req) response, err := client.Do(req)
check(err) check(err)
@ -62,11 +62,11 @@ func PostRequest(site *ConnectwiseSite, Url *url.URL, body io.Reader) []byte {
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //Takes a ConnectwiseSite and request URL, and returns the body of the response
func DeleteRequest(site *ConnectwiseSite, Url *url.URL) []byte { func (cw *ConnectwiseSite) DeleteRequest(Url *url.URL) []byte {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("DELETE", Url.String(), nil) req, err := http.NewRequest("DELETE", Url.String(), nil)
check(err) check(err)
req.Header.Set("Authorization", site.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req) response, err := client.Do(req)
check(err) check(err)

View File

@ -180,22 +180,22 @@ type TimeEntryReference struct {
} }
} }
func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket { func (cw *ConnectwiseSite) GetTicketByID(ticketID int) *Ticket {
Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d", ticketID)) Url := cw.BuildUrl(fmt.Sprintf("/service/tickets/%d", ticketID))
body := GetRequest(site, Url) body := cw.GetRequest(Url)
ticket := Ticket{} ticket := Ticket{}
check(json.Unmarshal(body, &ticket)) check(json.Unmarshal(body, &ticket))
return &ticket return &ticket
} }
func GetTicketTimeEntriesByID(site *ConnectwiseSite, ticketID int) *[]TimeEntryReference { func (cw *ConnectwiseSite) GetTicketTimeEntriesByID(ticketID int) *[]TimeEntryReference {
Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d/timeentries", ticketID)) Url := cw.BuildUrl(fmt.Sprintf("/service/tickets/%d/timeentries", ticketID))
body := GetRequest(site, Url) body := cw.GetRequest(Url)
timeEntryReference := []TimeEntryReference{} timeEntryReference := []TimeEntryReference{}
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference

View File

@ -17,10 +17,10 @@ type Callback struct {
InactiveFlag bool InactiveFlag bool
} }
func GetCallbacks(site *ConnectwiseSite) *[]Callback { func (cw *ConnectwiseSite) GetCallbacks() *[]Callback {
Url := BuildUrl(site, "/system/callbacks") Url := cw.BuildUrl("/system/callbacks")
body := GetRequest(site, Url) body := cw.GetRequest(Url)
callbacks := []Callback{} callbacks := []Callback{}
check(json.Unmarshal(body, &callbacks)) check(json.Unmarshal(body, &callbacks))
@ -30,23 +30,22 @@ func GetCallbacks(site *ConnectwiseSite) *[]Callback {
} }
//TBD: This should return something? //TBD: This should return something?
func NewCallback(site *ConnectwiseSite, callback Callback) { func (cw *ConnectwiseSite) NewCallback(callback Callback) {
Url := BuildUrl(site, "/system/callbacks") Url := cw.BuildUrl("/system/callbacks")
jsonCallback, err := json.Marshal(callback) jsonCallback, err := json.Marshal(callback)
check(err) check(err)
jsonBuffer := bytes.NewReader(jsonCallback) jsonBuffer := bytes.NewReader(jsonCallback)
//body := PostRequest(site, Url, jsonBuffer) cw.PostRequest(Url, jsonBuffer)
PostRequest(site, Url, jsonBuffer)
} }
func DeleteCallback(site *ConnectwiseSite, callback int) { func (cw *ConnectwiseSite) DeleteCallback(callback int) {
Url := BuildUrl(site, fmt.Sprintf("/system/callbacks/%d", callback)) Url := cw.BuildUrl(fmt.Sprintf("/system/callbacks/%d", callback))
body := DeleteRequest(site, Url) body := cw.DeleteRequest(Url)
fmt.Print(string(body)) fmt.Print(string(body))
} }