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

View File

@ -113,12 +113,12 @@ type Agreement struct {
} `json:"billToSite,omitempty"`
}
func GetAgreements(site *ConnectwiseSite) *[]Agreement {
func (cw *ConnectwiseSite) GetAgreements() *[]Agreement {
//Build the request URL
Url := BuildUrl(site, "/finance/agreements")
Url := cw.BuildUrl("/finance/agreements")
body := GetRequest(site, Url)
body := cw.GetRequest(Url)
agreements := []Agreement{}
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))
// check(json.Unmarshal(body, &ticket))
}

View File

@ -9,9 +9,9 @@ import (
"net/url"
)
func BuildUrl(site *ConnectwiseSite, restAction string) *url.URL {
func (cw *ConnectwiseSite) BuildUrl(restAction string) *url.URL {
var Url *url.URL
Url, err := url.Parse(site.Site)
Url, err := url.Parse(cw.Site)
check(err)
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
func GetRequest(site *ConnectwiseSite, Url *url.URL) []byte {
func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", Url.String(), nil)
check(err)
req.Header.Set("Authorization", site.Auth)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
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
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{}
req, err := http.NewRequest("POST", Url.String(), body)
check(err)
req.Header.Set("Authorization", site.Auth)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
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
func DeleteRequest(site *ConnectwiseSite, Url *url.URL) []byte {
func (cw *ConnectwiseSite) DeleteRequest(Url *url.URL) []byte {
client := &http.Client{}
req, err := http.NewRequest("DELETE", Url.String(), nil)
check(err)
req.Header.Set("Authorization", site.Auth)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")
response, err := client.Do(req)
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{}
check(json.Unmarshal(body, &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{}
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference

View File

@ -17,10 +17,10 @@ type Callback struct {
InactiveFlag bool
}
func GetCallbacks(site *ConnectwiseSite) *[]Callback {
func (cw *ConnectwiseSite) GetCallbacks() *[]Callback {
Url := BuildUrl(site, "/system/callbacks")
body := GetRequest(site, Url)
Url := cw.BuildUrl("/system/callbacks")
body := cw.GetRequest(Url)
callbacks := []Callback{}
check(json.Unmarshal(body, &callbacks))
@ -30,23 +30,22 @@ func GetCallbacks(site *ConnectwiseSite) *[]Callback {
}
//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)
check(err)
jsonBuffer := bytes.NewReader(jsonCallback)
//body := PostRequest(site, Url, jsonBuffer)
PostRequest(site, Url, jsonBuffer)
cw.PostRequest(Url, jsonBuffer)
}
func DeleteCallback(site *ConnectwiseSite, callback int) {
func (cw *ConnectwiseSite) DeleteCallback(callback int) {
Url := BuildUrl(site, fmt.Sprintf("/system/callbacks/%d", callback))
body := DeleteRequest(site, Url)
Url := cw.BuildUrl(fmt.Sprintf("/system/callbacks/%d", callback))
body := cw.DeleteRequest(Url)
fmt.Print(string(body))
}