| 1 |
package putio |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type ZipsService struct { |
| 12 |
client *Client |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
func (z *ZipsService) Get(ctx context.Context, id int64) (Zip, error) { |
| 17 |
req, err := z.client.NewRequest(ctx, "GET", "/v2/zips/"+itoa(id), nil) |
| 18 |
if err != nil { |
| 19 |
return Zip{}, err |
| 20 |
} |
| 21 |
|
| 22 |
var r Zip |
| 23 |
_, err = z.client.Do(req, &r) |
| 24 |
if err != nil { |
| 25 |
return Zip{}, err |
| 26 |
} |
| 27 |
|
| 28 |
return r, nil |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func (z *ZipsService) List(ctx context.Context) ([]Zip, error) { |
| 34 |
req, err := z.client.NewRequest(ctx, "GET", "/v2/zips/list", nil) |
| 35 |
if err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
|
| 39 |
var r struct { |
| 40 |
Zips []Zip |
| 41 |
} |
| 42 |
_, err = z.client.Do(req, &r) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
return r.Zips, nil |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func (z *ZipsService) Create(ctx context.Context, fileIDs ...int64) (int64, error) { |
| 53 |
if len(fileIDs) == 0 { |
| 54 |
return 0, fmt.Errorf("no file id given") |
| 55 |
} |
| 56 |
|
| 57 |
var ids []string |
| 58 |
for _, id := range fileIDs { |
| 59 |
ids = append(ids, itoa(id)) |
| 60 |
} |
| 61 |
|
| 62 |
params := url.Values{} |
| 63 |
params.Set("file_ids", strings.Join(ids, ",")) |
| 64 |
|
| 65 |
req, err := z.client.NewRequest(ctx, "POST", "/v2/zips/create", strings.NewReader(params.Encode())) |
| 66 |
if err != nil { |
| 67 |
return 0, err |
| 68 |
} |
| 69 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 70 |
|
| 71 |
var r struct { |
| 72 |
ID int64 `json:"zip_id"` |
| 73 |
} |
| 74 |
_, err = z.client.Do(req, &r) |
| 75 |
if err != nil { |
| 76 |
return 0, err |
| 77 |
} |
| 78 |
|
| 79 |
return r.ID, nil |
| 80 |
} |