fix(client): remove token from client args

This commit is contained in:
hexxa 2022-08-10 13:12:32 +08:00 committed by Hexxa
parent 6cc99312bf
commit 0cce1bd090
10 changed files with 294 additions and 256 deletions

View file

@ -12,8 +12,9 @@ import (
)
type UsersClient struct {
addr string
r *gorequest.SuperAgent
addr string
token *http.Cookie
r *gorequest.SuperAgent
}
func NewUsersClient(addr string) *UsersClient {
@ -28,55 +29,72 @@ func (cl *UsersClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath)
}
func (cl *UsersClient) SetToken(token *http.Cookie) {
cl.token = token
}
func (cl *UsersClient) Token() *http.Cookie {
return cl.token
}
func (cl *UsersClient) Login(user, pwd string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/login")).
resp, body, errs := cl.r.Post(cl.url("/v1/users/login")).
Send(multiusers.LoginReq{
User: user,
Pwd: pwd,
}).
End()
if len(errs) == 0 && resp.StatusCode == 200 {
// it may overwrite the token
httpResp := (*http.Response)(resp)
cl.token = GetCookie(httpResp.Cookies(), handlers.TokenCookie)
} else {
cl.token = &http.Cookie{}
}
return resp, body, errs
}
func (cl *UsersClient) Logout(token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) Logout() (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/logout")).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) SetPwd(oldPwd, newPwd string) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd")).
Send(multiusers.SetPwdReq{
OldPwd: oldPwd,
NewPwd: newPwd,
}).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) ForceSetPwd(userID, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) ForceSetPwd(userID, newPwd string) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd/force-set")).
Send(multiusers.ForceSetPwdReq{
ID: userID,
NewPwd: newPwd,
}).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) SetUser(ID uint64, role string, quota *db.Quota, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) SetUser(ID uint64, role string, quota *db.Quota) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/")).
Send(multiusers.SetUserReq{
ID: ID,
Role: role,
Quota: quota,
}).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) AddUser(name, pwd, role string, token *http.Cookie) (*http.Response, *multiusers.AddUserResp, []error) {
func (cl *UsersClient) AddUser(name, pwd, role string) (*http.Response, *multiusers.AddUserResp, []error) {
resp, body, errs := cl.r.Post(cl.url("/v1/users/")).
AddCookie(token).
AddCookie(cl.token).
Send(multiusers.AddUserReq{
Name: name,
Pwd: pwd,
@ -97,16 +115,16 @@ func (cl *UsersClient) AddUser(name, pwd, role string, token *http.Cookie) (*htt
return resp, auResp, errs
}
func (cl *UsersClient) DelUser(id string, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) DelUser(id string) (*http.Response, string, []error) {
return cl.r.Delete(cl.url("/v1/users/")).
AddCookie(token).
AddCookie(cl.token).
Param(handlers.UserIDParam, id).
End()
}
func (cl *UsersClient) ListUsers(token *http.Cookie) (*http.Response, *multiusers.ListUsersResp, []error) {
func (cl *UsersClient) ListUsers() (*http.Response, *multiusers.ListUsersResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/users/list")).
AddCookie(token).
AddCookie(cl.token).
End()
if len(errs) > 0 {
return nil, nil, errs
@ -121,27 +139,27 @@ func (cl *UsersClient) ListUsers(token *http.Cookie) (*http.Response, *multiuser
return resp, lsResp, errs
}
func (cl *UsersClient) AddRole(role string, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) AddRole(role string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/roles/")).
AddCookie(token).
AddCookie(cl.token).
Send(multiusers.AddRoleReq{
Role: role,
}).
End()
}
func (cl *UsersClient) DelRole(role string, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) DelRole(role string) (*http.Response, string, []error) {
return cl.r.Delete(cl.url("/v1/roles/")).
AddCookie(token).
AddCookie(cl.token).
Send(multiusers.DelRoleReq{
Role: role,
}).
End()
}
func (cl *UsersClient) ListRoles(token *http.Cookie) (*http.Response, *multiusers.ListRolesResp, []error) {
func (cl *UsersClient) ListRoles() (*http.Response, *multiusers.ListRolesResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/roles/list")).
AddCookie(token).
AddCookie(cl.token).
End()
if len(errs) > 0 {
return nil, nil, errs
@ -156,9 +174,9 @@ func (cl *UsersClient) ListRoles(token *http.Cookie) (*http.Response, *multiuser
return resp, lsResp, errs
}
func (cl *UsersClient) Self(token *http.Cookie) (*http.Response, *multiusers.SelfResp, []error) {
func (cl *UsersClient) Self() (*http.Response, *multiusers.SelfResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/users/self")).
AddCookie(token).
AddCookie(cl.token).
End()
if len(errs) > 0 {
return nil, nil, errs
@ -173,26 +191,26 @@ func (cl *UsersClient) Self(token *http.Cookie) (*http.Response, *multiusers.Sel
return resp, selfResp, errs
}
func (cl *UsersClient) SetPreferences(prefers *db.Preferences, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) SetPreferences(prefers *db.Preferences) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/preferences")).
Send(multiusers.SetPreferencesReq{
Preferences: prefers,
}).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) IsAuthed(token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) IsAuthed() (*http.Response, string, []error) {
return cl.r.Get(cl.url("/v1/users/isauthed")).
AddCookie(token).
AddCookie(cl.token).
End()
}
func (cl *UsersClient) ResetUsedSpace(userID uint64, token *http.Cookie) (*http.Response, string, []error) {
func (cl *UsersClient) ResetUsedSpace(userID uint64) (*http.Response, string, []error) {
return cl.r.Put(cl.url("/v1/users/used-space")).
Send(multiusers.ResetUsedSpaceReq{
UserID: userID,
}).
AddCookie(token).
AddCookie(cl.token).
End()
}