-
Notifications
You must be signed in to change notification settings - Fork 4
/
friends.go
175 lines (152 loc) · 4.56 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fitbit
import (
"encoding/json"
"fmt"
"strconv"
)
// FriendsList contins a list of friends
type FriendsList struct {
Data []Friend `json:"data"`
}
// Friend describes a friend
type Friend struct {
Attributes struct {
Friend bool `json:"friend"`
Child bool `json:"child"`
Name string `json:"name"`
Avatar string `json:"avatar"`
} `json:"attributes"`
ID string `json:"id"`
Type string `json:"type"`
}
// GetFriends returns the current friends of the current user
func (m *Session) GetFriends() (FriendsList, error) {
contents, err := m.makeRequest("https://api.fitbit.com/1.1/user/-/friends.json")
if err != nil {
return FriendsList{}, err
}
friends := FriendsList{}
if err := json.Unmarshal(contents, &friends); err != nil {
return FriendsList{}, err
}
return friends, nil
}
// FriendsLeaderboard contains the leaderboard of a user
type FriendsLeaderboard struct {
Data []struct {
Attributes struct {
StepSummary int `json:"step-summary"`
StepRank int `json:"step-rank"`
} `json:"attributes"`
Relationships struct {
User struct {
Data struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
} `json:"user"`
} `json:"relationships"`
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
Included []struct {
Attributes struct {
Friend bool `json:"friend,omitempty"`
Child bool `json:"child"`
Name string `json:"name"`
Avatar string `json:"avatar"`
} `json:"attributes,omitempty"`
ID string `json:"id"`
Type string `json:"type"`
} `json:"included"`
}
// GetFriendsLeaderboard returns the leaderbord including the user
func (m *Session) GetFriendsLeaderboard() (FriendsLeaderboard, error) {
contents, err := m.makeRequest("https://api.fitbit.com/1.1/user/-/leaderboard/friends.json")
if err != nil {
return FriendsLeaderboard{}, err
}
friends := FriendsLeaderboard{}
if err := json.Unmarshal(contents, &friends); err != nil {
return FriendsLeaderboard{}, err
}
return friends, nil
}
// FriendInviteByEmail invites another user to be friends by email
// FIXME: untested function, unknown response from server
func (m *Session) FriendInviteByEmail(value string) ([]byte, error) {
data := map[string]string{
"invitedUserEmail": value,
}
contents, err := m.makePOSTRequest("https://api.fitbit.com/1.1/user/-/friends/invitations", data)
if err != nil {
return nil, err
}
return contents, nil
}
// FriendInviteByUserID invites another user to be friends by user id
// FIXME: untested function, unknown response from server
func (m *Session) FriendInviteByUserID(value string) ([]byte, error) {
data := map[string]string{
"invitedUserId": value,
}
contents, err := m.makePOSTRequest("https://api.fitbit.com/1.1/user/-/friends/invitations", data)
if err != nil {
return nil, err
}
return contents, nil
}
// FriendsInvitations contains a list of open invitations to the user
type FriendsInvitations struct {
Data []struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes struct {
DateTime string `json:"dateTime"`
Email string `json:"email,omitempty"`
Source string `json:"source"`
} `json:"attributes,omitempty"`
Relationships struct {
User struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"data"`
} `json:"user"`
} `json:"relationships,omitempty"`
} `json:"data,omitempty"`
Included []struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes struct {
Friend bool `json:"friend"`
Child bool `json:"child"`
Avatar string `json:"avatar"`
Name string `json:"name"`
} `json:"attributes"`
} `json:"included,omitempty"`
}
// GetFriendInvitations returns a list of open friend invitations
func (m *Session) GetFriendInvitations() (FriendsInvitations, error) {
contents, err := m.makeRequest("https://api.fitbit.com/1.1/user/-/friends/invitations.json")
if err != nil {
return FriendsInvitations{}, err
}
friends := FriendsInvitations{}
if err := json.Unmarshal(contents, &friends); err != nil {
return FriendsInvitations{}, err
}
return friends, nil
}
// FriendsRespondToInvition reacts to a inviation
// FIXME: untested function, unknown response from server
func (m *Session) FriendsRespondToInvition(userID string, accept bool) ([]byte, error) {
data := map[string]string{
"accept": strconv.FormatBool(accept),
}
contents, err := m.makePOSTRequest(fmt.Sprintf("https://api.fitbit.com/1.1/user/-/friends/invitations/%s", userID), data)
if err != nil {
return nil, err
}
return contents, nil
}