-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-calendars.go
163 lines (139 loc) · 4.17 KB
/
get-calendars.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
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 (
GetCalendarsComponent = "google_get_calendars"
GetCalendarsRequestPort = "request"
GetCalendarsResponsePort = "response"
GetCalendarsErrorPort = "error"
)
type GetCalendarsContext any
type GetCalendarsSettings struct {
EnableErrorPort bool `json:"enableErrorPort" required:"true" title:"Enable Error Port" description:"If request may fail, error port will emit an error message"`
}
type GetCalendars struct {
settings GetCalendarsSettings
}
type GetCalendarsRequest struct {
Context GetCalendarsContext `json:"context" title:"Context" configurable:"true" propertyOrder:"1"`
Config ClientConfig `json:"config" title:"Config" required:"true" description:"Client Config" propertyOrder:"2"`
Token Token `json:"token" required:"true" title:"Auth Token" propertyOrder:"7"`
}
type GetCalendarsResponse struct {
Request GetCalendarsRequest `json:"request"`
Calendars []*calendar.CalendarListEntry `json:"calendars"`
}
type GetCalendarsError struct {
Request GetCalendarsRequest `json:"request"`
Error string `json:"error"`
}
func (g *GetCalendars) GetInfo() module.ComponentInfo {
return module.ComponentInfo{
Name: GetCalendarsComponent,
Description: "Get calendars list",
Info: "Gets list of Google calendars",
Tags: []string{"google", "calendar", "auth"},
}
}
func (g *GetCalendars) Handle(ctx context.Context, output module.Handler, port string, msg interface{}) error {
if port == module.SettingsPort {
in, ok := msg.(GetCalendarsSettings)
if !ok {
return fmt.Errorf("invalid settings")
}
g.settings = in
return nil
}
if port != ExchangeAuthCodeRequestPort {
return fmt.Errorf("unknown port %s", port)
}
in, ok := msg.(GetCalendarsRequest)
if !ok {
return fmt.Errorf("invalid input message")
}
calendars, err := g.getCalendars(ctx, in)
if err != nil {
// check err port
if !g.settings.EnableErrorPort {
return err
}
return output(ctx, ExchangeAuthCodeErrorPort, GetCalendarsError{
Request: in,
Error: err.Error(),
})
}
return output(ctx, GetCalendarsResponsePort, GetCalendarsResponse{
Request: in,
Calendars: calendars,
})
}
func (c *GetCalendars) getCalendars(ctx context.Context, req GetCalendarsRequest) ([]*calendar.CalendarListEntry, 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)
}
list, err := srv.CalendarList.List().Context(ctx).Do()
if err != nil {
return nil, err
}
return list.Items, nil
}
func (g *GetCalendars) Ports() []module.Port {
ports := []module.Port{
{
Name: module.SettingsPort,
Label: "Settings",
Configuration: GetCalendarsSettings{},
Source: true,
},
{
Source: true,
Name: GetCalendarsRequestPort,
Label: "Request",
Position: module.Left,
Configuration: GetCalendarsRequest{},
},
{
Source: false,
Name: GetCalendarsResponsePort,
Label: "Response",
Position: module.Right,
Configuration: GetCalendarsResponse{},
},
}
if !g.settings.EnableErrorPort {
return ports
}
return append(ports, module.Port{
Position: module.Bottom,
Name: GetCalendarsErrorPort,
Label: "Error",
Source: false,
Configuration: GetCalendarsError{},
})
}
func (g *GetCalendars) Instance() module.Component {
return &GetCalendars{}
}
var _ module.Component = (*GetCalendars)(nil)
func init() {
registry.Register(&GetCalendars{})
}