forked from jbogarin/go-cisco-webex-teams
/
people_api.go
267 lines (212 loc) · 9.27 KB
/
people_api.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package webexteams
import (
"fmt"
"strings"
"time"
"gopkg.in/resty.v1"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// PeopleService is the service to communicate with the People API endpoint
type PeopleService service
// People is the List of Persons
type People struct {
Items []Person `json:"items,omitempty"`
}
// PersonRequest is the Create Person Request Parameters
type PersonRequest struct {
Emails []string `json:"emails,omitempty"` // Email addresses of the person
DisplayName string `json:"displayName,omitempty"` // Full name of the person
FirstName string `json:"firstName,omitempty"` // First name of the person
LastName string `json:"lastName,omitempty"` // Last name of the person
Avatar string `json:"avatar,omitempty"` // URL to the person's avatar in PNG format
OrgID string `json:"orgId,omitempty"` // ID of the organization to which this person belongs
Roles []string `json:"roles,omitempty"` // Roles of the person
Licenses []string `json:"licenses,omitempty"` // Licenses allocated to the person
}
// Person is the Person definition
type Person struct {
ID string `json:"id,omitempty"` // Person ID.
Emails []string `json:"emails,omitempty"` // Person email array.
DisplayName string `json:"displayName,omitempty"` // Person display name.
NickName string `json:"nickName,omitempty"` // Person nickname.
FirstName string `json:"firstName,omitempty"` // Person first name.
LastName string `json:"lastName,omitempty"` // Person last name.
Avatar string `json:"avatar,omitempty"` // Person avatar URL.
OrgID string `json:"orgId,omitempty"` // Person organization ID.
Roles []string `json:"roles,omitempty"` // Person roles.
Licenses []string `json:"licenses,omitempty"` // Person licenses.
Created time.Time `json:"created,omitempty"` // Person creation date/time.
TimeZone string `json:"timeZone,omitempty"` // Person time zone.
LastActivity time.Time `json:"lastActivity,omitempty"` // Person last active date/time.
Status string `json:"status,omitempty"` // Person presence status (active or inactive).
PersonType string `json:"type,omitempty"` // Person type (person or bot).
}
// AddPerson is used to append a person to a slice of People
func (people *People) AddPerson(item Person) []Person {
people.Items = append(people.Items, item)
return people.Items
}
func peoplePagination(linkHeader string, size, max int) *People {
items := &People{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := RestyClient.R().
SetResult(&People{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*People)
if size != 0 {
size = size + len(items.Items)
if size < max {
people := peoplePagination(response.Header().Get("Link"), size, max)
for _, person := range people.Items {
items.AddPerson(person)
}
}
} else {
people := peoplePagination(response.Header().Get("Link"), size, max)
for _, person := range people.Items {
items.AddPerson(person)
}
}
}
}
return items
}
// CreatePerson Create a new user account for a given organization.
/* Create a new user account for a given organization. Only an admin can create a new user account.
Currently, users may have only one email address associated with their account. The emails parameter is an array, which accepts multiple values to allow for future expansion, but currently only one email address will be used for the new user.
@param personRequest
@return Person
*/
func (s *PeopleService) CreatePerson(personRequest *PersonRequest) (*Person, *resty.Response, error) {
path := "/people/"
response, err := RestyClient.R().
SetBody(personRequest).
SetResult(&Person{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Person)
return result, response, err
}
// DeletePerson Remove a person from the system. Only an admin can remove a person.
/* Remove a person from the system. Only an admin can remove a person.
Specify the person ID in the personID parameter in the URI.
@param personID Person ID.
@return
*/
func (s *PeopleService) DeletePerson(personID string) (*resty.Response, error) {
path := "/people/{personId}"
path = strings.Replace(path, "{"+"personId"+"}", fmt.Sprintf("%v", personID), -1)
response, err := RestyClient.R().
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
// GetMe Show the profile for the authenticated user.
/* Show the profile for the authenticated user. This is the same as GET /people/:id using the Person ID associated with your Auth token.
@return Person
*/
func (s *PeopleService) GetMe() (*Person, *resty.Response, error) {
path := "/people/me"
response, err := RestyClient.R().
SetResult(&Person{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Person)
return result, response, err
}
// GetPerson Shows details for a person, by ID.
/* Shows details for a person, by ID. Certain fields, such as status or lastActivity, will only be displayed for people within your organization or an organzation you manage.
Specify the person ID in the personID parameter in the URI.
@param personID Person ID.
@return Person
*/
func (s *PeopleService) GetPerson(personID string) (*Person, *resty.Response, error) {
path := "/people/{personId}"
path = strings.Replace(path, "{"+"personId"+"}", fmt.Sprintf("%v", personID), -1)
response, err := RestyClient.R().
SetResult(&Person{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Person)
return result, response, err
}
// ListPeopleQueryParams are the query params for the ListPeople API Call
type ListPeopleQueryParams struct {
ID string `url:"id,omitempty"` // List people by ID. Accepts up to 85 person IDs separated by commas.
Email string `url:"email,omitempty"` // List people with this email address. For non-admin requests, either this or displayName are required.
DisplayName string `url:"displayName,omitempty"` // List people whose name starts with this string. For non-admin requests, either this or email are required.
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
OrgID string `url:"orgId,omitempty"` // List people in this organization. Only admin users of another organization (such as partners) may use this parameter.
Paginate bool // Indicates if pagination is needed
}
// ListPeople List people in your organization.
/* List people in your organization. For most users, either the email or displayName parameter is required.
Admin users can omit these fields and list all users in their organization.
@param "id" (string) List people by ID. Accepts up to 85 person IDs separated by commas.
@param "email" (string) List people with this email address. For non-admin requests, either this or displayName are required.
@param "displayName" (string) List people whose name starts with this string. For non-admin requests, either this or email are required.
@param "max" (int) Limit the maximum number of items in the response.
@param "orgId" (string) List people in this organization. Only admin users of another organization (such as partners) may use this parameter.
@param paginate (bool) indicates if pagination is needed
@return People
*/
func (s *PeopleService) ListPeople(queryParams *ListPeopleQueryParams) (*People, *resty.Response, error) {
path := "/people/"
queryParamsString, _ := query.Values(queryParams)
response, err := RestyClient.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&People{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*People)
if queryParams.Paginate == true {
items := peoplePagination(response.Header().Get("Link"), 0, 0)
for _, person := range items.Items {
result.AddPerson(person)
}
} else {
if len(result.Items) < queryParams.Max {
items := peoplePagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, person := range items.Items {
result.AddPerson(person)
}
}
}
return result, response, err
}
// Update Update details for a person, by ID.
/* Update details for a person, by ID.
Specify the person ID in the personID parameter in the URI. Only an admin can update a person details. Email addresses for a person cannot be changed via the Cisco Webex Teams API.
Include all details for the person. This action expects all user details to be present in the request. A common approach is to first GET the person's details, make changes, then PUT both the changed and unchanged values.
@param personID Person ID.
@param personRequest
@return Person
*/
func (s *PeopleService) Update(personID string, personRequest *PersonRequest) (*Person, *resty.Response, error) {
path := "/people/{personId}"
path = strings.Replace(path, "{"+"personId"+"}", fmt.Sprintf("%v", personID), -1)
response, err := RestyClient.R().
SetBody(personRequest).
SetResult(&Person{}).
Put(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Person)
return result, response, err
}