-
Notifications
You must be signed in to change notification settings - Fork 8
/
users.go
102 lines (84 loc) · 2.23 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
package scalingo
import (
"context"
"encoding/json"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type UsersService interface {
Self(context.Context) (*User, error)
UpdateUser(context.Context, UpdateUserParams) (*User, error)
UserStopFreeTrial(context.Context) error
}
var _ UsersService = (*Client)(nil)
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Fullname string `json:"fullname"`
Email string `json:"email"`
Flags map[string]bool `json:"flags"`
}
type SelfResponse struct {
User *User `json:"user"`
}
func (c *Client) Self(ctx context.Context) (*User, error) {
req := &http.APIRequest{
Endpoint: "/users/self",
}
res, err := c.AuthAPI().Do(ctx, req)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()
var u SelfResponse
err = json.NewDecoder(res.Body).Decode(&u)
if err != nil {
return nil, errgo.Mask(err)
}
return u.User, nil
}
type UpdateUserParams struct {
Password string `json:"password,omitempty"`
Email string `json:"email,omitempty"`
}
type UpdateUserResponse struct {
User *User `json:"user"`
}
func (c *Client) UpdateUser(ctx context.Context, params UpdateUserParams) (*User, error) {
if params.Password == "" && params.Email == "" {
return nil, nil
}
req := &http.APIRequest{
Method: "PATCH",
Endpoint: "/account/profile",
Params: map[string]interface{}{
"user": params,
},
Expected: http.Statuses{200},
}
res, err := c.AuthAPI().Do(ctx, req)
if err != nil {
return nil, errgo.Notef(err, "fail to execute the query to update the user")
}
defer res.Body.Close()
var u UpdateUserResponse
err = json.NewDecoder(res.Body).Decode(&u)
if err != nil {
return nil, errgo.Notef(err, "fail to decode response of the query to update the user")
}
return u.User, nil
}
func (c *Client) UserStopFreeTrial(ctx context.Context) error {
req := &http.APIRequest{
Method: "POST",
Endpoint: "/users/stop_free_trial",
Params: map[string]interface{}{},
Expected: http.Statuses{200},
}
res, err := c.AuthAPI().Do(ctx, req)
if err != nil {
return errgo.Notef(err, "fail to execute the query to stop user free trial")
}
defer res.Body.Close()
return nil
}