Debian Codegraph @

calendar_event.go

raw
1 package proton
2
3 import (
4 "context"
5 "net/url"
6 "strconv"
7
8 "github.com/go-resty/resty/v2"
9 )
10
11 func (c *Client) CountCalendarEvents(ctx context.Context, calendarID string) (int, error) {
12 var res struct {
13 Total int
14 }
15
16 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
17 return r.SetResult(&res).Get("/calendar/v1/" + calendarID + "/events")
18 }); err != nil {
19 return 0, err
20 }
21
22 return res.Total, nil
23 }
24
25 // TODO: For now, the query params are partially constant -- should they be configurable?
26 func (c *Client) GetCalendarEvents(ctx context.Context, calendarID string, page, pageSize int, filter url.Values) ([]CalendarEvent, error) {
27 var res struct {
28 Events []CalendarEvent
29 }
30
31 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
32 return r.SetQueryParams(map[string]string{
33 "Page": strconv.Itoa(page),
34 "PageSize": strconv.Itoa(pageSize),
35 }).SetQueryParamsFromValues(filter).SetResult(&res).Get("/calendar/v1/" + calendarID + "/events")
36 }); err != nil {
37 return nil, err
38 }
39
40 return res.Events, nil
41 }
42
43 func (c *Client) GetAllCalendarEvents(ctx context.Context, calendarID string, filter url.Values) ([]CalendarEvent, error) {
44 total, err := c.CountCalendarEvents(ctx, calendarID)
45 if err != nil {
46 return nil, err
47 }
48
49 return fetchPaged(ctx, total, maxPageSize, c, func(ctx context.Context, page, pageSize int) ([]CalendarEvent, error) {
50 return c.GetCalendarEvents(ctx, calendarID, page, pageSize, filter)
51 })
52 }
53
54 func (c *Client) GetCalendarEvent(ctx context.Context, calendarID, eventID string) (CalendarEvent, error) {
55 var res struct {
56 Event CalendarEvent
57 }
58
59 if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
60 return r.SetResult(&res).Get("/calendar/v1/" + calendarID + "/events/" + eventID)
61 }); err != nil {
62 return CalendarEvent{}, err
63 }
64
65 return res.Event, nil
66 }