diff --git a/itglue/flexible-assets.go b/itglue/flexible-assets.go new file mode 100644 index 0000000..21a3ead --- /dev/null +++ b/itglue/flexible-assets.go @@ -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 +} diff --git a/itglue/itglue.go b/itglue/itglue.go index 9711f40..f6d9829 100644 --- a/itglue/itglue.go +++ b/itglue/itglue.go @@ -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 {