-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduledEvents.go
266 lines (218 loc) · 7.16 KB
/
scheduledEvents.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
package calendly
import (
"encoding/json"
"fmt"
"time"
)
// Event holds a Calendly Event object
type Event struct {
URI string `json:"uri"`
Name string `json:"name"`
Status string `json:"status"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
EventType string `json:"event_type"`
Location Location `json:"location"`
InviteesCounter InviteesCounter `json:"invitees_counter"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
EventMemberships []EventMemberships `json:"event_memberships"`
EventGuests []Guest `json:"event_guests"`
Cancellation Cancellation `json:"cancellation"`
}
type Location struct {
Type string `json:"type"`
Location string `json:"location"`
}
type InviteesCounter struct {
Total int `json:"total"`
Active int `json:"active"`
Limit int `json:"limit"`
}
type EventMemberships struct {
User string `json:"user"`
}
// Guest holds a Calendly Guest object
type Guest struct {
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Invitee holds a Calendly Invitee object
type Invitee struct {
CancelURL string `json:"cancel_url"`
CreatedAt time.Time `json:"created_at"`
Email string `json:"email"`
Event string `json:"event"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Name string `json:"name"`
NewInvitee interface{} `json:"new_invitee"`
OldInvitee interface{} `json:"old_invitee"`
QuestionsAndAnswers []CustomQuestionsAndAnswers `json:"questions_and_answers"`
RescheduleURL string `json:"reschedule_url"`
Rescheduled bool `json:"rescheduled"`
Status string `json:"status"`
TextReminderNumber interface{} `json:"text_reminder_number"`
Timezone string `json:"timezone"`
Tracking Tracking `json:"tracking"`
UpdatedAt time.Time `json:"updated_at"`
URI string `json:"uri"`
Canceled bool `json:"canceled"`
Cancellation Cancellation `json:"cancellation"`
Payment Payment `json:"payment"`
NoShow string `json:"no_show"`
}
type Cancellation struct {
CancelerType string `json:"canceler_type"`
CanceledBy string `json:"canceled_by"`
Reason string `json:"reason"`
}
type Payment struct {
ExternalID string `json:"external_id"`
Provider string `json:"provider"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
Terms string `json:"terms"`
Successful bool `json:"successful"`
}
type ListEventInviteesInput struct {
ID string
Count int
Email string
PageToken string
Sort string
Status string
}
type ListEventsInput struct {
Count int
InviteeEmail string
MaxStartTime string
MinStartTime string
Organization string
PageToken string
Sort string
Status string
User string
}
// ListEventInvitees lists all the invitees for a specific event
func (cw *CalendlyWrapper) ListEventInvitees(input *ListEventInviteesInput) ([]Invitee, error) {
var invList []Invitee
var invListResp map[string]json.RawMessage
if input.ID == "" {
return invList, fmt.Errorf("you must provide an ID")
}
url := fmt.Sprintf("%s%s%s%s", cw.baseApiUrl, "scheduled_events/", input.ID, "/invitees")
if input.Count >= 1 && input.Count <= 100 {
url += fmt.Sprintf("?count=%d", input.Count)
} else {
// Because the first param uses ? instead of & we always use count
// Easier than trying to dynamically add ? to the first param
url += "?count=20"
}
if input.Email != "" {
url += fmt.Sprintf("&email=%s", input.Email)
}
if input.PageToken != "" {
url += fmt.Sprintf("&page_token=%s", input.PageToken)
}
if input.Sort != "" {
url += fmt.Sprintf("&sort=%s", input.Sort)
}
if input.Status != "" {
url += fmt.Sprintf("&status=%s", input.Status)
}
resp, err := cw.sendGetReq(url)
if err != nil {
return invList, err
}
err = json.Unmarshal(resp, &invListResp)
if err != nil {
return invList, err
}
err = json.Unmarshal(invListResp["collection"], &invList)
if err != nil {
return invList, err
}
return invList, nil
}
// ListEvents returns an event list from the Calendly API
func (cw *CalendlyWrapper) ListEvents(input *ListEventsInput) ([]Event, error) {
var events []Event
var eventsResponse map[string]json.RawMessage
url := fmt.Sprintf("%s/scheduled_events", cw.baseApiUrl)
if input.Count >= 1 && input.Count <= 100 {
url += fmt.Sprintf("?count=%d", input.Count)
} else {
// Because the first param uses ? instead of & we always use count
// Easier than trying to dynamically add ? to the first param
url += "?count=20"
}
if input.MaxStartTime != "" {
url += fmt.Sprintf("&max_start_time=%s", input.MaxStartTime)
}
if input.MinStartTime != "" {
url += fmt.Sprintf("&min_start_time=%s", input.MinStartTime)
}
if input.Organization != "" {
url += fmt.Sprintf("&organization=%s", input.Organization)
}
if input.InviteeEmail != "" {
url += fmt.Sprintf("&email=%s", input.InviteeEmail)
}
if input.PageToken != "" {
url += fmt.Sprintf("&page_token=%s", input.PageToken)
}
if input.Sort != "" {
url += fmt.Sprintf("&sort=%s", input.Sort)
}
if input.Status != "" {
url += fmt.Sprintf("&status=%s", input.Status)
}
if input.User != "" {
url += fmt.Sprintf("&user=%s", input.User)
}
resp, err := cw.sendGetReq(url)
if err != nil {
return events, err
}
err = json.Unmarshal(resp, &eventsResponse)
if err != nil {
return events, err
}
err = json.Unmarshal(eventsResponse["collection"], &events)
if err != nil {
return events, err
}
return events, nil
}
// GetEventInvitee returns an invitee for a specific event
func (cw *CalendlyWrapper) GetEventInvitee(eventid, inviteeid string) (Invitee, error) {
var inv Invitee
var invResp map[string]Invitee
resp, err := cw.sendGetReq(fmt.Sprintf("%sscheduled_events/%s/invitees/%s", cw.baseApiUrl, eventid, inviteeid))
if err != nil {
return inv, err
}
err = json.Unmarshal(resp, &invResp)
if err != nil {
return inv, err
}
inv = invResp["resource"]
return inv, nil
}
// GetEvent gets the specified Calendly event
func (cw *CalendlyWrapper) GetEvent(id string) (Event, error) {
var event Event
var eventResponse map[string]Event
resp, err := cw.sendGetReq(fmt.Sprintf("%sscheduled_events/%s", cw.baseApiUrl, id))
if err != nil {
return event, err
}
err = json.Unmarshal(resp, &eventResponse)
if err != nil {
return event, err
}
event = eventResponse["resource"]
return event, nil
}