/
rootfolder.go
94 lines (74 loc) · 2.79 KB
/
rootfolder.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
package sonarr
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"golift.io/starr"
)
// Define Base Path for MediaManagement calls.
const bpRootFolder = APIver + "/rootFolder"
// RootFolder is the /api/v3/rootfolder endpoint.
type RootFolder struct {
Accessible bool `json:"accessible,omitempty"`
ID int64 `json:"id,omitempty"`
FreeSpace int64 `json:"freeSpace,omitempty"`
Path string `json:"path"`
UnmappedFolders []*starr.Path `json:"unmappedFolders,omitempty"`
}
// GetRootFolders returns all configured root folders.
func (s *Sonarr) GetRootFolders() ([]*RootFolder, error) {
return s.GetRootFoldersContext(context.Background())
}
// GetRootFoldersContext returns all configured root folders.
func (s *Sonarr) GetRootFoldersContext(ctx context.Context) ([]*RootFolder, error) {
var output []*RootFolder
req := starr.Request{URI: bpRootFolder}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}
return output, nil
}
// GetRootFolder returns a single root folder.
func (s *Sonarr) GetRootFolder(folderID int64) (*RootFolder, error) {
return s.GetRootFolderContext(context.Background(), folderID)
}
// GetRootFolderContext returns a single root folder.
func (s *Sonarr) GetRootFolderContext(ctx context.Context, folderID int64) (*RootFolder, error) {
var output RootFolder
req := starr.Request{URI: path.Join(bpRootFolder, fmt.Sprint(folderID))}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}
return &output, nil
}
// AddRootFolder creates a root folder.
func (s *Sonarr) AddRootFolder(folder *RootFolder) (*RootFolder, error) {
return s.AddRootFolderContext(context.Background(), folder)
}
// AddRootFolderContext creates a root folder.
func (s *Sonarr) AddRootFolderContext(ctx context.Context, folder *RootFolder) (*RootFolder, error) {
var output RootFolder
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(folder); err != nil {
return nil, fmt.Errorf("json.Marshal(%s): %w", bpRootFolder, err)
}
req := starr.Request{URI: bpRootFolder, Body: &body}
if err := s.PostInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
}
return &output, nil
}
// DeleteRootFolder removes a single root folder.
func (s *Sonarr) DeleteRootFolder(folderID int64) error {
return s.DeleteRootFolderContext(context.Background(), folderID)
}
// DeleteRootFolderContext removes a single root folder.
func (s *Sonarr) DeleteRootFolderContext(ctx context.Context, folderID int64) error {
req := starr.Request{URI: path.Join(bpRootFolder, fmt.Sprint(folderID))}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}
return nil
}