-
Notifications
You must be signed in to change notification settings - Fork 13
/
glipgroups.go
153 lines (132 loc) · 3.49 KB
/
glipgroups.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
package glipgroups
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"time"
ro "github.com/grokify/oauth2more/ringcentral"
)
const (
GroupTypeTeam = "Team"
groupNameIdJoin = " - "
)
type GroupsSet struct {
GroupsMap map[string]Group // ID to Group Map
}
func (set *GroupsSet) AddGroups(groups []Group) {
for _, grp := range groups {
set.GroupsMap[grp.ID] = grp
}
}
func (set *GroupsSet) FindGroupsByName(groupName string) []Group {
groupName = strings.TrimSpace(groupName)
groups := []Group{}
for _, group := range set.GroupsMap {
if groupName == group.Name {
groups = append(groups, group)
}
}
return groups
}
func (set *GroupsSet) FindGroupsByNameLower(groupName string) []Group {
groupName = strings.ToLower(strings.TrimSpace(groupName))
groups := []Group{}
for _, group := range set.GroupsMap {
if groupName == strings.ToLower(group.Name) {
groups = append(groups, group)
}
}
return groups
}
func (set *GroupsSet) FindGroupByName(groupName string) (Group, error) {
groups := set.FindGroupsByName(groupName)
if len(groups) != 1 {
return Group{}, fmt.Errorf("Found [%v] groups for name [%v]", len(groups), groupName)
}
return groups[0], nil
}
func (set *GroupsSet) GroupNamesSorted(withIds bool) []string {
names := []string{}
for _, group := range set.GroupsMap {
name := group.Name
if withIds {
name += groupNameIdJoin + group.ID
}
names = append(names, name)
}
sort.Strings(names)
return names
}
func (set *GroupsSet) GroupsSorted() []Group {
names := set.GroupNamesSorted(true)
groups := []Group{}
for _, name := range names {
parts := strings.Split(name, groupNameIdJoin)
if len(parts) > 0 {
id := parts[len(parts)-1]
if g, ok := set.GroupsMap[id]; ok {
groups = append(groups, g)
}
}
}
return groups
}
type Group struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
CreationTime time.Time `json:"creationTime,omitempty"`
LastModifiedTime time.Time `json:"lastModifiedTime,omitempty"`
Members []string `json:"members,omitempty"`
}
func NewGroupsSetApiRequest(client *http.Client, serverUrl string, groupType string) (GroupsSet, error) {
groupType = strings.TrimSpace(groupType)
if len(groupType) == 0 {
groupType = "Team"
}
set := GroupsSet{GroupsMap: map[string]Group{}}
query := url.Values{}
query.Add("recordCount", "250")
if len(groupType) > 0 {
query.Add("type", groupType)
}
for {
groupsURL := ro.BuildURL(serverUrl, "/glip/groups", true, query)
resp, err := client.Get(groupsURL)
if err != nil {
return set, err
}
groupsResp, err := GetGroupsResponseFromHTTPResponse(resp)
if err != nil {
return set, err
}
set.AddGroups(groupsResp.Records)
if len(groupsResp.Navigation.PrevPageToken) > 0 {
query.Add("pageToken", groupsResp.Navigation.PrevPageToken)
} else {
break
}
}
return set, nil
}
type GetGroupsResponse struct {
Records []Group `json:"records,omitempty"`
Navigation Navigation `json:"navigation,omitempty"`
}
func GetGroupsResponseFromHTTPResponse(resp *http.Response) (GetGroupsResponse, error) {
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return GetGroupsResponse{}, err
}
var apiResp GetGroupsResponse
err = json.Unmarshal(bytes, &apiResp)
return apiResp, err
}
type Navigation struct {
PrevPageToken string `json:"prevPageToken,omitempty"`
NextPageToken string `json:"nextPageToken,omitempty"`
}