-
Notifications
You must be signed in to change notification settings - Fork 18
/
label.go
82 lines (63 loc) · 1.8 KB
/
label.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
package proton
import (
"context"
"errors"
"strconv"
"github.com/go-resty/resty/v2"
)
var ErrNoSuchLabel = errors.New("no such label")
func (c *Client) GetLabel(ctx context.Context, labelID string, labelTypes ...LabelType) (Label, error) {
labels, err := c.GetLabels(ctx, labelTypes...)
if err != nil {
return Label{}, err
}
for _, label := range labels {
if label.ID == labelID {
return label, nil
}
}
return Label{}, ErrNoSuchLabel
}
func (c *Client) GetLabels(ctx context.Context, labelTypes ...LabelType) ([]Label, error) {
var labels []Label
for _, labelType := range labelTypes {
labelType := labelType
var res struct {
Labels []Label
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetQueryParam("Type", strconv.Itoa(int(labelType))).SetResult(&res).Get("/core/v4/labels")
}); err != nil {
return nil, err
}
labels = append(labels, res.Labels...)
}
return labels, nil
}
func (c *Client) CreateLabel(ctx context.Context, req CreateLabelReq) (Label, error) {
var res struct {
Label Label
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).Post("/core/v4/labels")
}); err != nil {
return Label{}, err
}
return res.Label, nil
}
func (c *Client) DeleteLabel(ctx context.Context, labelID string) error {
return c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.Delete("/core/v4/labels/" + labelID)
})
}
func (c *Client) UpdateLabel(ctx context.Context, labelID string, req UpdateLabelReq) (Label, error) {
var res struct {
Label Label
}
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetBody(req).SetResult(&res).Put("/core/v4/labels/" + labelID)
}); err != nil {
return Label{}, err
}
return res.Label, nil
}