-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-channel-watch.go
193 lines (168 loc) · 6.44 KB
/
calendar-channel-watch.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
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"
)
const (
CalendarChannelWatchComponent = "google_calendar_channel_watch"
CalendarChannelWatchRequestPort = "request"
CalendarChannelWatchResponsePort = "response"
CalendarChannelWatchErrorPort = "error"
)
type CalendarWatchChannel struct {
ID string `json:"id" required:"true" title:"ID" description:"A UUID or similar unique string that identifies this channel."`
Type string `json:"type" required:"true" title:"Type" enum:"web_hook" enumTitles:"Webhook" description:"The type of delivery mechanism used for this channel. Valid values are \"web_hook\" (or \"webhook\"). Both values refer to a channel where Http requests are used to deliver messages."`
Address string `json:"address" required:"true" title:"Address" description:"The address where notifications are delivered for this channel."`
Expiration int64 `json:"expiration" title:"Expiration" description:"Date and time of notification channel expiration, expressed as a Unix timestamp, in milliseconds."`
ResourceId string `json:"resourceId" title:"ResourceID" description:"An opaque ID that identifies the resource being watched on this channel. Stable across different API versions."`
ResourceUri string `json:"resourceUri" title:"ResourceURI" description:"A version-specific identifier for the watched resource."`
Token string `json:"token" title:"Auth Token" description:"An arbitrary string delivered to the target address with each notification delivered over this channel."`
}
type CalendarChannelWatchSettings struct {
EnableErrorPort bool `json:"enableErrorPort" required:"true" title:"Enable Error Port" description:"If request may fail, error port will emit an error message"`
}
type CalendarChannelWatchContext any
type CalendarChannelWatchRequest struct {
Context CalendarChannelWatchContext `json:"context" configurable:"true" title:"Context" description:"Arbitrary message to be send further" propertyOrder:"1"`
Calendar CalendarChannelWatchRequestCalendar `json:"calendar" required:"true" title:"Calendar" propertyOrder:"2"`
Channel CalendarWatchChannel `json:"channel" required:"true" title:"Channel" propertyOrder:"3"`
Token Token `json:"token" required:"true" title:"Token" propertyOrder:"4"`
Config ClientConfig `json:"config" required:"true" title:"Client credentials" propertyOrder:"5"`
}
type CalendarChannelWatchRequestCalendar struct {
ID string `json:"id" required:"true" title:"Calendar ID" description:"Google Calendar ID to be watched"`
}
type CalendarChannelWatchChannel struct {
ID string `json:"id"`
}
type CalendarChannelWatchResponse struct {
Request CalendarChannelWatchRequest `json:"request"`
Channel CalendarChannelWatchChannel `json:"channel"`
}
type CalendarChannelWatchError struct {
Request CalendarChannelWatchRequest `json:"request"`
Error string `json:"error"`
}
type CalendarChannelWatch struct {
settings CalendarChannelWatchSettings
}
func (h *CalendarChannelWatch) Instance() module.Component {
return &CalendarChannelWatch{
settings: CalendarChannelWatchSettings{},
}
}
func (h *CalendarChannelWatch) GetInfo() module.ComponentInfo {
return module.ComponentInfo{
Name: CalendarChannelWatchComponent,
Description: "Watch calendar channel",
Info: "Register calendar watcher",
Tags: []string{"Google", "Calendar"},
}
}
func (h *CalendarChannelWatch) Handle(ctx context.Context, handler module.Handler, port string, msg interface{}) error {
if port == module.SettingsPort {
in, ok := msg.(CalendarChannelWatchSettings)
if !ok {
return fmt.Errorf("invalid settings")
}
h.settings = in
return nil
}
if port != CalendarChannelWatchRequestPort {
return fmt.Errorf("unknown port %s", port)
}
req, ok := msg.(CalendarChannelWatchRequest)
if !ok {
return fmt.Errorf("invalid message")
}
ch, err := h.watch(ctx, req)
if err != nil {
if !h.settings.EnableErrorPort {
return err
}
return handler(ctx, CalendarChannelWatchErrorPort, CalendarChannelWatchError{
Request: req,
Error: err.Error(),
})
}
return handler(ctx, CalendarChannelWatchResponsePort, CalendarChannelWatchResponse{
Request: req,
Channel: CalendarChannelWatchChannel{
ID: ch.Id,
},
})
}
func (h *CalendarChannelWatch) watch(ctx context.Context, req CalendarChannelWatchRequest) (*calendar.Channel, 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)
}
return srv.Events.Watch(req.Calendar.ID, &calendar.Channel{
Type: req.Channel.Type,
Address: req.Channel.Address,
Token: req.Channel.Token,
Id: req.Channel.ID,
Expiration: req.Channel.Expiration,
}).Do()
}
func (h *CalendarChannelWatch) Ports() []module.Port {
ports := []module.Port{
{
Name: module.SettingsPort,
Label: "Settings",
Configuration: CalendarChannelWatchSettings{},
Source: true,
},
{
Name: CalendarChannelWatchRequestPort,
Label: "Request",
Configuration: CalendarChannelWatchRequest{
Channel: CalendarWatchChannel{
Type: "web_hook",
},
Token: Token{
TokenType: "Bearer",
},
},
Source: true,
Position: module.Left,
},
{
Name: CalendarChannelWatchResponsePort,
Label: "Response",
Source: false,
Position: module.Right,
Configuration: CalendarChannelWatchResponse{},
},
}
if !h.settings.EnableErrorPort {
return ports
}
return append(ports, module.Port{
Name: CalendarChannelWatchErrorPort,
Label: "Error",
Source: false,
Position: module.Bottom,
Configuration: CalendarChannelWatchError{},
})
}
var _ module.Component = (*CalendarChannelWatch)(nil)
func init() {
registry.Register(&CalendarChannelWatch{})
}