Various formatting, documentation and style changes.

This commit is contained in:
Steven Polley 2018-06-28 19:35:34 -06:00
parent bcca1e872e
commit e88572ea4c
7 changed files with 53 additions and 37 deletions

View File

@ -7,6 +7,7 @@ import (
"time"
)
//Company is a struct to hold the unmarshaled JSON data when making a call to the Company API
type Company struct {
ID int `json:"id"`
Identifier string `json:"identifier"`
@ -145,28 +146,32 @@ type Company struct {
} `json:"customFields"`
}
//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 *ConnectwiseSite) GetCompanyByName(companyName string) *[]Company {
companies := []Company{}
Url := cw.BuildUrl("/company/companies")
cwurl := cw.BuildURL("/company/companies")
parameters := url.Values{}
parameters.Add("conditions", "name=\""+companyName+"\"")
Url.RawQuery = parameters.Encode()
cwurl.RawQuery = parameters.Encode()
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
check(json.Unmarshal(body, &companies))
return &companies
}
//GetCompanyByID expects the Connectwise Company ID and returns a pointer to a Company
//Does not return a slice like GetCompanyByName as the ID will only ever have one match
func (cw *ConnectwiseSite) GetCompanyByID(companyID int) *Company {
company := Company{}
Url := cw.BuildUrl(fmt.Sprintf("/company/companies/%d", companyID))
cwurl := cw.BuildURL(fmt.Sprintf("/company/companies/%d", companyID))
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
fmt.Print(string(body))
check(json.Unmarshal(body, &company))

View File

@ -6,6 +6,7 @@ import (
"log"
)
//ConnectwiseSite is a stuct containing the URL of the site and the API authorization token in the format that CW expects it.
type ConnectwiseSite struct {
Site string
Auth string
@ -17,7 +18,7 @@ func check(err error) {
}
}
//Returns a ConnectwiseSite struct with the site and auth string available for use in API requests
//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) *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)

View File

@ -5,6 +5,7 @@ import (
"fmt"
)
//Agreement is a struct to hold the unmarshaled JSON data when making a call to the Finance API
type Agreement struct {
ID int `json:"id"`
Name string `json:"name"`
@ -113,12 +114,14 @@ type Agreement struct {
} `json:"billToSite,omitempty"`
}
//GetAgreements returns a list of agreements, not paginated and currently not that usefule
//TBD: Pagination and filtering options still need to be considered
func (cw *ConnectwiseSite) GetAgreements() *[]Agreement {
//Build the request URL
Url := cw.BuildUrl("/finance/agreements")
cwurl := cw.BuildURL("/finance/agreements")
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
agreements := []Agreement{}
check(json.Unmarshal(body, &agreements))
@ -126,11 +129,13 @@ func (cw *ConnectwiseSite) GetAgreements() *[]Agreement {
}
//GetBillingCycles is not complete
//TBD: Finish this.
func (cw *ConnectwiseSite) GetBillingCycles() {
Url := cw.BuildUrl("/finance/billingCycles")
cwurl := cw.BuildURL("/finance/billingCycles")
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
fmt.Print(string(body))
// check(json.Unmarshal(body, &ticket))
}

View File

@ -9,13 +9,14 @@ import (
"net/url"
)
func (cw *ConnectwiseSite) BuildUrl(restAction string) *url.URL {
var Url *url.URL
Url, err := url.Parse(cw.Site)
//BuildURL will take a REST action such as "/companies/company/5" and then append the CW site to it and return a pointer to a url.URL
func (cw *ConnectwiseSite) BuildURL(restAction string) *url.URL {
var cwurl *url.URL
cwurl, err := url.Parse(cw.Site)
check(err)
Url.Path += restAction
cwurl.Path += restAction
return Url
return cwurl
}
//Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice
@ -25,18 +26,18 @@ func getHTTPResponseBody(resp *http.Response) []byte {
out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body)
log.Fatal(out)
return make([]byte, 0)
} else {
body, err := ioutil.ReadAll(resp.Body)
check(err)
return body
}
body, err := ioutil.ReadAll(resp.Body)
check(err)
return body
}
//Takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
//GetRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) GetRequest(cwurl *url.URL) []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", Url.String(), nil)
req, err := http.NewRequest("GET", cwurl.String(), nil)
check(err)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")
@ -47,10 +48,10 @@ func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
return getHTTPResponseBody(response)
}
//Takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) PostRequest(Url *url.URL, body io.Reader) []byte {
//PostRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) PostRequest(cwurl *url.URL, body io.Reader) []byte {
client := &http.Client{}
req, err := http.NewRequest("POST", Url.String(), body)
req, err := http.NewRequest("POST", cwurl.String(), body)
check(err)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")
@ -61,10 +62,10 @@ func (cw *ConnectwiseSite) PostRequest(Url *url.URL, body io.Reader) []byte {
return getHTTPResponseBody(response)
}
//Takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) DeleteRequest(Url *url.URL) []byte {
//DeleteRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) DeleteRequest(cwurl *url.URL) []byte {
client := &http.Client{}
req, err := http.NewRequest("DELETE", Url.String(), nil)
req, err := http.NewRequest("DELETE", cwurl.String(), nil)
check(err)
req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json")

