Beginning framework
This commit is contained in:
parent
07b29c5b8f
commit
13e1366985
109
itglue.go
Normal file
109
itglue.go
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
package itglue
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//ITGAPI contains the ITG API URL for North America, as well as the API key.
|
||||||
|
//Think of it as an instance of the API client
|
||||||
|
type ITGAPI struct {
|
||||||
|
Site string //Full URL ITG API
|
||||||
|
APIKey string //API Key
|
||||||
|
}
|
||||||
|
|
||||||
|
//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 {
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
|
||||||
|
out := fmt.Sprintf("ITG returned HTTP status code %s\n%s", resp.Status, resp.Body)
|
||||||
|
log.Fatal(out)
|
||||||
|
return make([]byte, 0) //TBD: Don't hack
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
check(err)
|
||||||
|
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
//BuildURL expects a restaction to be passed to it
|
||||||
|
//Returns the full request URL containing the ITG API domain prepended to the rest action
|
||||||
|
func (itg *ITGAPI) BuildURL(restAction string) *url.URL {
|
||||||
|
var URL *url.URL
|
||||||
|
URL, err := url.Parse(itg.Site)
|
||||||
|
check(err)
|
||||||
|
URL.Path += restAction
|
||||||
|
return URL
|
||||||
|
}
|
||||||
|
|
||||||
|
//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(URL *url.URL) []byte {
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("GET", URL.String(), nil)
|
||||||
|
check(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)
|
||||||
|
check(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(URL *url.URL, body io.Reader) []byte {
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("POST", URL.String(), body)
|
||||||
|
check(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)
|
||||||
|
check(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(URL *url.URL, body io.Reader) []byte {
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("PATCH", URL.String(), body)
|
||||||
|
check(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)
|
||||||
|
check(err)
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
return getHTTPResponseBody(response)
|
||||||
|
|
||||||
|
}
|
36
organizations.go
Normal file
36
organizations.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package itglue
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
//Organization maps JSON data from the ITG Organization resource to Go struct
|
||||||
|
type Organization struct {
|
||||||
|
Data struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Attributes struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
OrganizationTypeID int `json:"organization-type-id"`
|
||||||
|
OrganizationTypeName string `json:"organization-type-name"`
|
||||||
|
OrganizationStatusID int `json:"organization-status-id"`
|
||||||
|
OrganizationStatusName string `json:"organization-status-name"`
|
||||||
|
Logo string `json:"logo"`
|
||||||
|
QuickNotes string `json:"quick-notes"`
|
||||||
|
ShortName string `json:"short-name"`
|
||||||
|
CreatedAt string `json:"created-at"`
|
||||||
|
UpdatedAt string `json:"updated-at"`
|
||||||
|
} `json:"attributes"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetOrganizationByID expects an ITG organization ID
|
||||||
|
//Returns a pointer to an Organization struct
|
||||||
|
func (itg *ITGAPI) GetOrganizationByID(organizationID int) {
|
||||||
|
URL := itg.BuildURL(fmt.Sprintf("/organizations/%d", organizationID))
|
||||||
|
|
||||||
|
body := itg.GetRequest(URL)
|
||||||
|
fmt.Print(string(body))
|
||||||
|
|
||||||
|
//organization := Organization{}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user