-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
146 lines (138 loc) · 3.29 KB
/
client.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
package qbittorrent
import (
"fmt"
"github.com/5rahim/go-qbit/application"
"github.com/5rahim/go-qbit/log"
"github.com/5rahim/go-qbit/rss"
"github.com/5rahim/go-qbit/search"
"github.com/5rahim/go-qbit/sync"
"github.com/5rahim/go-qbit/torrent"
"github.com/5rahim/go-qbit/transfer"
"github.com/rs/zerolog"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
type Client struct {
baseURL string
logger *zerolog.Logger
client *http.Client
Username string
Password string
Port int
Host string
BinaryPath string
Application qbittorrent_application.Client
Log qbittorrent_log.Client
RSS qbittorrent_rss.Client
Search qbittorrent_search.Client
Sync qbittorrent_sync.Client
Torrent qbittorrent_torrent.Client
Transfer qbittorrent_transfer.Client
}
type NewClientOptions struct {
HTTPS bool
Username string
Password string
Port int
Host string
BinaryPath string
}
func NewClient(opts *NewClientOptions) *Client {
protocol := "http"
if opts.HTTPS {
protocol = "https"
}
baseURL := fmt.Sprintf("%s://%s:%d/api/v2", protocol, opts.Host, opts.Port)
client := &http.Client{}
return &Client{
baseURL: baseURL,
client: client,
Username: opts.Username,
Password: opts.Password,
Port: opts.Port,
BinaryPath: opts.BinaryPath,
Host: opts.Host,
Application: qbittorrent_application.Client{
BaseUrl: baseURL + "/app",
Client: client,
},
Log: qbittorrent_log.Client{
BaseUrl: baseURL + "/log",
Client: client,
},
RSS: qbittorrent_rss.Client{
BaseUrl: baseURL + "/rss",
Client: client,
},
Search: qbittorrent_search.Client{
BaseUrl: baseURL + "/search",
Client: client,
},
Sync: qbittorrent_sync.Client{
BaseUrl: baseURL + "/sync",
Client: client,
},
Torrent: qbittorrent_torrent.Client{
BaseUrl: baseURL + "/torrents",
Client: client,
},
Transfer: qbittorrent_transfer.Client{
BaseUrl: baseURL + "/transfer",
Client: client,
},
}
}
func (c *Client) Login() error {
endpoint := c.baseURL + "/auth/login"
data := url.Values{}
data.Add("username", c.Username)
data.Add("password", c.Password)
request, err := http.NewRequest("POST", endpoint, strings.NewReader(data.Encode()))
if err != nil {
return err
}
request.Header.Add("content-type", "application/x-www-form-urlencoded")
resp, err := c.client.Do(request)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
}
}()
if resp.StatusCode != 200 {
return fmt.Errorf("invalid status %s", resp.Status)
}
if len(resp.Cookies()) < 1 {
return fmt.Errorf("no cookies in login response")
}
apiURL, err := url.Parse(c.baseURL)
if err != nil {
return err
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return err
}
jar.SetCookies(apiURL, []*http.Cookie{resp.Cookies()[0]})
c.client.Jar = jar
return nil
}
func (c *Client) Logout() error {
endpoint := c.baseURL + "/auth/logout"
request, err := http.NewRequest("POST", endpoint, nil)
if err != nil {
return err
}
resp, err := c.client.Do(request)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("invalid status %s", resp.Status)
}
return nil
}