| 1 |
package putio |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
type TransfersService struct { |
| 13 |
client *Client |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func (t *TransfersService) List(ctx context.Context) ([]Transfer, error) { |
| 19 |
req, err := t.client.NewRequest(ctx, "GET", "/v2/transfers/list", nil) |
| 20 |
if err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
|
| 24 |
var r struct { |
| 25 |
Transfers []Transfer |
| 26 |
} |
| 27 |
_, err = t.client.Do(req, &r) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
|
| 32 |
return r.Transfers, nil |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
func (t *TransfersService) Add(ctx context.Context, urlStr string, parent int64, callbackURL string) (Transfer, error) { |
| 40 |
if urlStr == "" { |
| 41 |
return Transfer{}, fmt.Errorf("empty URL") |
| 42 |
} |
| 43 |
|
| 44 |
params := url.Values{} |
| 45 |
params.Set("url", urlStr) |
| 46 |
|
| 47 |
|
| 48 |
if parent >= 0 { |
| 49 |
params.Set("save_parent_id", itoa(parent)) |
| 50 |
} |
| 51 |
if callbackURL != "" { |
| 52 |
params.Set("callback_url", callbackURL) |
| 53 |
} |
| 54 |
|
| 55 |
req, err := t.client.NewRequest(ctx, "POST", "/v2/transfers/add", strings.NewReader(params.Encode())) |
| 56 |
if err != nil { |
| 57 |
return Transfer{}, err |
| 58 |
} |
| 59 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 60 |
|
| 61 |
var r struct { |
| 62 |
Transfer Transfer |
| 63 |
} |
| 64 |
_, err = t.client.Do(req, &r) |
| 65 |
if err != nil { |
| 66 |
return Transfer{}, err |
| 67 |
} |
| 68 |
|
| 69 |
return r.Transfer, nil |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (t *TransfersService) Get(ctx context.Context, id int64) (Transfer, error) { |
| 74 |
req, err := t.client.NewRequest(ctx, "GET", "/v2/transfers/"+itoa(id), nil) |
| 75 |
if err != nil { |
| 76 |
return Transfer{}, err |
| 77 |
} |
| 78 |
|
| 79 |
var r struct { |
| 80 |
Transfer Transfer |
| 81 |
} |
| 82 |
_, err = t.client.Do(req, &r) |
| 83 |
if err != nil { |
| 84 |
return Transfer{}, err |
| 85 |
} |
| 86 |
|
| 87 |
return r.Transfer, nil |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (t *TransfersService) Retry(ctx context.Context, id int64) (Transfer, error) { |
| 92 |
params := url.Values{} |
| 93 |
params.Set("id", itoa(id)) |
| 94 |
|
| 95 |
req, err := t.client.NewRequest(ctx, "POST", "/v2/transfers/retry", strings.NewReader(params.Encode())) |
| 96 |
if err != nil { |
| 97 |
return Transfer{}, err |
| 98 |
} |
| 99 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 100 |
|
| 101 |
var r struct { |
| 102 |
Transfer Transfer |
| 103 |
} |
| 104 |
_, err = t.client.Do(req, &r) |
| 105 |
if err != nil { |
| 106 |
return Transfer{}, err |
| 107 |
} |
| 108 |
|
| 109 |
return r.Transfer, nil |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func (t *TransfersService) Cancel(ctx context.Context, ids ...int64) error { |
| 114 |
if len(ids) == 0 { |
| 115 |
return fmt.Errorf("no id given") |
| 116 |
} |
| 117 |
|
| 118 |
var transfers []string |
| 119 |
for _, id := range ids { |
| 120 |
transfers = append(transfers, itoa(id)) |
| 121 |
} |
| 122 |
|
| 123 |
params := url.Values{} |
| 124 |
params.Set("transfer_ids", strings.Join(transfers, ",")) |
| 125 |
|
| 126 |
req, err := t.client.NewRequest(ctx, "POST", "/v2/transfers/cancel", strings.NewReader(params.Encode())) |
| 127 |
if err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 131 |
|
| 132 |
_, err = t.client.Do(req, &struct{}{}) |
| 133 |
if err != nil { |
| 134 |
return err |
| 135 |
} |
| 136 |
|
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
func (t *TransfersService) Clean(ctx context.Context) error { |
| 142 |
req, err := t.client.NewRequest(ctx, "POST", "/v2/transfers/clean", nil) |
| 143 |
if err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 147 |
|
| 148 |
_, err = t.client.Do(req, &struct{}{}) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
|
| 153 |
return nil |
| 154 |
} |