Split organization-types into separate file. new file for each endpoint

This commit is contained in:
Steven Polley 2018-07-22 02:19:22 -06:00
parent 3d987bb089
commit 582b4c205f
2 changed files with 48 additions and 42 deletions

View File

@ -0,0 +1,48 @@
package itglue
import (
"encoding/json"
"fmt"
"time"
)
type OrganizationTypeData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created-at"`
UpdatedAt time.Time `json:"updated-at"`
Synced bool `json:"synced"`
} `json:"attributes"`
}
type OrganizationType struct {
Data struct{ OrganizationTypeData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
}
type OrganizationTypeList struct {
Data []struct{ OrganizationTypeData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
}
///organization_types
func (itg *ITGAPI) GetOrganizationTypes() (*OrganizationTypeList, error) {
itgurl, err := itg.BuildURL("/organization_types")
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)
}
organizationTypes := &OrganizationTypeList{}
err = json.Unmarshal(body, organizationTypes)
if err != nil {
return nil, fmt.Errorf("could not get organization types: %s", err)
}
return organizationTypes, nil
}

View File

@ -4,20 +4,8 @@ import (
"encoding/json"
"fmt"
"net/url"
"time"
)
type OrganizationTypeData struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes struct {
Name string `json:"name"`
CreatedAt time.Time `json:"created-at"`
UpdatedAt time.Time `json:"updated-at"`
Synced bool `json:"synced"`
} `json:"attributes"`
}
//OrganizationData contains the schema of an Organization in IT Glue without the "data" wrapper.
//This allows us to reuse the schema when data is either a JSON object or an array, depending on what results are returned
type OrganizationData struct {
@ -50,36 +38,6 @@ type OrganizationList struct {
Meta struct{ Metadata } `json:"metadata"`
}
type OrganizationType struct {
Data struct{ OrganizationTypeData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
}
type OrganizationTypeList struct {
Data []struct{ OrganizationTypeData } `json:"data"`
Meta struct{ Metadata } `json:"metadata"`
}
///organization_types
func (itg *ITGAPI) GetOrganizationTypes() (*OrganizationTypeList, error) {
itgurl, err := itg.BuildURL("/organization_types")
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)
}
organizationTypes := &OrganizationTypeList{}
err = json.Unmarshal(body, organizationTypes)
if err != nil {
return nil, fmt.Errorf("could not get organization types: %s", err)
}
return organizationTypes, nil
}
//GetOrganizationByID expects an ITG organization ID
//Returns a pointer to an Organization struct
func (itg *ITGAPI) GetOrganizationByID(organizationID int) (*Organization, error) {