Create generic BuildUrl function to reduce the need to go through all the net/url crap each time
This commit is contained in:
parent
a66ba7ca89
commit
4eaca267d9
@ -7,9 +7,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Used when more than one company is returned in a response.
|
|
||||||
type Companies []Company
|
|
||||||
|
|
||||||
type Company struct {
|
type Company struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Identifier string `json:"identifier"`
|
Identifier string `json:"identifier"`
|
||||||
@ -148,9 +145,9 @@ type Company struct {
|
|||||||
} `json:"customFields"`
|
} `json:"customFields"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCompaniesByName(site *ConnectwiseSite, companyName string) *Companies {
|
func GetCompanyByName(site *ConnectwiseSite, companyName string) *[]Company {
|
||||||
|
|
||||||
companies := Companies{}
|
companies := []Company{}
|
||||||
|
|
||||||
//Build the request URL
|
//Build the request URL
|
||||||
var Url *url.URL
|
var Url *url.URL
|
||||||
|
@ -19,6 +19,7 @@ func check(err error) {
|
|||||||
|
|
||||||
//Returns a ConnectwiseSite struct with the site and auth string available for use in API requests
|
//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 {
|
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 := fmt.Sprintf("%s+%s:%s", company, publicKey, privateKey)
|
||||||
authString = base64.StdEncoding.EncodeToString([]byte(authString))
|
authString = base64.StdEncoding.EncodeToString([]byte(authString))
|
||||||
authString = fmt.Sprintf("Basic %s", authString)
|
authString = fmt.Sprintf("Basic %s", authString)
|
||||||
|
@ -6,8 +6,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Agreements []Agreement
|
|
||||||
|
|
||||||
type Agreement struct {
|
type Agreement struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -116,21 +114,13 @@ type Agreement struct {
|
|||||||
} `json:"billToSite,omitempty"`
|
} `json:"billToSite,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAgreements(site *ConnectwiseSite) *Agreements {
|
func GetAgreements(site *ConnectwiseSite) *[]Agreement {
|
||||||
|
|
||||||
agreements := Agreements{}
|
|
||||||
|
|
||||||
//Build the request URL
|
//Build the request URL
|
||||||
var Url *url.URL
|
Url := BuildUrl(site, "/finance/agreements")
|
||||||
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()
|
|
||||||
|
|
||||||
body := GetRequest(site, Url)
|
body := GetRequest(site, Url)
|
||||||
|
agreements := []Agreement{}
|
||||||
check(json.Unmarshal(body, &agreements))
|
check(json.Unmarshal(body, &agreements))
|
||||||
|
|
||||||
return &agreements
|
return &agreements
|
||||||
@ -139,11 +129,7 @@ func GetAgreements(site *ConnectwiseSite) *Agreements {
|
|||||||
|
|
||||||
func GetBillingCycles(site *ConnectwiseSite) {
|
func GetBillingCycles(site *ConnectwiseSite) {
|
||||||
|
|
||||||
//Build the request URL
|
Url := BuildUrl(site, "/finance/billingCycles")
|
||||||
var Url *url.URL
|
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += "/finance/billingCycles"
|
|
||||||
|
|
||||||
body := GetRequest(site, Url)
|
body := GetRequest(site, Url)
|
||||||
fmt.Print(string(body))
|
fmt.Print(string(body))
|
||||||
|
@ -9,6 +9,15 @@ import (
|
|||||||
"net/url"
|
"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
|
//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)
|
//TBD: Needs to accept 201 and 204 (returned for Create and Delete operations)
|
||||||
func getHTTPResponseBody(resp *http.Response) []byte {
|
func getHTTPResponseBody(resp *http.Response) []byte {
|
||||||
|
@ -7,8 +7,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tickets []Ticket
|
|
||||||
|
|
||||||
type Ticket struct {
|
type Ticket struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Summary string `json:"summary"`
|
Summary string `json:"summary"`
|
||||||
@ -185,15 +183,10 @@ type TimeEntryReference struct {
|
|||||||
|
|
||||||
func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket {
|
func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket {
|
||||||
|
|
||||||
ticket := Ticket{}
|
Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d", ticketID))
|
||||||
|
|
||||||
//Build the request URL
|
|
||||||
var Url *url.URL
|
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += fmt.Sprintf("/service/tickets/%d", ticketID)
|
|
||||||
|
|
||||||
body := GetRequest(site, Url)
|
body := GetRequest(site, Url)
|
||||||
|
ticket := Ticket{}
|
||||||
check(json.Unmarshal(body, &ticket))
|
check(json.Unmarshal(body, &ticket))
|
||||||
|
|
||||||
return &ticket
|
return &ticket
|
||||||
@ -201,14 +194,10 @@ func GetTicketByID(site *ConnectwiseSite, ticketID int) *Ticket {
|
|||||||
|
|
||||||
func GetTicketTimeEntriesByID(site *ConnectwiseSite, ticketID int) *[]TimeEntryReference {
|
func GetTicketTimeEntriesByID(site *ConnectwiseSite, ticketID int) *[]TimeEntryReference {
|
||||||
|
|
||||||
timeEntryReference := []TimeEntryReference{}
|
Url := BuildUrl(site, fmt.Sprintf("/service/tickets/%d/timeentries", ticketID))
|
||||||
|
|
||||||
var Url *url.URL
|
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += fmt.Sprintf("/service/tickets/%d/timeentries", ticketID)
|
|
||||||
|
|
||||||
body := GetRequest(site, Url)
|
body := GetRequest(site, Url)
|
||||||
|
timeEntryReference := []TimeEntryReference{}
|
||||||
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference
|
check(json.Unmarshal(body, &timeEntryReference)) // *[]TimeEntryReference
|
||||||
|
|
||||||
return &timeEntryReference
|
return &timeEntryReference
|
||||||
|
@ -20,28 +20,20 @@ type Callback struct {
|
|||||||
|
|
||||||
func GetCallbacks(site *ConnectwiseSite) *[]Callback {
|
func GetCallbacks(site *ConnectwiseSite) *[]Callback {
|
||||||
|
|
||||||
callbacks := []Callback{}
|
Url := BuildUrl(site, "/system/callbacks")
|
||||||
|
|
||||||
//Build the request URL
|
|
||||||
var Url *url.URL
|
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += "/system/callbacks"
|
|
||||||
|
|
||||||
body := GetRequest(site, Url)
|
body := GetRequest(site, Url)
|
||||||
|
|
||||||
|
callbacks := []Callback{}
|
||||||
check(json.Unmarshal(body, &callbacks))
|
check(json.Unmarshal(body, &callbacks))
|
||||||
|
|
||||||
return &callbacks
|
return &callbacks
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TBD: This should return something?
|
||||||
func NewCallback(site *ConnectwiseSite, callback Callback) {
|
func NewCallback(site *ConnectwiseSite, callback Callback) {
|
||||||
|
|
||||||
var Url *url.URL
|
Url := BuildUrl(site, "/system/callbacks")
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += "/system/callbacks"
|
|
||||||
|
|
||||||
jsonCallback, err := json.Marshal(callback)
|
jsonCallback, err := json.Marshal(callback)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
@ -54,13 +46,8 @@ func NewCallback(site *ConnectwiseSite, callback Callback) {
|
|||||||
|
|
||||||
func DeleteCallback(site *ConnectwiseSite, callback int) {
|
func DeleteCallback(site *ConnectwiseSite, callback int) {
|
||||||
|
|
||||||
var Url *url.URL
|
Url := BuildUrl(site, fmt.Sprintf("/system/callbacks/%d", callback))
|
||||||
Url, err := url.Parse(site.Site)
|
|
||||||
check(err)
|
|
||||||
Url.Path += fmt.Sprintf("/system/callbacks/%d", callback)
|
|
||||||
|
|
||||||
body := DeleteRequest(site, Url)
|
body := DeleteRequest(site, Url)
|
||||||
|
|
||||||
fmt.Print(string(body))
|
fmt.Print(string(body))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,14 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
cwSite = "https://yourconnectwisesite.com/v4_6_release/apis/3.0"
|
cwSite = "https://yourconnectwisesite.com/v4_6_release/apis/3.0"
|
||||||
cwAPIKeyPrivate = "ASDLFK4ah89ad"
|
cwAPIKeyPrivate = "ASDLFK4ah89ad" //Put in either your private API key or account password if using user impersonation
|
||||||
cwAPIKey = "ASLDFKJ2342kl"
|
cwAPIKey = "ASLDFKJ2342kl" //Put in either your public API key or account username if using user impersonation
|
||||||
cwCompany = "yourcompanyname"
|
cwCompany = "yourcompanyname" //The connectwise company name
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cw := connectwise.NewSite(cwSite, cwAPIKey, cwAPIKeyPrivate, cwCompany)
|
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)
|
fmt.Println(*companyDataByID)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user