go-cw style request wrapping

master
Steven Polley 4 years ago
parent a73c2e77b6
commit 99207293a2
  1. 12
      itglue/flexible-asset-types.go
  2. 15
      itglue/flexible-assets.go
  3. 81
      itglue/itglue.go
  4. 12
      itglue/organization-statuses.go
  5. 11
      itglue/organization-types.go
  6. 35
      itglue/organizations.go
  7. 12
      itglue/user-metrics.go
  8. 12
      itglue/users.go

@ -34,18 +34,14 @@ type FlexibleAssetTypeList struct {
}
func (itg *ITGAPI) GetFlexibleAssetTypes() (*FlexibleAssetTypeList, error) {
itgurl, err := itg.BuildURL("/flexible_asset_types")
req := itg.NewRequest("/flexible_asset_types", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
flexibleAssetTypes := &FlexibleAssetTypeList{}
err = json.Unmarshal(body, flexibleAssetTypes)
err = json.Unmarshal(req.Body, flexibleAssetTypes)
if err != nil {
return nil, fmt.Errorf("could not get flexible asset types: %s", err)
}

@ -33,21 +33,16 @@ type FlexibleAssetList struct {
}
func (itg *ITGAPI) GetFlexibleAssets(flexibleAssetTypeID int) error {
itgurl, err := itg.BuildURL("/flexible_assets")
req := itg.NewRequest("/flexible_assets", "GET", nil)
err := req.Do()
if err != nil {
return fmt.Errorf("could not build URL: %s", err)
return fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return fmt.Errorf("request failed: %s", err)
}
fmt.Println(string(body))
fmt.Println(string(req.Body))
/*
flexibleAssets := &FlexibleAssetList{}
err = json.Unmarshal(body, flexibleAssets)
err = json.Unmarshal(req.Body, flexibleAssets)
if err != nil {
return nil, fmt.Errorf("could not get flexible asset: %s", err)
}

@ -3,7 +3,6 @@ package itglue
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
@ -39,6 +38,12 @@ type Request struct {
PageSize int
}
//NewITGAPI expects the API key to be passed to it
//Returns a pointer to an ITGAPI struct
func NewITGAPI(apiKey string) *ITGAPI {
return &ITGAPI{Site: "https://api.itglue.com", APIKey: apiKey}
}
//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}
@ -82,12 +87,6 @@ func (req *Request) Do() error {
return nil
}
//NewITGAPI expects the API key to be passed to it
//Returns a pointer to an ITGAPI struct
func NewITGAPI(apiKey string) *ITGAPI {
return &ITGAPI{Site: "https://api.itglue.com", APIKey: apiKey}
}
func getHTTPResponseBody(resp *http.Response) ([]byte, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("ITG returned HTTP status code %s\n%s", resp.Status, resp.Body)
@ -112,71 +111,3 @@ func (itg *ITGAPI) BuildURL(restAction string) (*url.URL, error) {
itgurl.Path += restAction
return itgurl, nil
}
//GetRequest allows a custom GET request to the API to be made.
//Also used internally by this package by the binding functions
//Expects URL to be passed
//Returns the response body as a byte slice
func (itg *ITGAPI) GetRequest(itgurl *url.URL) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", itgurl.String(), nil)
if err != nil {
return nil, fmt.Errorf("could not create http request: %s", err)
}
req.Header.Set("Content-Type", "application/vnd.api+json")
req.Header.Set("x-api-key", itg.APIKey)
req.Header.Set("cache-control", "no-cache")
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed: %s", err)
}
defer response.Body.Close()
return getHTTPResponseBody(response)
}
//PostRequest allows a custom POST request tot he API to be made
//Also used internally by this package by the binding functions
//Expects a URL and a body to be passed
//Returns the response body as a byte slice
func (itg *ITGAPI) PostRequest(itgurl *url.URL, body io.Reader) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", itgurl.String(), body)
if err != nil {
return nil, fmt.Errorf("could not create http request: %s", err)
}
req.Header.Set("Content-Type", "application/vnd.api+json")
req.Header.Set("x-api-key", itg.APIKey)
req.Header.Set("cache-control", "no-cache")
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed: %s", err)
}
defer response.Body.Close()
return getHTTPResponseBody(response)
}
//PatchRequest allows a custom PATCH request tot he API to be made
//Also used internally by this package by the binding functions
//Expects a URL and a body to be passed
//Returns the response body as a byte slice
func (itg *ITGAPI) PatchRequest(itgurl *url.URL, body io.Reader) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("PATCH", itgurl.String(), body)
if err != nil {
return nil, fmt.Errorf("could not create http request: %s", err)
}
req.Header.Set("Content-Type", "application/vnd.api+json")
req.Header.Set("x-api-key", itg.APIKey)
req.Header.Set("cache-control", "no-cache")
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed: %s", err)
}
defer response.Body.Close()
return getHTTPResponseBody(response)
}

@ -30,18 +30,14 @@ type OrganizationStatusList struct {
}
func (itg *ITGAPI) GetOrganizationStatuses() (*OrganizationStatusList, error) {
itgurl, err := itg.BuildURL("/organization_statuses")
req := itg.NewRequest("/organization_statuses", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
organizationStatuses := &OrganizationStatusList{}
err = json.Unmarshal(body, organizationStatuses)
err = json.Unmarshal(req.Body, organizationStatuses)
if err != nil {
return nil, fmt.Errorf("could not get organization types: %s", err)
}

@ -31,17 +31,14 @@ type OrganizationTypeList struct {
///organization_types
func (itg *ITGAPI) GetOrganizationTypes() (*OrganizationTypeList, error) {
itgurl, err := itg.BuildURL("/organization_types")
req := itg.NewRequest("/organization_types", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
organizationTypes := &OrganizationTypeList{}
err = json.Unmarshal(body, organizationTypes)
err = json.Unmarshal(req.Body, organizationTypes)
if err != nil {
return nil, fmt.Errorf("could not get organization types: %s", err)
}

@ -3,7 +3,6 @@ package itglue
import (
"encoding/json"
"fmt"
"net/url"
)
//OrganizationData contains the schema of an Organization in IT Glue without the "data" wrapper.
@ -41,39 +40,39 @@ type OrganizationList struct {
//GetOrganizationByID expects an ITG organization ID
//Returns a pointer to an Organization struct
func (itg *ITGAPI) GetOrganizationByID(organizationID int) (*Organization, error) {
itgurl, err := itg.BuildURL(fmt.Sprintf("/organizations/%d", organizationID))
req := itg.NewRequest(fmt.Sprintf("/organizations/%d", organizationID), "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
organization := &Organization{}
err = json.Unmarshal(body, organization)
err = json.Unmarshal(req.Body, organization)
if err != nil {
return nil, fmt.Errorf("could not get organization: %s", err)
}
return organization, nil
}
//GetOrganizationByName expects an exact matching organization name and returns an OrganizationList
func (itg *ITGAPI) GetOrganizationByName(organizationName string) (*OrganizationList, error) {
itgurl, err := itg.BuildURL("/organizations")
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
params := url.Values{}
params.Add("filter[name]", organizationName)
itgurl.RawQuery = params.Encode()
body, err := itg.GetRequest(itgurl)
req := itg.NewRequest("/organizations", "GET", nil)
req.URLValues.Add("filter[name]", organizationName)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
organization := &OrganizationList{}
err = json.Unmarshal(body, organization)
err = json.Unmarshal(req.Body, organization)
if err != nil {
return nil, fmt.Errorf("could not get organization: %s", err)
}
if len((*organization).Data) == 0 {
return nil, fmt.Errorf("ITG returned no results for %s", organizationName)
}
return organization, nil
}

@ -33,18 +33,14 @@ type UserMetricList struct {
}
func (itg *ITGAPI) GetUserMetrics() (*UserMetricList, error) {
itgurl, err := itg.BuildURL("/user_metrics")
req := itg.NewRequest("/user_metrics", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
userMetrics := &UserMetricList{}
err = json.Unmarshal(body, userMetrics)
err = json.Unmarshal(req.Body, userMetrics)
if err != nil {
return nil, fmt.Errorf("could not get users: %s", err)
}

@ -43,18 +43,14 @@ type UserList struct {
}
func (itg *ITGAPI) GetUsers() (*UserList, error) {
itgurl, err := itg.BuildURL("/users")
req := itg.NewRequest("/users", "GET", nil)
err := req.Do()
if err != nil {
return nil, fmt.Errorf("could not build URL: %s", err)
}
body, err := itg.GetRequest(itgurl)
if err != nil {
return nil, fmt.Errorf("request failed: %s", err)
return nil, fmt.Errorf("request failed for %s: %s", req.RestAction, err)
}
users := &UserList{}
err = json.Unmarshal(body, users)
err = json.Unmarshal(req.Body, users)
if err != nil {
return nil, fmt.Errorf("could not get users: %s", err)
}

Loading…
Cancel
Save