-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
109 lines (92 loc) · 2.79 KB
/
user.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
package pixela
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
type user struct {
UserName string
Token string
}
func (u *user) Create(agreeTermsOfService, notMinor bool, thanksCode string) (*Result, error) {
param, err := u.createCreateRequestParameter(agreeTermsOfService, notMinor, thanksCode)
if err != nil {
return &Result{}, errors.Wrapf(err, "failed to create user create parameter")
}
return doRequestAndParseResponse(param)
}
func (u *user) createCreateRequestParameter(agreeTermsOfService, notMinor bool, thanksCode string) (*requestParameter, error) {
create := &userCreate{
Token: u.Token,
UserName: u.UserName,
AgreeTermsOfService: boolToString(agreeTermsOfService),
NotMinor: boolToString(notMinor),
ThanksCode: thanksCode,
}
b, err := json.Marshal(create)
if err != nil {
return &requestParameter{}, errors.Wrap(err, "failed to marshal json")
}
return &requestParameter{
Method: http.MethodPost,
URL: APIBaseURL + "/users",
Header: map[string]string{},
Body: b,
}, nil
}
type userCreate struct {
Token string `json:"token"`
UserName string `json:"username"`
AgreeTermsOfService string `json:"AgreeTermsOfService"`
NotMinor string `json:"NotMinor"`
ThanksCode string `json:"thanksCode,omitempty"`
}
func boolToString(b bool) string {
if b {
return "yes"
}
return "no"
}
func (u *user) Update(newToken, thanksCode string) (*Result, error) {
param, err := u.createUpdateRequestParameter(newToken, thanksCode)
if err != nil {
return &Result{}, errors.Wrapf(err, "failed to create user update parameter")
}
return doRequestAndParseResponse(param)
}
func (u *user) createUpdateRequestParameter(newToken, thanksCode string) (*requestParameter, error) {
update := userUpdate{
NewToken: newToken,
ThanksCode: thanksCode,
}
b, err := json.Marshal(update)
if err != nil {
return &requestParameter{}, errors.Wrap(err, "failed to marshal json")
}
return &requestParameter{
Method: http.MethodPut,
URL: fmt.Sprintf(APIBaseURL+"/users/%s", u.UserName),
Header: map[string]string{userToken: u.Token},
Body: b,
}, nil
}
type userUpdate struct {
NewToken string `json:"newToken"`
ThanksCode string `json:"thanksCode,omitempty"`
}
func (u *user) Delete() (*Result, error) {
param, err := u.createDeleteRequestParameter()
if err != nil {
return &Result{}, errors.Wrapf(err, "failed to create user delete parameter")
}
return doRequestAndParseResponse(param)
}
func (u *user) createDeleteRequestParameter() (*requestParameter, error) {
return &requestParameter{
Method: http.MethodDelete,
URL: fmt.Sprintf(APIBaseURL+"/users/%s", u.UserName),
Header: map[string]string{userToken: u.Token},
Body: []byte{},
}, nil
}