forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 8
/
zips.go
79 lines (65 loc) · 1.67 KB
/
zips.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
package putio
import (
"context"
"net/http"
"net/url"
"strings"
)
// ZipsService is the service manage zip streams.
type ZipsService struct {
client *Client
}
// Get gives detailed information about the given zip file id.
func (z *ZipsService) Get(ctx context.Context, id int64) (Zip, error) {
req, err := z.client.NewRequest(ctx, http.MethodGet, "/v2/zips/"+itoa(id), nil)
if err != nil {
return Zip{}, err
}
var r Zip
_, err = z.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return Zip{}, err
}
return r, nil
}
// List lists active zip files.
func (z *ZipsService) List(ctx context.Context) ([]Zip, error) {
req, err := z.client.NewRequest(ctx, http.MethodGet, "/v2/zips/list", nil)
if err != nil {
return nil, err
}
var r struct {
Zips []Zip
}
_, err = z.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return nil, err
}
return r.Zips, nil
}
// Create creates zip files for given file IDs. If the operation is successful,
// a zip ID will be returned to keep track of zip process.
func (z *ZipsService) Create(ctx context.Context, fileIDs ...int64) (int64, error) {
if len(fileIDs) == 0 {
return 0, ErrNoFileIDIsGiven
}
ids := []string{}
for _, id := range fileIDs {
ids = append(ids, itoa(id))
}
params := url.Values{}
params.Set("file_ids", strings.Join(ids, ","))
req, err := z.client.NewRequest(ctx, http.MethodPost, "/v2/zips/create", strings.NewReader(params.Encode()))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var r struct {
ID int64 `json:"zip_id"`
}
_, err = z.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return 0, err
}
return r.ID, nil
}