File: calendar_event.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (66 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download
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
package proton

import (
	"context"
	"net/url"
	"strconv"

	"github.com/go-resty/resty/v2"
)

func (c *Client) CountCalendarEvents(ctx context.Context, calendarID string) (int, error) {
	var res struct {
		Total int
	}

	if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
		return r.SetResult(&res).Get("/calendar/v1/" + calendarID + "/events")
	}); err != nil {
		return 0, err
	}

	return res.Total, nil
}

// TODO: For now, the query params are partially constant -- should they be configurable?
func (c *Client) GetCalendarEvents(ctx context.Context, calendarID string, page, pageSize int, filter url.Values) ([]CalendarEvent, error) {
	var res struct {
		Events []CalendarEvent
	}

	if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
		return r.SetQueryParams(map[string]string{
			"Page":     strconv.Itoa(page),
			"PageSize": strconv.Itoa(pageSize),
		}).SetQueryParamsFromValues(filter).SetResult(&res).Get("/calendar/v1/" + calendarID + "/events")
	}); err != nil {
		return nil, err
	}

	return res.Events, nil
}

func (c *Client) GetAllCalendarEvents(ctx context.Context, calendarID string, filter url.Values) ([]CalendarEvent, error) {
	total, err := c.CountCalendarEvents(ctx, calendarID)
	if err != nil {
		return nil, err
	}

	return fetchPaged(ctx, total, maxPageSize, c, func(ctx context.Context, page, pageSize int) ([]CalendarEvent, error) {
		return c.GetCalendarEvents(ctx, calendarID, page, pageSize, filter)
	})
}

func (c *Client) GetCalendarEvent(ctx context.Context, calendarID, eventID string) (CalendarEvent, error) {
	var res struct {
		Event CalendarEvent
	}

	if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
		return r.SetResult(&res).Get("/calendar/v1/" + calendarID + "/events/" + eventID)
	}); err != nil {
		return CalendarEvent{}, err
	}

	return res.Event, nil
}