-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.go
168 lines (136 loc) · 4.14 KB
/
cli.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
160
161
162
163
164
165
166
167
168
package qbt_apiv2
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"golang.org/x/net/publicsuffix"
)
type Client struct {
httpCli *http.Client
URL string
// API `sync/maindata`` Parameter `rid`
rid int
mainData *MainData
}
// NewCli v2
func NewCli(url string, auth ...string) (*Client, error) {
client := new(Client)
// ensure url ends with "/"
if url[len(url)-1:] != "/" {
url += "/"
}
client.URL = url + "api/v2/"
// create cookie jar
cliJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
client.httpCli = &http.Client{
Jar: cliJar,
}
nreq := len(auth) == 0
var (
err error
)
if nreq {
err = client.Login("", "")
} else {
err = client.Login(auth[0], auth[1])
}
if err != nil {
return nil, err
}
return client, nil
}
// Common Methods for HTTP Requests
// Use POST request to send x-www-form-urlencoded encoding.
func (c *Client) postXwwwFormUrlencoded(endpoint string, opts Optional) (*http.Response, error) {
values := url.Values{}
for k, v := range opts.StringField() {
values.Set(k, v)
}
req, err := http.NewRequest("POST", c.URL+endpoint, bytes.NewBufferString(values.Encode()))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.httpCli.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform request: %w", err)
}
return resp, nil
}
// writeOptions will write a map to the buffer through multipart.NewWriter
func writeOptions(writer *multipart.Writer, opts Optional) {
ws := opts.StringField()
for key, val := range ws {
writer.WriteField(key, val)
}
}
// postMultipart will perform a multiple part POST request
func (c *Client) postMultipart(endpoint string, buffer bytes.Buffer, contentType string) (*http.Response, error) {
req, err := http.NewRequest("POST", c.URL+endpoint, &buffer)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
// add the content-type so qbittorrent knows what to expect
req.Header.Set("Content-Type", contentType)
resp, err := c.httpCli.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform request: %w", err)
}
return resp, nil
}
// postMultipartData will perform a multiple part POST request without a file
func (c *Client) postMultipartData(endpoint string, opts Optional) (*http.Response, error) {
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
// write the options to the buffer
// will contain the link string
writeOptions(writer, opts)
// close the writer before doing request to get closing line on multipart request
if err := writer.Close(); err != nil {
return nil, fmt.Errorf("failed to close writer: %w", err)
}
resp, err := c.postMultipart(endpoint, buffer, writer.FormDataContentType())
if err != nil {
return nil, err
}
return resp, nil
}
// postMultipartFile will perform a multiple part POST request with a file
func (c *Client) postMultipartFile(endpoint string, fileName string, opts Optional) (*http.Response, error) {
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
// open the file for reading
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
// defer the closing of the file until the end of function
// so we can still copy its contents
defer file.Close()
// create form for writing the file to and give it the filename
formWriter, err := writer.CreateFormFile("torrents", path.Base(fileName))
if err != nil {
return nil, fmt.Errorf("error adding file: %w", err)
}
// write the options to the buffer
writeOptions(writer, opts)
// copy the file contents into the form
if _, err = io.Copy(formWriter, file); err != nil {
return nil, fmt.Errorf("error copying file: %w", err)
}
// close the writer before doing request to get closing line on multipart request
if err := writer.Close(); err != nil {
return nil, fmt.Errorf("failed to close writer: %w", err)
}
resp, err := c.postMultipart(endpoint, buffer, writer.FormDataContentType())
if err != nil {
return nil, err
}
return resp, nil
}