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" "time"
) )
//Company is a struct to hold the unmarshaled JSON data when making a call to the Company API
type Company struct { type Company struct {
ID int `json:"id"` ID int `json:"id"`
Identifier string `json:"identifier"` Identifier string `json:"identifier"`
@ -145,28 +146,32 @@ type Company struct {
} `json:"customFields"` } `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 { func (cw *ConnectwiseSite) GetCompanyByName(companyName string) *[]Company {
companies := []Company{} companies := []Company{}
Url := cw.BuildUrl("/company/companies") cwurl := cw.BuildURL("/company/companies")
parameters := url.Values{} parameters := url.Values{}
parameters.Add("conditions", "name=\""+companyName+"\"") 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)) check(json.Unmarshal(body, &companies))
return &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 { func (cw *ConnectwiseSite) GetCompanyByID(companyID int) *Company {
company := 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)) fmt.Print(string(body))
check(json.Unmarshal(body, &company)) check(json.Unmarshal(body, &company))

View File

@ -6,6 +6,7 @@ import (
"log" "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 { type ConnectwiseSite struct {
Site string Site string
Auth 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 { 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 //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 := fmt.Sprintf("%s+%s:%s", company, publicKey, privateKey)

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
) )
//Agreement is a struct to hold the unmarshaled JSON data when making a call to the Finance API
type Agreement struct { type Agreement struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@ -113,12 +114,14 @@ type Agreement struct {
} `json:"billToSite,omitempty"` } `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 { func (cw *ConnectwiseSite) GetAgreements() *[]Agreement {
//Build the request URL //Build the request URL
Url := cw.BuildUrl("/finance/agreements") cwurl := cw.BuildURL("/finance/agreements")
body := cw.GetRequest(Url) body := cw.GetRequest(cwurl)
agreements := []Agreement{} agreements := []Agreement{}
check(json.Unmarshal(body, &agreements)) 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() { 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)) fmt.Print(string(body))
// check(json.Unmarshal(body, &ticket)) // check(json.Unmarshal(body, &ticket))
} }

View File

@ -9,13 +9,14 @@ import (
"net/url" "net/url"
) )
func (cw *ConnectwiseSite) BuildUrl(restAction string) *url.URL { //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
var Url *url.URL func (cw *ConnectwiseSite) BuildURL(restAction string) *url.URL {
Url, err := url.Parse(cw.Site) var cwurl *url.URL
cwurl, err := url.Parse(cw.Site)
check(err) 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 //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) out := fmt.Sprintf("CW API returned HTTP Status Code %s\n%s", resp.Status, resp.Body)
log.Fatal(out) log.Fatal(out)
return make([]byte, 0) return make([]byte, 0)
} else { }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
check(err) check(err)
return body return body
}
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //GetRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte { func (cw *ConnectwiseSite) GetRequest(cwurl *url.URL) []byte {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", Url.String(), nil) req, err := http.NewRequest("GET", cwurl.String(), nil)
check(err) check(err)
req.Header.Set("Authorization", cw.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -47,10 +48,10 @@ func (cw *ConnectwiseSite) GetRequest(Url *url.URL) []byte {
return getHTTPResponseBody(response) return getHTTPResponseBody(response)
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //PostRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) PostRequest(Url *url.URL, body io.Reader) []byte { func (cw *ConnectwiseSite) PostRequest(cwurl *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", cwurl.String(), body)
check(err) check(err)
req.Header.Set("Authorization", cw.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") 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) return getHTTPResponseBody(response)
} }
//Takes a ConnectwiseSite and request URL, and returns the body of the response //DeleteRequest takes a ConnectwiseSite and request URL, and returns the body of the response
func (cw *ConnectwiseSite) DeleteRequest(Url *url.URL) []byte { func (cw *ConnectwiseSite) DeleteRequest(cwurl *url.URL) []byte {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("DELETE", Url.String(), nil) req, err := http.NewRequest("DELETE", cwurl.String(), nil)
check(err) check(err)
req.Header.Set("Authorization", cw.Auth) req.Header.Set("Authorization", cw.Auth)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")

View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
//Company is a struct to hold the unmarshaled JSON data when making a call to the Service API
type Ticket struct { type Ticket struct {
ID int `json:"id"` ID int `json:"id"`
Summary string `json:"summary"` Summary string `json:"summary"`
@ -171,6 +172,7 @@ type Ticket struct {
ContactPhoneExtension string `json:"contactPhoneExtension,omitempty"` 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 //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 { type TimeEntryReference struct {
ID int 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 { 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{} ticket := Ticket{}
check(json.Unmarshal(body, &ticket)) check(json.Unmarshal(body, &ticket))
return &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 { 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{} timeEntryReference := []TimeEntryReference{}
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference

View File

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

View File

@ -85,7 +85,7 @@ type TimeEntry struct {
func (cw *ConnectwiseSite) GetTimeEntryByID(timeEntryID int) *TimeEntry { 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) body := cw.GetRequest(Url)
fmt.Print(string(body)) fmt.Print(string(body))