forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 8
/
transfers.go
154 lines (128 loc) · 3.88 KB
/
transfers.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
package putio
import (
"context"
"net/http"
"net/url"
"strings"
)
// TransfersService is the service to operate on torrent transfers, such as
// adding a torrent or magnet link, retrying a current one etc.
type TransfersService struct {
client *Client
}
// List lists all active transfers. If a transfer is completed, it will not be
// available in response.
func (t *TransfersService) List(ctx context.Context) ([]Transfer, error) {
req, err := t.client.NewRequest(ctx, http.MethodGet, "/v2/transfers/list", nil)
if err != nil {
return nil, err
}
var r struct {
Transfers []Transfer
}
_, err = t.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return nil, err
}
return r.Transfers, nil
}
// Add creates a new transfer. A valid torrent or a magnet URL is expected.
// Parent is the folder where the new transfer is downloaded to. If a negative
// value is given, user's preferred download folder is used. CallbackURL is
// used to send a POST request after the transfer is finished downloading.
func (t *TransfersService) Add(ctx context.Context, urlStr string, parent int64, callbackURL string) (Transfer, error) {
if urlStr == "" {
return Transfer{}, ErrEmptyURL
}
params := url.Values{}
params.Set("url", urlStr)
// negative values indicate user's preferred download folder. don't include
// it in the request
if parent >= 0 {
params.Set("save_parent_id", itoa(parent))
}
if callbackURL != "" {
params.Set("callback_url", callbackURL)
}
req, err := t.client.NewRequest(ctx, http.MethodPost, "/v2/transfers/add", strings.NewReader(params.Encode()))
if err != nil {
return Transfer{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var r struct {
Transfer Transfer
}
_, err = t.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return Transfer{}, err
}
return r.Transfer, nil
}
// Get returns the given transfer's properties.
func (t *TransfersService) Get(ctx context.Context, id int64) (Transfer, error) {
req, err := t.client.NewRequest(ctx, http.MethodGet, "/v2/transfers/"+itoa(id), nil)
if err != nil {
return Transfer{}, err
}
var r struct {
Transfer Transfer
}
_, err = t.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return Transfer{}, err
}
return r.Transfer, nil
}
// Retry retries previously failed transfer.
func (t *TransfersService) Retry(ctx context.Context, id int64) (Transfer, error) {
params := url.Values{}
params.Set("id", itoa(id))
req, err := t.client.NewRequest(ctx, http.MethodPost, "/v2/transfers/retry", strings.NewReader(params.Encode()))
if err != nil {
return Transfer{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var r struct {
Transfer Transfer
}
_, err = t.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return Transfer{}, err
}
return r.Transfer, nil
}
// Cancel deletes given transfers.
func (t *TransfersService) Cancel(ctx context.Context, ids ...int64) error {
if len(ids) == 0 {
return ErrNoFileIDIsGiven
}
transfers := []string{}
for _, id := range ids {
transfers = append(transfers, itoa(id))
}
params := url.Values{}
params.Set("transfer_ids", strings.Join(transfers, ","))
req, err := t.client.NewRequest(ctx, http.MethodPost, "/v2/transfers/cancel", strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = t.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}
// Clean removes completed transfers from the transfer list.
func (t *TransfersService) Clean(ctx context.Context) error {
req, err := t.client.NewRequest(ctx, http.MethodPost, "/v2/transfers/clean", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = t.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}