| 1 |
package putio |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
type FriendsService struct { |
| 10 |
client *Client |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
func (f *FriendsService) List(ctx context.Context) ([]Friend, error) { |
| 15 |
req, err := f.client.NewRequest(ctx, "GET", "/v2/friends/list", nil) |
| 16 |
if err != nil { |
| 17 |
return nil, err |
| 18 |
} |
| 19 |
|
| 20 |
var r struct { |
| 21 |
Friends []Friend |
| 22 |
Total int |
| 23 |
} |
| 24 |
_, err = f.client.Do(req, &r) |
| 25 |
if err != nil { |
| 26 |
return nil, err |
| 27 |
} |
| 28 |
|
| 29 |
return r.Friends, nil |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func (f *FriendsService) WaitingRequests(ctx context.Context) ([]Friend, error) { |
| 34 |
req, err := f.client.NewRequest(ctx, "GET", "/v2/friends/waiting-requests", nil) |
| 35 |
if err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
|
| 39 |
var r struct { |
| 40 |
Friends []Friend |
| 41 |
} |
| 42 |
_, err = f.client.Do(req, &r) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
return r.Friends, nil |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func (f *FriendsService) Request(ctx context.Context, username string) error { |
| 52 |
if username == "" { |
| 53 |
return fmt.Errorf("empty username") |
| 54 |
} |
| 55 |
req, err := f.client.NewRequest(ctx, "POST", "/v2/friends/"+username+"/request", nil) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 60 |
|
| 61 |
_, err = f.client.Do(req, &struct{}{}) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
return nil |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (f *FriendsService) Approve(ctx context.Context, username string) error { |
| 71 |
if username == "" { |
| 72 |
return fmt.Errorf("empty username") |
| 73 |
} |
| 74 |
|
| 75 |
req, err := f.client.NewRequest(ctx, "POST", "/v2/friends/"+username+"/approve", nil) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 80 |
|
| 81 |
_, err = f.client.Do(req, &struct{}{}) |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (f *FriendsService) Deny(ctx context.Context, username string) error { |
| 90 |
if username == "" { |
| 91 |
return fmt.Errorf("empty username") |
| 92 |
} |
| 93 |
|
| 94 |
req, err := f.client.NewRequest(ctx, "POST", "/v2/friends/"+username+"/deny", nil) |
| 95 |
if err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 99 |
|
| 100 |
_, err = f.client.Do(req, &struct{}{}) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
return nil |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (f *FriendsService) Unfriend(ctx context.Context, username string) error { |
| 109 |
if username == "" { |
| 110 |
return fmt.Errorf("empty username") |
| 111 |
} |
| 112 |
|
| 113 |
req, err := f.client.NewRequest(ctx, "POST", "/v2/friends/"+username+"/unfriend", nil) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 118 |
|
| 119 |
_, err = f.client.Do(req, &struct{}{}) |
| 120 |
if err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |