-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser_service.go
37 lines (32 loc) · 886 Bytes
/
user_service.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
package natureremo
import (
"context"
"fmt"
"net/url"
)
// UserService provides interface of Nature Remo APIs which are related to user.
type UserService interface {
// Me gets own user data.
Me(ctx context.Context) (*User, error)
// Update updates user data.
Update(ctx context.Context, u *User) (*User, error)
}
type userService struct {
cli *Client
}
func (s *userService) Me(ctx context.Context) (*User, error) {
var u User
if err := s.cli.get(ctx, "users/me", nil, &u); err != nil {
return nil, fmt.Errorf("GET users/me failed: %w", err)
}
return &u, nil
}
func (s *userService) Update(ctx context.Context, me *User) (*User, error) {
data := url.Values{}
data.Set("nickname", me.Nickname)
var u User
if err := s.cli.postForm(ctx, "users/me", data, &u); err != nil {
return nil, fmt.Errorf("POST users/me failed with %#v: %w", me, err)
}
return &u, nil
}