forked from jbogarin/go-cisco-webex-teams
/
organizations_api.go
140 lines (113 loc) · 4 KB
/
organizations_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
package webexteams
import (
"fmt"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// OrganizationsService is the service to communicate with the Organizations API endpoint
type OrganizationsService service
// Organization is the Organization definition
type Organization struct {
ID string `json:"id,omitempty"` // Organization ID.
DisplayName string `json:"displayName,omitempty"` // Organization Display Name.
Created time.Time `json:"created,omitempty"` // Organization creation date/time.
}
// Organizations is the List of Organizations
type Organizations struct {
Items []Organization `json:"items,omitempty"`
}
// AddOrganization is used to append a organization to a slice of organizations
func (organizations *Organizations) AddOrganization(item Organization) []Organization {
organizations.Items = append(organizations.Items, item)
return organizations.Items
}
func (s *OrganizationsService) organizationsPagination(linkHeader string, size, max int) *Organizations {
items := &Organizations{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := s.client.R().
SetResult(&Organizations{}).
SetError(&Error{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Organizations)
if size != 0 {
size = size + len(items.Items)
if size < max {
organizations := s.organizationsPagination(response.Header().Get("Link"), size, max)
for _, organization := range organizations.Items {
items.AddOrganization(organization)
}
}
} else {
organizations := s.organizationsPagination(response.Header().Get("Link"), size, max)
for _, organization := range organizations.Items {
items.AddOrganization(organization)
}
}
}
}
return items
}
// GetOrganization Shows details for an organization, by ID.
/* Shows details for an organization, by ID.
Specify the org ID in the orgID parameter in the URI.
@param orgID Organization ID.
@return Organization
*/
func (s *OrganizationsService) GetOrganization(orgID string) (*Organization, *resty.Response, error) {
path := "/organizations/{orgId}"
path = strings.Replace(path, "{"+"orgId"+"}", fmt.Sprintf("%v", orgID), -1)
response, err := s.client.R().
SetResult(&Organization{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Organization)
return result, response, err
}
// ListOrganizationsQueryParams are the query params for the ListOrganizations API Call
type ListOrganizationsQueryParams struct {
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
Paginate bool // Indicates if pagination is needed
}
// ListOrganizations List all organizations visible by your account. The results will not be paginated.
/* List all organizations visible by your account. The results will not be paginated.
@param "max" (int) Limit the maximum number of items in the response.
@param paginate (bool) indicates if pagination is needed
@return Organizations
*/
func (s *OrganizationsService) ListOrganizations(queryParams *ListOrganizationsQueryParams) (*Organizations, *resty.Response, error) {
path := "/organizations/"
queryParamsString, _ := query.Values(queryParams)
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Organizations{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Organizations)
if queryParams.Paginate {
items := s.organizationsPagination(response.Header().Get("Link"), 0, 0)
for _, organization := range items.Items {
result.AddOrganization(organization)
}
} else {
if len(result.Items) < queryParams.Max {
items := s.organizationsPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, organization := range items.Items {
result.AddOrganization(organization)
}
}
}
return result, response, err
}