This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
/
foreach.go
55 lines (50 loc) · 1.53 KB
/
foreach.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
package itercal
import (
"context"
"fmt"
"time"
directory "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/calendar/v3"
)
func ForEachEvent(ctx context.Context, srv *calendar.Service, calendarId string, start, end time.Time, f func(*calendar.Event) error) error {
ec := srv.Events.List(calendarId).
Context(ctx).
ShowDeleted(false).SingleEvents(true).
TimeMin(start.Format(time.RFC3339)).
TimeMax(end.Format(time.RFC3339)).
OrderBy("startTime")
return ec.Pages(ctx, func(events *calendar.Events) error {
for _, item := range events.Items {
if err := f(item); err != nil {
return err
}
}
return nil
})
}
func ForEachBuilding(ctx context.Context, srv *directory.Service, f func(b *directory.Building) error) error {
bc := srv.Resources.Buildings.List("my_customer").Context(ctx)
return bc.Pages(ctx, func(buildings *directory.Buildings) error {
for _, b := range buildings.Buildings {
if err := f(b); err != nil {
return err
}
}
return nil
})
}
func ForEachResourceInBuilding(ctx context.Context, srv *directory.Service, buildingId string, f func(r *directory.CalendarResource) error) error {
qstr := "resourceCategory=CONFERENCE_ROOM"
if buildingId != "" {
qstr = fmt.Sprintf("buildingId=%s AND %s", buildingId, qstr)
}
rc := srv.Resources.Calendars.List("my_customer").Context(ctx).Query(qstr)
return rc.Pages(ctx, func(calendars *directory.CalendarResources) error {
for _, c := range calendars.Items {
if err := f(c); err != nil {
return err
}
}
return nil
})
}