go-cw style request wrapping

This commit is contained in:
Steven Polley 2018-07-22 19:35:10 -06:00
parent a73c2e77b6
commit 99207293a2
8 changed files with 48 additions and 142 deletions

View File

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

View File

@ -33,21 +33,16 @@ type FlexibleAssetList struct {
} }
func (itg *ITGAPI) GetFlexibleAssets(flexibleAssetTypeID int) error { 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 { 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) fmt.Println(string(req.Body))
if err != nil {
return fmt.Errorf("request failed: %s", err)
}
fmt.Println(string(body))
/* /*
flexibleAssets := &FlexibleAssetList{} flexibleAssets := &FlexibleAssetList{}
err = json.Unmarshal(body, flexibleAssets) err = json.Unmarshal(req.Body, flexibleAssets)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not get flexible asset: %s", err) return nil, fmt.Errorf("could not get flexible asset: %s", err)
} }

View File

@ -3,7 +3,6 @@ package itglue
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -39,6 +38,12 @@ type Request struct {
PageSize int 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 //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 { func (itg *ITGAPI) NewRequest(restAction, method string, body []byte) *Request {
req := Request{ITG: itg, RestAction: restAction, Method: method, Body: body} req := Request{ITG: itg, RestAction: restAction, Method: method, Body: body}
@ -82,12 +87,6 @@ func (req *Request) Do() error {
return nil 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) { func getHTTPResponseBody(resp *http.Response) ([]byte, error) {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("ITG returned HTTP status code %s\n%s", resp.Status, resp.Body) 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 itgurl.Path += restAction
return itgurl, nil 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)
}

View File

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

View File

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

View File

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

View File

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

View File

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