Prepare for new central request function

This commit is contained in:
Steven Polley 2018-07-22 18:51:25 -06:00
parent 6e6bb1ad45
commit a73c2e77b6
2 changed files with 112 additions and 0 deletions

58
itglue/flexible-assets.go Normal file
View File

@ -0,0 +1,58 @@
package itglue
import (
"fmt"
"time"
)
type FlexibleAssetData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"created-at"`
UpdatedAt time.Time `json:"updated-at"`
Icon string `json:"icon"`
Enabled bool `json:"enabled"`
} `json:"attributes"`
Relationships struct {
} `json:"relationships"`
}
type FlexibleAsset struct {
Data struct{ FlexibleAssetData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
Links struct{ Links } `json:"links"`
}
type FlexibleAssetList struct {
Data []struct{ FlexibleAssetData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
Links struct{ Links } `json:"links"`
}
func (itg *ITGAPI) GetFlexibleAssets(flexibleAssetTypeID int) error {
itgurl, err := itg.BuildURL("/flexible_assets")
if err != nil {
return fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return fmt.Errorf("request failed: %s", err)
}
fmt.Println(string(body))
/*
flexibleAssets := &FlexibleAssetList{}
err = json.Unmarshal(body, flexibleAssets)
if err != nil {
return nil, fmt.Errorf("could not get flexible asset: %s", err)
}
return flexibleAssets, nil
*/
return nil
}

View File

@ -1,6 +1,7 @@
package itglue
import (
"bytes"
"fmt"
"io"
"io/ioutil"
@ -28,6 +29,59 @@ type ITGAPI struct {
APIKey string //API Key
}
type Request struct {
ITG *ITGAPI
RestAction string
Method string //GET, POST, DELETE, etc
Body []byte //In a GET request, this is an instance of the struct that the expected json data is to be unmarshaled into
URLValues url.Values //Parameters sent to ITG for filtering by conditions that utilize strings
Page int
PageSize int
}
//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 (itg *ITGAPI) NewRequest(restAction, method string, body []byte) *Request {
req := Request{ITG: itg, RestAction: restAction, Method: method, Body: body}
req.URLValues = url.Values{}
return &req
}
//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 {
itgurl, err := req.ITG.BuildURL(req.RestAction)
if err != nil {
return fmt.Errorf("could not build url %s: %s", req.RestAction, err)
}
//If pagination parameters are not being specified, then don't include them in the URL - not all endpoints will accept page and pagesize, they will 400 - bad request
if req.Page == 0 || req.PageSize == 0 {
itgurl.RawQuery = req.URLValues.Encode()
} else {
itgurl.RawQuery = fmt.Sprintf("%s&page[number]=%d&page[size]=%d", req.URLValues.Encode(), req.Page, req.PageSize)
}
client := &http.Client{}
jsonBuffer := bytes.NewReader(req.Body)
httpreq, err := http.NewRequest(req.Method, itgurl.String(), jsonBuffer)
if err != nil {
return fmt.Errorf("could not construct http request: %s", err)
}
httpreq.Header.Set("Content-Type", "application/vnd.api+json")
httpreq.Header.Set("x-api-key", req.ITG.APIKey)
httpreq.Header.Set("cache-control", "no-cache")
resp, err := client.Do(httpreq)
if err != nil {
return fmt.Errorf("could not perform http %s request: %s", req.Method, err)
}
req.Body, err = getHTTPResponseBody(resp)
if err != nil {
return fmt.Errorf("failed to get http response body: %s", err)
}
return nil
}
//NewITGAPI expects the API key to be passed to it
//Returns a pointer to an ITGAPI struct
func NewITGAPI(apiKey string) *ITGAPI {