-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint.go
58 lines (49 loc) · 1.53 KB
/
endpoint.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
package getevents
import (
"context"
"encoding/json"
"github.com/open-integration/oi/catalog/services/google-calendar/types"
api "github.com/open-integration/oi/pkg/api/v1"
"github.com/open-integration/oi/pkg/logger"
"github.com/open-integration/oi/pkg/service"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
func Handle(ctx context.Context, lgr logger.Logger, svc *service.Service, req *api.CallRequest) (*api.CallResponse, error) {
args := &types.GoogleCalendarEventsArgumentsSchema{}
if err := service.UnmarshalRequestArgumentsInto(req, args); err != nil {
return nil, err
}
calendar, err := connect(ctx, args.ServiceAccount)
if err != nil {
return service.BuildErrorResponse(err)
}
call := calendar.Events.List(args.CalendarID)
call.Context(ctx)
call.ShowDeleted(*args.ShowDeleted)
call.TimeMin(*args.TimeMin)
call.TimeMax(*args.TimeMax)
call.SingleEvents(true)
events, err := call.Do()
if err != nil {
return service.BuildErrorResponse(err)
}
return service.BuildSuccessfullResponse(events.Items)
}
func connect(ctx context.Context, serviceAccount types.ServiceAccount) (*calendar.Service, error) {
b, err := json.Marshal(serviceAccount)
if err != nil {
return nil, err
}
scopes := []string{
calendar.CalendarScope,
calendar.CalendarEventsReadonlyScope,
}
config, err := google.JWTConfigFromJSON(b, scopes...)
if err != nil {
return nil, err
}
client := config.Client(context.Background())
return calendar.NewService(ctx, option.WithHTTPClient(client))
}