-
Notifications
You must be signed in to change notification settings - Fork 3
/
sabnzbd.go
159 lines (134 loc) · 2.51 KB
/
sabnzbd.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sabnzbd
import (
"encoding/json"
"io"
"net"
"net/http"
"net/url"
"sync"
)
type Sabnzbd struct {
mu sync.RWMutex
https bool
addr string
path string
auth authenticator
}
func New(options ...Option) (s *Sabnzbd, err error) {
s = &Sabnzbd{
addr: "localhost:8080",
path: "api",
auth: &noneAuth{},
}
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
return s, nil
}
func (s *Sabnzbd) SetOptions(options ...Option) (err error) {
for _, option := range options {
if err := option(s); err != nil {
return err
}
}
return nil
}
func (s *Sabnzbd) useHttps() {
s.mu.Lock()
defer s.mu.Unlock()
s.https = true
}
func (s *Sabnzbd) useHttp() {
s.mu.Lock()
defer s.mu.Unlock()
s.https = false
}
func (s *Sabnzbd) setAddr(addr string) error {
_, _, err := net.SplitHostPort(addr)
if err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.addr = addr
return nil
}
func (s *Sabnzbd) setPath(path string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.path = path
return nil
}
func (s *Sabnzbd) setAuth(a authenticator) error {
s.mu.Lock()
defer s.mu.Unlock()
s.auth = a
return nil
}
type sabnzbdURL struct {
*url.URL
v url.Values
auth authenticator
}
func (s *Sabnzbd) url() *sabnzbdURL {
s.mu.RLock()
defer s.mu.RUnlock()
su := &sabnzbdURL{
URL: &url.URL{
Scheme: "http",
Host: s.addr,
Path: s.path,
},
auth: s.auth,
}
if s.https {
su.Scheme = "https"
}
su.v = su.URL.Query()
return su
}
func (su *sabnzbdURL) SetJsonOutput() {
su.v.Set("output", "json")
}
func (su *sabnzbdURL) SetMode(mode string) {
su.v.Set("mode", mode)
}
func (su *sabnzbdURL) Authenticate() {
su.auth.Authenticate(su.v)
}
func (su *sabnzbdURL) String() string {
su.RawQuery = su.v.Encode()
return su.URL.String()
}
func (su *sabnzbdURL) CallJSON(r interface{}) error {
resp, err := http.Get(su.String())
if err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(r); err != nil {
return err
}
if err, ok := r.(error); ok {
return apiStringError(err.Error())
}
return nil
}
func (su *sabnzbdURL) CallJSONMultipart(reader io.Reader, contentType string, r interface{}) error {
resp, err := http.Post(su.String(), contentType, reader)
if err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(r); err != nil {
return err
}
if err, ok := r.(error); ok {
return apiStringError(err.Error())
}
return nil
}