NewRequest is now a method of a connectwise.Site

This commit is contained in:
Steven Polley 2018-07-08 15:16:59 -06:00
parent 993c7ee254
commit b538596d9c
6 changed files with 38 additions and 14 deletions

View File

@ -147,7 +147,7 @@ type Company struct {
//CompanyCount returns the number of companies in ConnectWise
func (cw *Site) CompanyCount() (int, error) {
req := NewRequest(cw, "/company/companies/count", "GET", nil)
req := cw.NewRequest("/company/companies/count", "GET", nil)
err := req.Do()
if err != nil {
return 0, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -165,7 +165,7 @@ func (cw *Site) CompanyCount() (int, error) {
//GetCompanyByName expects an exact match, perhaps an improvement could be made to support wildcard characters.
//Will return a pointer to a Company
func (cw *Site) GetCompanyByName(companyName string) (*Company, error) {
req := NewRequest(cw, "/company/companies", "GET", nil)
req := cw.NewRequest("/company/companies", "GET", nil)
req.URLValues.Add("conditions", "name=\""+companyName+"\"")
err := req.Do()
@ -189,7 +189,7 @@ func (cw *Site) GetCompanyByName(companyName string) (*Company, error) {
//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 *Site) GetCompanyByID(companyID int) (*Company, error) {
req := NewRequest(cw, fmt.Sprintf("/company/companies/%d", companyID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/company/companies/%d", companyID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)

View File

@ -117,7 +117,7 @@ type Agreement struct {
//GetAgreements returns a list of agreements, not paginated and currently not that useful
//TBD: Pagination and filtering options still need to be considered
func (cw *Site) GetAgreements() (*[]Agreement, error) {
req := NewRequest(cw, "/finance/agreements", "GET", nil)
req := cw.NewRequest("/finance/agreements", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -135,7 +135,7 @@ func (cw *Site) GetAgreements() (*[]Agreement, error) {
//GetAgreementsByCompanyName returns a list of agreements that belong to an exact matching company name
//TBD: Pagination and filtering options still need to be considered
func (cw *Site) GetAgreementsByCompanyName(companyName string) (*[]Agreement, error) {
req := NewRequest(cw, "/finance/agreements", "GET", nil)
req := cw.NewRequest("/finance/agreements", "GET", nil)
req.URLValues.Add("conditions", "company/name=\""+companyName+"\"")
err := req.Do()
@ -154,7 +154,7 @@ func (cw *Site) GetAgreementsByCompanyName(companyName string) (*[]Agreement, er
//GetAgreementByID returns an agreement that matches the ID provided
func (cw *Site) GetAgreementByID(agreementID int) (*Agreement, error) {
req := NewRequest(cw, fmt.Sprintf("/finance/agreements/%d", agreementID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/finance/agreements/%d", agreementID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)

View File

@ -2,6 +2,7 @@ package connectwise
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@ -20,12 +21,35 @@ type Request struct {
}
//NewRequest is a function which takes the mandatory fields to perform a request to the CW API and returns a pointer to a Request struct
func NewRequest(cw *Site, restAction, method string, body []byte) *Request {
func (cw *Site) NewRequest(restAction, method string, body []byte) *Request {
req := Request{CW: cw, RestAction: restAction, Method: method, Body: body}
req.URLValues = url.Values{}
return &req
}
//NewPaginationRequest is a method which takes in the mandatory fields to paginate
//TBD - finish this
func (cw *Site) NewPaginationRequest(restAction, method string, body []byte, pageSize, pageNumber int) (*[]Company, error) {
req := cw.NewRequest("/company/companies", "GET", nil)
req.Page = pageNumber
req.PageSize = pageSize
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
co := &[]Company{}
err = json.Unmarshal(req.Body, co)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
}
return co, nil
}
//Do is a method of the Request struct which uses the data contained within the Request instance to perform an HTTP request to ConnectWise
func (req *Request) Do() error {
cwurl, err := req.CW.BuildURL(req.RestAction)

View File

@ -195,7 +195,7 @@ type ConfigurationReference struct {
//GetTicketByID expects a ticket ID and returns a pointer to a Ticket struct
func (cw *Site) GetTicketByID(ticketID int) (*Ticket, error) {
req := NewRequest(cw, fmt.Sprintf("/service/tickets/%d", ticketID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/service/tickets/%d", ticketID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -213,7 +213,7 @@ func (cw *Site) GetTicketByID(ticketID int) (*Ticket, error) {
//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 *Site) GetTicketTimeEntriesByID(ticketID int) (*[]TimeEntryReference, error) {
req := NewRequest(cw, fmt.Sprintf("/service/tickets/%d/timeentries", ticketID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/service/tickets/%d/timeentries", ticketID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -230,7 +230,7 @@ func (cw *Site) GetTicketTimeEntriesByID(ticketID int) (*[]TimeEntryReference, e
//GetTicketConfigurationsByID expects a ticket ID and returns a pointer to a slice of the configurations attached to the ticket
func (cw *Site) GetTicketConfigurationsByID(ticketID int) (*[]ConfigurationReference, error) {
req := NewRequest(cw, fmt.Sprintf("/service/tickets/%d/configurations", ticketID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/service/tickets/%d/configurations", ticketID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)

View File

@ -23,7 +23,7 @@ type Callback struct {
//GetCallbacks returns a slice of Callback structs containing all the callbacks currently registered with ConnectWise
func (cw *Site) GetCallbacks() (*[]Callback, error) {
req := NewRequest(cw, "/system/callbacks", "GET", nil)
req := cw.NewRequest("/system/callbacks", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -46,7 +46,7 @@ func (cw *Site) NewCallback(callback *Callback) (*Callback, error) {
return nil, fmt.Errorf("could not marshal json data: %s", err)
}
req := NewRequest(cw, "/system/callbacks", "POST", jsonCallback)
req := cw.NewRequest("/system/callbacks", "POST", jsonCallback)
err = req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
@ -64,7 +64,7 @@ func (cw *Site) NewCallback(callback *Callback) (*Callback, error) {
//DeleteCallback expects the ID of an existing callback and will unregister it with ConnectWise
//Does not return anything - CW gives an empty response body
func (cw *Site) DeleteCallback(callback int) error {
req := NewRequest(cw, fmt.Sprintf("/system/callbacks/%d", callback), "DELETE", nil)
req := cw.NewRequest(fmt.Sprintf("/system/callbacks/%d", callback), "DELETE", nil)
err := req.Do()
if err != nil {
return fmt.Errorf("request failed for %s: %s", req.RestAction, err)

View File

@ -86,7 +86,7 @@ type TimeEntry struct {
//GetTimeEntryByID expects a time entry ID and will return a pointer to a TimeEntry struct
func (cw *Site) GetTimeEntryByID(timeEntryID int) (*TimeEntry, error) {
req := NewRequest(cw, fmt.Sprintf("/time/entries/%d", timeEntryID), "GET", nil)
req := cw.NewRequest(fmt.Sprintf("/time/entries/%d", timeEntryID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)