/
places_api.go
235 lines (180 loc) · 6.92 KB
/
places_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
package webexteams
import (
"time"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// PlacesService is the service to communicate with the Places API endpoint
type PlacesService service
// PlaceCreateRequest is the Place Create Request Parameters
type PlaceCreateRequest struct {
Title string `json:"title,omitempty"` // A user-friendly name for the place.
TeamID string `json:"teamId,omitempty"` // The ID for the team with which this place is associated.
}
// PlaceUpdateRequest is the Place Update Request Parameters
type PlaceUpdateRequest struct {
Title string `json:"title,omitempty"` // Place title.
}
// Place is the Place definition
type Place struct {
ID string `json:"id,omitempty"` // Place ID.
Title string `json:"title,omitempty"` // Place title.
PlaceType string `json:"type,omitempty"` // Place type (group or direct).
IsLocked bool `json:"isLocked,omitempty"` // Place is moderated.
TeamID string `json:"teamId,omitempty"` // Place Team ID.
CreatorID string `json:"creatorId,omitempty"` // Place creator Person ID.
LastActivity time.Time `json:"lastActivity,omitempty"` // Place last activity date/time.
Created time.Time `json:"created,omitempty"` // Place creation date/time.
}
// Places is the List of Places
type Places struct {
Items []Place `json:"items,omitempty"`
}
// AddPlace is used to append a place to a slice of places
func (places *Places) AddPlace(item Place) []Place {
places.Items = append(places.Items, item)
return places.Items
}
func (s *PlacesService) placesPagination(linkHeader string, size, max int) *Places {
items := &Places{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := s.client.R().
SetResult(&Places{}).
SetError(&Error{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Places)
size = size + len(items.Items)
if max < 0 || size < max {
places := s.placesPagination(response.Header().Get("Link"), size, max)
for _, place := range places.Items {
items.AddPlace(place)
}
}
}
}
return items
}
// CreatePlace Creates a place. The authenticated user is automatically added as a member of the place.
/* Creates a place. The authenticated user is automatically added as a member of the place. See the Memberships API to learn how to add more people to the place.
To create a 1-to-1 place, use the Create Messages endpoint to send a message directly to another person by using the toPersonID or toPersonEmail parameters.
@param placeCreateRequest
@return Place
*/
func (s *PlacesService) CreatePlace(placeCreateRequest *PlaceCreateRequest) (*Place, *resty.Response, error) {
path := "/places/"
response, err := s.client.R().
SetBody(placeCreateRequest).
SetResult(&Place{}).
SetError(&Error{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Place)
return result, response, err
}
// DeletePlace Deletes a place, by ID. Deleted places cannot be recovered.
/* Deletes a place, by ID. Deleted places cannot be recovered.
Deleting a place that is part of a team will archive the place instead.
Specify the place ID in the placeID parameter in the URI
@param placeID Place ID.
@return
*/
func (s *PlacesService) DeletePlace(placeID string) (*resty.Response, error) {
path := "/places/" + placeID
response, err := s.client.R().
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
// GetPlace Shows details for a place, by ID.
/* Shows details for a place, by ID.
The title of the place for 1-to-1 places will be the display name of the other person.
Specify the place ID in the placeID parameter in the URI.
@param placeID Place ID.
@return Place
*/
func (s *PlacesService) GetPlace(placeID string) (*Place, *resty.Response, error) {
path := "/places/" + placeID
response, err := s.client.R().
SetResult(&Place{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Place)
return result, response, err
}
// ListPlacesQueryParams are the query params for the ListPlaces API Call
type ListPlacesQueryParams struct {
TeamID string `url:"teamId,omitempty"` // Limit the places to those associated with a team, by ID.
PlaceType string `url:"type,omitempty"` // direct returns all 1-to-1 places. group returns all group places.
SortBy string `url:"sortBy,omitempty"` // Sort results by place ID (id), most recent activity (lastactivity), or most recently created (created).
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response. Negative value will list all items (use this carefully).
RequestBy int `url:"-"` // Number of items to retrieve by requests (Max if let at 0)
}
// ListPlaces List places.
/* List places.
The title of the place for 1-to-1 places will be the display name of the other person.
By default, lists places to which the authenticated user belongs.
Long result sets will be split into pages.
@param "teamId" (string) Limit the places to those associated with a team, by ID.
@param "type_" (string) direct returns all 1-to-1 places. group returns all group places.
@param "sortBy" (string) Sort results by place ID (id), most recent activity (lastactivity), or most recently created (created).
@param "max" (int) Limit the maximum number of items in the response. Negative value will list all items (use this carefully).
@param "requestBy" (int) Number of items by request
@return Places
*/
func (s *PlacesService) ListPlaces(queryParams *ListPlacesQueryParams) (*Places, *resty.Response, error) {
path := "/places/"
max := queryParams.Max
if queryParams.RequestBy > 0 {
queryParams.Max = queryParams.RequestBy
} else if queryParams.Max < 0 {
queryParams.Max = 0
}
queryParamsString, _ := query.Values(queryParams)
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Places{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Places)
if max < 0 || len(result.Items) < max {
items := s.placesPagination(response.Header().Get("Link"), len(result.Items), max)
for _, place := range items.Items {
result.AddPlace(place)
}
}
return result, response, err
}
// UpdatePlace Updates details for a place, by ID.
/* Updates details for a place, by ID.
Specify the place ID in the placeID parameter in the URI.
@param placeID Place ID.
@param placeUpdateRequest
@return Place
*/
func (s *PlacesService) UpdatePlace(placeID string, placeUpdateRequest *PlaceUpdateRequest) (*Place, *resty.Response, error) {
path := "/places/" + placeID
response, err := s.client.R().
SetBody(placeUpdateRequest).
SetResult(&Place{}).
SetError(&Error{}).
Put(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Place)
return result, response, err
}