From f8eac0388a9ad0008f685e9d56bfda7b7d278b6f Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Fri, 29 Jun 2018 23:54:16 -0600 Subject: [PATCH] Added GetOrganizationByName. Update README.MD with usage --- README.md | 25 ++++++++++++++++++++++++- itglue/organizations.go | 18 ++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0547e88..1378c6f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,27 @@ # go-itg IT Glue API Structs + Receivers for the Go -This is not complete, and at least for now, I will only be adding support the functionality required for use within my organization. Feel free to send a pull request if you'd like to contribute anything that's missing. +This is nowhere near complete, and at least for now, I will only be adding support the functionality required for use within my organization. Feel free to send a pull request if you'd like to contribute anything that's missing. + +#Installation +``` +go get github.com/StevenPolley/go-itg +``` + +#Usage +``` +package main + +import ( + "fmt" + + "github.com/StevenPolley/go-itg/itglue" +) + +func main() { + itgAPI := itglue.NewITGAPI("ITG.XXXXXXXXXXXXX555555YOURAPIDKEYHERE2222222222222222XXXXXX") + fmt.Println((*itgAPI).GetOrganizationByID(1234242).Data.Attributes.Name) //Returns the name of the Organization with ID 1234242 + fmt.Println((*itgAPI).GetOrganizationByName("test Org Inc.").Data[0].Attributes.Description) //Returns the description of the Organization with Name "Test Org Inc." + +} +``` \ No newline at end of file diff --git a/itglue/organizations.go b/itglue/organizations.go index d447adc..a307c64 100644 --- a/itglue/organizations.go +++ b/itglue/organizations.go @@ -3,6 +3,7 @@ package itglue import ( "encoding/json" "fmt" + "net/url" ) //OrganizationInternalData contains the schema of an Organization in IT Glue without the "data" wrapper. @@ -39,13 +40,22 @@ type OrganizationList struct { //Returns a pointer to an Organization struct func (itg *ITGAPI) GetOrganizationByID(organizationID int) *Organization { itgurl := itg.BuildURL(fmt.Sprintf("/organizations/%d", organizationID)) - body := itg.GetRequest(itgurl) - organization := &Organization{} err := json.Unmarshal(body, organization) check(err) - return organization - +} + +//GetOrganizationByName expects an exact matching organization name and returns an OrganizationList +func (itg *ITGAPI) GetOrganizationByName(organizationName string) *OrganizationList { + itgurl := itg.BuildURL("/organizations") + params := url.Values{} + params.Add("filter[name]", organizationName) + itgurl.RawQuery = params.Encode() + body := itg.GetRequest(itgurl) + organization := &OrganizationList{} + err := json.Unmarshal(body, organization) + check(err) + return organization }