This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
contact_group.go
139 lines (108 loc) · 4.49 KB
/
contact_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package accounting
import (
"encoding/json"
"encoding/xml"
"github.com/XeroAPI/xerogolang"
"github.com/markbates/goth"
)
//ContactGroup is a way of organising Contacts into groups
type ContactGroup struct {
// The Name of the contact group. Required when creating a new contact group
Name string `json:"Name,omitempty" xml:"Name,omitempty"`
// The Status of a contact group. To delete a contact group update the status to DELETED. Only contact groups with a status of ACTIVE are returned on GETs.
Status string `json:"Status,omitempty" xml:"Status,omitempty"`
// The Xero identifier for an contact group – specified as a string following the endpoint name. e.g. /297c2dc5-cc47-4afd-8ec8-74990b8761e9
ContactGroupID string `json:"ContactGroupID,omitempty" xml:"ContactGroupID,omitempty"`
// The ContactID and Name of Contacts in a contact group. Returned on GETs when the ContactGroupID is supplied in the URL.
Contacts []Contact `json:"Contacts,omitempty" xml:"-"`
}
//ContactGroups is a collection of ContactGroups
type ContactGroups struct {
ContactGroups []ContactGroup `json:"ContactGroups" xml:"ContactGroup"`
}
func unmarshalContactGroup(contactGroupResponseBytes []byte) (*ContactGroups, error) {
var contactGroupResponse *ContactGroups
err := json.Unmarshal(contactGroupResponseBytes, &contactGroupResponse)
if err != nil {
return nil, err
}
return contactGroupResponse, err
}
//Create will create contactGroups given an ContactGroups struct
func (c *ContactGroups) Create(provider *xerogolang.Provider, session goth.Session) (*ContactGroups, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(c, " ", " ")
if err != nil {
return nil, err
}
contactGroupResponseBytes, err := provider.Create(session, "ContactGroups", additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalContactGroup(contactGroupResponseBytes)
}
//Update will update an contactGroup given an ContactGroups struct
//This will only handle single contactGroup - you cannot update multiple contactGroups in a single call
func (c *ContactGroups) Update(provider *xerogolang.Provider, session goth.Session) (*ContactGroups, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
"Content-Type": "application/xml",
}
body, err := xml.MarshalIndent(c, " ", " ")
if err != nil {
return nil, err
}
contactGroupResponseBytes, err := provider.Update(session, "ContactGroups/"+c.ContactGroups[0].ContactGroupID, additionalHeaders, body)
if err != nil {
return nil, err
}
return unmarshalContactGroup(contactGroupResponseBytes)
}
//FindContactGroups will get all contactGroups
func FindContactGroups(provider *xerogolang.Provider, session goth.Session) (*ContactGroups, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
contactGroupResponseBytes, err := provider.Find(session, "ContactGroups", additionalHeaders, nil)
if err != nil {
return nil, err
}
return unmarshalContactGroup(contactGroupResponseBytes)
}
//FindContactGroup will get a single contactGroup - contactGroupID must be a GUID for an contactGroup
func FindContactGroup(provider *xerogolang.Provider, session goth.Session, contactGroupID string) (*ContactGroups, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
contactGroupResponseBytes, err := provider.Find(session, "ContactGroups/"+contactGroupID, additionalHeaders, nil)
if err != nil {
return nil, err
}
return unmarshalContactGroup(contactGroupResponseBytes)
}
//RemoveContactGroup will get a single contactGroup - contactGroupID must be a GUID for an contactGroup
func RemoveContactGroup(provider *xerogolang.Provider, session goth.Session, contactGroupID string) (*ContactGroups, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
contactGroupResponseBytes, err := provider.Remove(session, "ContactGroups/"+contactGroupID, additionalHeaders)
if err != nil {
return nil, err
}
return unmarshalContactGroup(contactGroupResponseBytes)
}
//GenerateExampleContactGroup Creates an Example contactGroup
func GenerateExampleContactGroup() *ContactGroups {
contactGroup := ContactGroup{
Name: "Festivus Supporters",
Status: "ACTIVE",
}
contactGroupCollection := &ContactGroups{
ContactGroups: []ContactGroup{},
}
contactGroupCollection.ContactGroups = append(contactGroupCollection.ContactGroups, contactGroup)
return contactGroupCollection
}