-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-get-events.go
200 lines (170 loc) · 5.85 KB
/
calendar-get-events.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
package google
import (
"context"
"fmt"
"github.com/tiny-systems/module/module"
"github.com/tiny-systems/module/registry"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
"time"
)
const (
CalendarGetEventsComponent = "google_calendar_get_events"
CalendarGetEventsRequestPort = "request"
CalendarGetEventsResponsePort = "response"
CalendarGetEventsErrorPort = "error"
)
type CalendarGetEventsContext any
type CalendarGetEventsRequest struct {
Context CalendarGetEventsContext `json:"context" configurable:"true" title:"Context" description:"Arbitrary message to be send further" propertyOrder:"1"`
CalendarId string `json:"calendarId" required:"true" default:"primary" minLength:"1" title:"Calendar ID" propertyOrder:"2"`
ShowDeleted bool `json:"showDeleted" required:"true" title:"Show deleted events" default:"true" propertyOrder:"3"`
StartDate time.Time `json:"startDate" title:"Start date" propertyOrder:"4"`
EndDate time.Time `json:"endDate" title:"End date" propertyOrder:"5"`
SyncToken string `json:"syncToken" title:"Sync Token" propertyOrder:"6"`
PageToken string `json:"pageToken" title:"Page Token" propertyOrder:"7"`
Token Token `json:"token" required:"true" title:"Auth Token" propertyOrder:"8"`
Config ClientConfig `json:"config" required:"true" title:"Client credentials" propertyOrder:"9"`
}
type ClientConfig struct {
Credentials string `json:"credentials" required:"true" format:"textarea" title:"Credentials" description:"Google client credentials.json file content"`
Scopes []string `json:"scopes" title:"Scopes" required:"true"`
}
type CalendarGetEventsError struct {
Request CalendarGetEventsRequest `json:"request"`
Error string `json:"error"`
}
type CalendarGetEventResponse struct {
Request CalendarGetEventsRequest `json:"request"`
Results calendar.Events `json:"results"`
}
type CalendarGetEvents struct {
settings CalendarGetEventsSettings
}
type CalendarGetEventsSettings struct {
EnableErrorPort bool `json:"enableErrorPort" default:"false" required:"true" title:"Enable Error Port" description:"If request may fail, error port will emit an error message"`
}
func (c *CalendarGetEvents) GetInfo() module.ComponentInfo {
return module.ComponentInfo{
Name: CalendarGetEventsComponent,
Description: "Calendar Get Events",
Info: "Calendar Get Events",
Tags: []string{"Google", "Calendar"},
}
}
func (c *CalendarGetEvents) Handle(ctx context.Context, handler module.Handler, port string, msg interface{}) error {
if port == module.SettingsPort {
in, ok := msg.(CalendarGetEventsSettings)
if !ok {
return fmt.Errorf("invalid settings")
}
c.settings = in
return nil
}
if port != CalendarGetEventsRequestPort {
return fmt.Errorf("unknown port %s", CalendarGetEventsRequestPort)
}
req, ok := msg.(CalendarGetEventsRequest)
if !ok {
return fmt.Errorf("invalid message")
}
events, err := c.getEvents(ctx, req)
if err != nil {
if !c.settings.EnableErrorPort {
return err
}
return handler(ctx, CalendarGetEventsErrorPort, CalendarGetEventsError{
Request: req,
Error: err.Error(),
})
}
return handler(ctx, CalendarGetEventsResponsePort, CalendarGetEventResponse{
Request: req,
Results: *events,
})
}
func (c *CalendarGetEvents) getEvents(ctx context.Context, req CalendarGetEventsRequest) (*calendar.Events, error) {
config, err := google.ConfigFromJSON([]byte(req.Config.Credentials), req.Config.Scopes...)
if err != nil {
return nil, fmt.Errorf("unable to parse client secret file to config: %v", err)
}
client := config.Client(ctx, &oauth2.Token{
AccessToken: req.Token.AccessToken,
RefreshToken: req.Token.RefreshToken,
Expiry: req.Token.Expiry,
TokenType: req.Token.TokenType,
})
srv, err := calendar.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("unable to retrieve calendar client: %v", err)
}
call := srv.Events.List(req.CalendarId).ShowDeleted(req.ShowDeleted).SingleEvents(true)
if !req.StartDate.IsZero() {
call.TimeMin(req.StartDate.Format(time.RFC3339))
}
if !req.EndDate.IsZero() {
call.TimeMax(req.EndDate.Format(time.RFC3339))
}
if req.PageToken != "" {
call.PageToken(req.PageToken)
}
if req.SyncToken != "" {
call.SyncToken(req.SyncToken)
}
call.MaxResults(100).OrderBy("startTime")
events, err := call.Do()
if err != nil {
return nil, fmt.Errorf("unable to retrieve user's events: %v", err)
}
return events, nil
}
func (c *CalendarGetEvents) Ports() []module.Port {
ports := []module.Port{
{
Name: module.SettingsPort,
Label: "Settings",
Configuration: CalendarGetEventsSettings{},
Source: true,
},
{
Name: CalendarGetEventsRequestPort,
Label: "Request",
Configuration: CalendarGetEventsRequest{
Config: ClientConfig{
Scopes: []string{"https://www.googleapis.com/auth/calendar.events.readonly"},
},
CalendarId: "SomeID",
Token: Token{
TokenType: "Bearer",
},
},
Source: true,
Position: module.Left,
},
{
Name: CalendarGetEventsResponsePort,
Label: "Response",
Source: false,
Position: module.Right,
Configuration: CalendarGetEventResponse{},
}}
if !c.settings.EnableErrorPort {
return ports
}
return append(ports, module.Port{
Position: module.Bottom,
Name: CalendarGetEventsErrorPort,
Label: "Error",
Source: false,
Configuration: CalendarGetEventsError{},
})
}
func (c *CalendarGetEvents) Instance() module.Component {
return &CalendarGetEvents{}
}
var _ module.Component = (*CalendarGetEvents)(nil)
func init() {
registry.Register(&CalendarGetEvents{})
}