View File

@ -6,6 +6,7 @@ import (
"time"
)
//Company is a struct to hold the unmarshaled JSON data when making a call to the Service API
type Ticket struct {
ID int `json:"id"`
Summary string `json:"summary"`
@ -171,6 +172,7 @@ type Ticket struct {
ContactPhoneExtension string `json:"contactPhoneExtension,omitempty"`
}
//TimeEntryReference is a struct to hold the unmarshaled JSON data when making a call to the Service API
//TBD: For some reason the Info struct contained in TimeEntryReference does get data when the JSON is unmarshaled into this struct. The ID works fine
type TimeEntryReference struct {
ID int
@ -180,22 +182,24 @@ type TimeEntryReference struct {
}
}
//GetTicketByID expects a ticket ID and returns a pointer to a Ticket struct
func (cw *ConnectwiseSite) GetTicketByID(ticketID int) *Ticket {
Url := cw.BuildUrl(fmt.Sprintf("/service/tickets/%d", ticketID))
cwurl := cw.BuildURL(fmt.Sprintf("/service/tickets/%d", ticketID))
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
ticket := Ticket{}
check(json.Unmarshal(body, &ticket))
return &ticket
}
//GetTicketTimeEntriesByID expects a ticket ID and returns a pointer a to a slice of TimeEntryReference's, all the time entries attached to that ticket
func (cw *ConnectwiseSite) GetTicketTimeEntriesByID(ticketID int) *[]TimeEntryReference {
Url := cw.BuildUrl(fmt.Sprintf("/service/tickets/%d/timeentries", ticketID))
cwurl := cw.BuildURL(fmt.Sprintf("/service/tickets/%d/timeentries", ticketID))
body := cw.GetRequest(Url)
body := cw.GetRequest(cwurl)
timeEntryReference := []TimeEntryReference{}
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference

View File

@ -19,7 +19,7 @@ type Callback struct {
func (cw *ConnectwiseSite) GetCallbacks() *[]Callback {
Url := cw.BuildUrl("/system/callbacks")
Url := cw.BuildURL("/system/callbacks")
body := cw.GetRequest(Url)
callbacks := []Callback{}
@ -32,7 +32,7 @@ func (cw *ConnectwiseSite) GetCallbacks() *[]Callback {
//TBD: This should return something?
func (cw *ConnectwiseSite) NewCallback(callback Callback) {
Url := cw.BuildUrl("/system/callbacks")
Url := cw.BuildURL("/system/callbacks")
jsonCallback, err := json.Marshal(callback)
check(err)
@ -44,7 +44,7 @@ func (cw *ConnectwiseSite) NewCallback(callback Callback) {
func (cw *ConnectwiseSite) DeleteCallback(callback int) {
Url := cw.BuildUrl(fmt.Sprintf("/system/callbacks/%d", callback))
Url := cw.BuildURL(fmt.Sprintf("/system/callbacks/%d", callback))
body := cw.DeleteRequest(Url)
fmt.Print(string(body))

View File

@ -85,7 +85,7 @@ type TimeEntry struct {
func (cw *ConnectwiseSite) GetTimeEntryByID(timeEntryID int) *TimeEntry {
Url := cw.BuildUrl(fmt.Sprintf("/time/entries/%d", timeEntryID))
Url := cw.BuildURL(fmt.Sprintf("/time/entries/%d", timeEntryID))
body := cw.GetRequest(Url)
fmt.Print(string(body))