Create generic BuildUrl function to reduce the need to go through all the net/url crap each time

This commit is contained in:
Steven Polley 2018-06-22 18:47:39 -06:00
parent a66ba7ca89
commit 4eaca267d9
7 changed files with 30 additions and 61 deletions

View File

@ -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

View File

@ -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)

View File

@ -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))

View File

@ -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 {

View File

@ -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

View File

@ -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))
}

View File

@ -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)
}
```