/
tag.go
106 lines (82 loc) · 2.75 KB
/
tag.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package sonarr
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"golift.io/starr"
)
const bpTag = APIver + "/tag"
// GetTags returns all configured tags.
func (s *Sonarr) GetTags() ([]*starr.Tag, error) {
return s.GetTagsContext(context.Background())
}
// GetTagsContext returns all configured tags.
func (s *Sonarr) GetTagsContext(ctx context.Context) ([]*starr.Tag, error) {
var output []*starr.Tag
req := starr.Request{URI: bpTag}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}
return output, nil
}
// GetTag returns a single tag.
func (s *Sonarr) GetTag(tagID int) (*starr.Tag, error) {
return s.GetTagContext(context.Background(), tagID)
}
// GetTagContext returns a single tag.
func (s *Sonarr) GetTagContext(ctx context.Context, tagID int) (*starr.Tag, error) {
var output starr.Tag
req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tagID))}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}
return &output, nil
}
// AddTag creates a tag.
func (s *Sonarr) AddTag(tag *starr.Tag) (*starr.Tag, error) {
return s.AddTagContext(context.Background(), tag)
}
// AddTagContext creates a tag.
func (s *Sonarr) AddTagContext(ctx context.Context, tag *starr.Tag) (*starr.Tag, error) {
var output starr.Tag
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(tag); err != nil {
return nil, fmt.Errorf("json.Marshal(%s): %w", bpTag, err)
}
req := starr.Request{URI: bpTag, Body: &body}
if err := s.PostInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
}
return &output, nil
}
// UpdateTag updates the tag.
func (s *Sonarr) UpdateTag(tag *starr.Tag) (*starr.Tag, error) {
return s.UpdateTagContext(context.Background(), tag)
}
// UpdateTagContext updates the tag.
func (s *Sonarr) UpdateTagContext(ctx context.Context, tag *starr.Tag) (*starr.Tag, error) {
var output starr.Tag
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(tag); err != nil {
return nil, fmt.Errorf("json.Marshal(%s): %w", bpTag, err)
}
req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tag.ID)), Body: &body}
if err := s.PutInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
}
return &output, nil
}
// DeleteTag removes a single tag.
func (s *Sonarr) DeleteTag(tagID int) error {
return s.DeleteTagContext(context.Background(), tagID)
}
// DeleteTagContext removes a single tag.
func (s *Sonarr) DeleteTagContext(ctx context.Context, tagID int) error {
req := starr.Request{URI: path.Join(bpTag, fmt.Sprint(tagID))}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}
return nil
}