-
Notifications
You must be signed in to change notification settings - Fork 2
/
labels.go
77 lines (61 loc) · 1.65 KB
/
labels.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
package fortnox
import (
"context"
"fmt"
)
// Label data type
type Label struct {
ID int `json:"Id"`
Description string `json:"Description,omitempty"`
}
// ListLabelsResp Response for multiple labels
type ListLabelsResp struct {
Labels []*Label `json:"Labels"`
}
// ListLabels lists labels
func (c *Client) ListLabels(ctx context.Context) ([]*Label, error) {
resp := &ListLabelsResp{}
err := c.request(ctx, "GET", "labels", nil, nil, resp)
if err != nil {
return nil, err
}
return resp.Labels, nil
}
// CreateLabelReq is the request used for creating labels
type CreateLabelReq struct {
Label struct {
Description string `json:"Description"`
} `json:"Label"`
}
// LabelResp Response for single label
type LabelResp struct {
Label Label `json:"Label"`
}
// CreateLabel creates a label
func (c *Client) CreateLabel(ctx context.Context, name string) (*Label, error) {
resp := &LabelResp{}
req := CreateLabelReq{}
req.Label.Description = name
err := c.request(ctx, "POST", "labels", &req, nil, resp)
if err != nil {
return nil, err
}
return &resp.Label, nil
}
// An UpdateLabelsReq is used in updating labels
type UpdateLabelsReq CreateLabelReq
// UpdateLabel updates a label
func (c *Client) UpdateLabel(ctx context.Context, id int, name string) (*Label, error) {
resp := &LabelResp{}
req := CreateLabelReq{}
req.Label.Description = name
err := c.request(ctx, "PUT", fmt.Sprintf("labels/%d", id), &req, nil, resp)
if err != nil {
return nil, err
}
return &resp.Label, nil
}
// DeleteLabel deletes a label
func (c *Client) DeleteLabel(ctx context.Context, id int) error {
return c.deleteResource(ctx, fmt.Sprintf("labels/%d", id))
}