File: zips.go

package info (click to toggle)
golang-github-putdotio-go-putio 0.0~git20190822.19b9c63-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 168 kB
  • sloc: makefile: 2
file content (80 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download | duplicates (2)
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
package putio

import (
	"context"
	"fmt"
	"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, "GET", "/v2/zips/"+itoa(id), nil)
	if err != nil {
		return Zip{}, err
	}

	var r Zip
	_, err = z.client.Do(req, &r)
	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, "GET", "/v2/zips/list", nil)
	if err != nil {
		return nil, err
	}

	var r struct {
		Zips []Zip
	}
	_, err = z.client.Do(req, &r)
	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, fmt.Errorf("no file id given")
	}

	var 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, "POST", "/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)
	if err != nil {
		return 0, err
	}

	return r.ID, nil
}