-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
74 lines (62 loc) · 1.82 KB
/
Copy pathclient.go
File metadata and controls
74 lines (62 loc) · 1.82 KB
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
package calendar
import (
"context"
"fmt"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
"github.com/open-cli-collective/google-readonly/internal/auth"
)
// Client wraps the Google Calendar API service
type Client struct {
service *calendar.Service
}
// NewClient creates a new Calendar client with OAuth2 authentication
func NewClient(ctx context.Context) (*Client, error) {
client, err := auth.GetHTTPClient(ctx)
if err != nil {
return nil, err
}
srv, err := calendar.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("unable to create Calendar service: %w", err)
}
return &Client{
service: srv,
}, nil
}
// ListCalendars returns all calendars the user has access to
func (c *Client) ListCalendars() ([]*calendar.CalendarListEntry, error) {
resp, err := c.service.CalendarList.List().Do()
if err != nil {
return nil, fmt.Errorf("failed to list calendars: %w", err)
}
return resp.Items, nil
}
// ListEvents returns events from the specified calendar within the given time range
func (c *Client) ListEvents(calendarID string, timeMin, timeMax string, maxResults int64) ([]*calendar.Event, error) {
call := c.service.Events.List(calendarID).
SingleEvents(true).
OrderBy("startTime")
if timeMin != "" {
call = call.TimeMin(timeMin)
}
if timeMax != "" {
call = call.TimeMax(timeMax)
}
if maxResults > 0 {
call = call.MaxResults(maxResults)
}
resp, err := call.Do()
if err != nil {
return nil, fmt.Errorf("failed to list events: %w", err)
}
return resp.Items, nil
}
// GetEvent retrieves a single event by ID
func (c *Client) GetEvent(calendarID, eventID string) (*calendar.Event, error) {
event, err := c.service.Events.Get(calendarID, eventID).Do()
if err != nil {
return nil, fmt.Errorf("failed to get event: %w", err)
}
return event, nil
}