-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
251 lines (229 loc) · 6.27 KB
/
calendar.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
package koyomi
import (
"context"
"log"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/shogo82148/go-retry"
"github.com/tkuchiki/parsetime"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)
type Event struct {
ID string `json:"id"`
Summary string `json:"summary"`
Description string `json:"description"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
func newEvent(ev *calendar.Event) (*Event, error) {
var startTime, endTime time.Time
p, err := parsetime.NewParseTime()
if err != nil {
return nil, errors.Wrap(err, "error parsetime.NewParseTime")
}
if ev.Start != nil {
v := ev.Start.DateTime
if v == "" {
v = ev.Start.Date
}
startTime, err = p.Parse(v)
if err != nil {
return nil, errors.Wrap(err, "error Parse")
}
}
if ev.End != nil {
v := ev.End.DateTime
if v == "" {
v = ev.End.Date
}
endTime, err = p.Parse(v)
if err != nil {
return nil, errors.Wrap(err, "error Parse")
}
}
return &Event{
ID: ev.Id,
Summary: ev.Summary,
Description: ev.Description,
StartTime: startTime,
EndTime: endTime,
}, nil
}
type CalendarService interface {
List(ctx context.Context, calendarID string, startTime, endTime time.Time) ([]*Event, error)
Insert(ctx context.Context, calendarID string, event *Event) (*Event, error)
Update(ctx context.Context, calendarID string, event *Event) (*Event, error)
Delete(ctx context.Context, calendarID, eventID string) error
}
type calendarService struct {
cs *calendar.Service
}
var policy = retry.Policy{
MinDelay: time.Second,
MaxDelay: 100 * time.Second,
MaxCount: 10,
}
func newCalendarService(ctx context.Context, credentialPath string) (CalendarService, error) {
cs, err := calendar.NewService(ctx, option.WithCredentialsFile(credentialPath))
if err != nil {
return nil, errors.Wrap(err, "error calendar.NewService")
}
return &calendarService{cs: cs}, nil
}
func (s *calendarService) List(ctx context.Context, calendarID string, startTime, endTime time.Time) ([]*Event, error) {
evs := []*calendar.Event{}
req := s.cs.Events.List(calendarID).
TimeMin(startTime.Format(time.RFC3339)).
TimeMax(endTime.Format(time.RFC3339)).
OrderBy("startTime").
SingleEvents(true)
pageToken := ""
for {
retrier := policy.Start(ctx)
var resp *calendar.Events
for retrier.Continue() {
var err error
resp, err = req.PageToken(pageToken).Context(ctx).Do()
if err == nil {
break
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusTooManyRequests {
log.Printf("[WARN] reached to Too Many Requests: calendar_id=%s", calendarID)
continue
}
}
return nil, errors.Wrap(err, "error Events.List")
}
evs = append(evs, resp.Items...)
if resp.NextPageToken == "" {
break
}
pageToken = resp.NextPageToken
}
result := make([]*Event, 0, len(evs))
for _, ev := range evs {
event, err := newEvent(ev)
if err != nil {
return nil, errors.Wrap(err, "error newEvent")
}
result = append(result, event)
}
return result, nil
}
func (s *calendarService) Insert(ctx context.Context, calendarID string, event *Event) (*Event, error) {
ev := &calendar.Event{
Id: event.ID,
Summary: event.Summary,
Description: event.Description,
Start: &calendar.EventDateTime{
DateTime: event.StartTime.Format(time.RFC3339),
},
End: &calendar.EventDateTime{
DateTime: event.EndTime.Format(time.RFC3339),
},
}
req := s.cs.Events.Insert(calendarID, ev)
retrier := policy.Start(ctx)
var resp *calendar.Event
for retrier.Continue() {
var err error
resp, err = req.Context(ctx).Do()
if err == nil {
break
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusTooManyRequests {
log.Printf("[WARN] reached to Too Many Requests: calendar_id=%s", calendarID)
continue
}
}
return nil, errors.Wrap(err, "error Events.Insert")
}
result, err := newEvent(resp)
if err != nil {
return nil, errors.Wrap(err, "error newEvent")
}
return result, nil
}
func (s *calendarService) Update(ctx context.Context, calendarID string, event *Event) (*Event, error) {
ev, err := s.get(ctx, calendarID, event.ID)
if err != nil {
return nil, errors.Wrap(err, "error get")
}
if event.Summary != "" {
ev.Summary = event.Summary
}
if event.Description != "" {
ev.Description = event.Description
}
if !event.StartTime.IsZero() {
ev.Start.DateTime = event.StartTime.Format(time.RFC3339)
}
if !event.EndTime.IsZero() {
ev.End.DateTime = event.EndTime.Format(time.RFC3339)
}
req := s.cs.Events.Update(calendarID, ev.Id, ev)
retrier := policy.Start(ctx)
var resp *calendar.Event
for retrier.Continue() {
var err error
resp, err = req.Context(ctx).Do()
if err == nil {
break
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusTooManyRequests {
log.Printf("[WARN] reached to Too Many Requests: calendar_id=%s", calendarID)
continue
}
}
return nil, errors.Wrap(err, "error Events.Update")
}
result, err := newEvent(resp)
if err != nil {
return nil, errors.Wrap(err, "error newEvent")
}
return result, nil
}
func (s *calendarService) get(ctx context.Context, calendarID string, eventID string) (*calendar.Event, error) {
req := s.cs.Events.Get(calendarID, eventID)
retrier := policy.Start(ctx)
var ev *calendar.Event
for retrier.Continue() {
var err error
ev, err = req.Context(ctx).Do()
if err == nil {
break
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusTooManyRequests {
log.Printf("[WARN] reached to Too Many Requests: calendar_id=%s", calendarID)
continue
}
}
return nil, errors.Wrap(err, "error Events.Get")
}
return ev, nil
}
func (s *calendarService) Delete(ctx context.Context, calendarID, eventID string) error {
req := s.cs.Events.Delete(calendarID, eventID)
retrier := policy.Start(ctx)
for retrier.Continue() {
err := req.Context(ctx).Do()
if err == nil {
break
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusTooManyRequests {
log.Printf("[WARN] reached to Too Many Requests: calendar_id=%s", calendarID)
continue
}
}
return errors.Wrap(err, "error Events.Delete")
}
return nil
}