-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathusers.go
258 lines (218 loc) · 5.97 KB
/
users.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package splitwise
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strconv"
)
// Users resources to access and modify user information.
type Users interface {
// CurrentUser returns information about the current user
CurrentUser(ctx context.Context) (*CurrentUser, error)
// UserByID returns a user information by their id
UserByID(ctx context.Context, id uint64) (*User, error)
// UpdateUser updates a user's information by their ID and returns the result
UpdateUser(ctx context.Context, id uint64, fields ...UserUpdatableField) (*CurrentUser, error)
}
type currentUserResponse struct {
User CurrentUser `json:"user"`
}
type CurrentUser struct {
ID uint64 `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Picture struct {
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
} `json:"picture"`
CustomPicture bool `json:"custom_picture"`
Email string `json:"email"`
RegistrationStatus string `json:"registration_status"`
ForceRefreshAt interface{} `json:"force_refresh_at"` // TODO: Data type is not known.
Locale string `json:"locale"`
CountryCode string `json:"country_code"`
DateFormat string `json:"date_format"`
DefaultCurrency string `json:"default_currency"`
DefaultGroupID int64 `json:"default_group_id"`
NotificationsRead string `json:"notifications_read"` // TODO: Convert it to time.Date type
NotificationsCount uint `json:"notifications_count"`
Notifications struct {
AddedAsFriend bool `json:"added_as_friend"`
AddedToGroup bool `json:"added_to_group"`
ExpenseAdded bool `json:"expense_added"`
ExpenseUpdated bool `json:"expense_updated"`
Bills bool `json:"bills"`
Payments bool `json:"payments"`
MonthlySummary bool `json:"monthly_summary"`
Announcements bool `json:"announcements"`
} `json:"notifications"`
}
// CurrentUser returns information about the current user
func (c client) CurrentUser(ctx context.Context) (*CurrentUser, error) {
url := c.baseURL + "/api/v3.0/get_current_user"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
token, err := c.AuthProvider.Auth()
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
err = c.checkError(res)
if err != nil {
return nil, err
}
var response currentUserResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response.User, nil
}
type userResponse struct {
User User `json:"user"`
}
type User struct {
ID uint64 `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Picture struct {
Small string `json:"small"`
Medium string `json:"medium"`
Large string `json:"large"`
} `json:"picture"`
CustomPicture bool `json:"custom_picture"`
Email string `json:"email"`
RegistrationStatus string `json:"registration_status"`
}
// UserByID returns a user information by their id.
func (c client) UserByID(ctx context.Context, id uint64) (*User, error) {
url := c.baseURL + "/api/v3.0/get_user/" + strconv.FormatUint(id, 10)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
token, err := c.AuthProvider.Auth()
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
err = c.checkError(res)
if err != nil {
return nil, err
}
var response userResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response.User, nil
}
type updateUserResponse struct {
User CurrentUser `json:"user"`
}
func (c client) UpdateUser(ctx context.Context, id uint64, fields ...UserUpdatableField) (*CurrentUser, error) {
url := c.baseURL + "/api/v3.0/update_user/" + strconv.FormatUint(id, 10)
body := map[string]interface{}{}
for _, field := range fields {
body[field.Key()] = field.Value()
}
rawBody, err := json.Marshal(&body)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(rawBody))
if err != nil {
return nil, err
}
token, err := c.AuthProvider.Auth()
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
err = c.checkError(res)
if err != nil {
return nil, err
}
var response updateUserResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response.User, nil
}
type UserUpdatableField interface {
Key() string
Value() interface{}
}
type userUpdatableField struct {
key string
value interface{}
}
func (u userUpdatableField) Key() string {
return u.key
}
func (u userUpdatableField) Value() interface{} {
return u.value
}
func UserLastNameField(value string) UserUpdatableField {
return &userUpdatableField{
key: "last_name",
value: value,
}
}
func UserFirstNameField(value string) UserUpdatableField {
return &userUpdatableField{
key: "first_name",
value: value,
}
}
func UserEmailField(value string) UserUpdatableField {
return &userUpdatableField{
key: "email",
value: value,
}
}
func UserPasswordField(value string) UserUpdatableField {
return &userUpdatableField{
key: "password",
value: value,
}
}
func UserLocaleField(value string) UserUpdatableField {
return &userUpdatableField{
key: "locale",
value: value,
}
}
func UserDefaultCurrencyField(value string) UserUpdatableField {
return &userUpdatableField{
key: "default_currency",
value: value,
}
}