forked from igungor/go-putio
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfriends.go
124 lines (104 loc) · 2.87 KB
/
friends.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package putio
import (
"context"
"net/http"
)
// FriendsService is the service to operate on user friends.
type FriendsService struct {
client *Client
}
// List lists users friends.
func (f *FriendsService) List(ctx context.Context) ([]Friend, error) {
req, err := f.client.NewRequest(ctx, http.MethodGet, "/v2/friends/list", nil)
if err != nil {
return nil, err
}
var r struct {
Friends []Friend
Total int
}
_, err = f.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return nil, err
}
return r.Friends, nil
}
// WaitingRequests lists user's pending friend requests.
func (f *FriendsService) WaitingRequests(ctx context.Context) ([]Friend, error) {
req, err := f.client.NewRequest(ctx, http.MethodGet, "/v2/friends/waiting-requests", nil)
if err != nil {
return nil, err
}
var r struct {
Friends []Friend
}
_, err = f.client.Do(req, &r) // nolint:bodyclose
if err != nil {
return nil, err
}
return r.Friends, nil
}
// Request sends a friend request to the given username.
func (f *FriendsService) Request(ctx context.Context, username string) error {
if username == "" {
return ErrEmptyUserName
}
req, err := f.client.NewRequest(ctx, http.MethodPost, "/v2/friends/"+username+"/request", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = f.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}
// Approve approves a friend request from the given username.
func (f *FriendsService) Approve(ctx context.Context, username string) error {
if username == "" {
return ErrEmptyUserName
}
req, err := f.client.NewRequest(ctx, http.MethodPost, "/v2/friends/"+username+"/approve", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = f.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}
// Deny denies a friend request from the given username.
func (f *FriendsService) Deny(ctx context.Context, username string) error {
if username == "" {
return ErrEmptyUserName
}
req, err := f.client.NewRequest(ctx, http.MethodPost, "/v2/friends/"+username+"/deny", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = f.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}
// Unfriend removed friend from user's friend list.
func (f *FriendsService) Unfriend(ctx context.Context, username string) error {
if username == "" {
return ErrEmptyUserName
}
req, err := f.client.NewRequest(ctx, http.MethodPost, "/v2/friends/"+username+"/unfriend", nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = f.client.Do(req, &struct{}{}) // nolint:bodyclose
if err != nil {
return err
}
return nil
}