-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
114 lines (99 loc) · 2.83 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
110
111
112
113
114
package zincapi
import (
"fmt"
"os"
"github.com/guonaihong/gout"
jsoniter "github.com/json-iterator/go"
)
type Role string
const (
RoleAdmin Role = "admin"
RoleUser Role = "user"
)
type User struct {
UserID string `json:"_id"`
Username string `json:"username"`
Role Role `json:"role"`
Password string `json:"password"`
}
type Hits struct {
Total Total `json:"total"`
MaxScore float64 `json:"max_score"`
Hits []Hit `json:"hits"`
}
type Hit struct {
Index string `json:"_index"`
Type string `json:"_type"`
ID string `json:"_id"`
Score float64 `json:"_score"`
Timestamp string `json:"@timestamp"`
Source Source `json:"_source"`
}
type Source struct {
ID string `json:"_id"`
Name string `json:"name"`
Role string `json:"role"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type Total struct {
Value int64 `json:"value"`
}
type Shards struct {
Total int64 `json:"total"`
Successful int64 `json:"successful"`
Skipped int64 `json:"skipped"`
Failed int64 `json:"failed"`
}
type UserResponse struct {
Message Message `json:"message"`
Took int64 `json:"took"`
TimedOut bool `json:"timed_out"`
Shards Shards `json:"_shards"`
Hits Hits `json:"hits"`
Error string `json:"error"`
}
type Message struct {
ID string `json:"_id"`
Name string `json:"name"`
Role string `json:"role"`
Salt string `json:"salt"`
Password string `json:"password"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// CreateOrUpdate 创建或更新用户信息
// https://docs.zincsearch.com/api/user/create/
func (u *User) CreateOrUpdate(p *User) (*UserResponse, error) {
b, err := jsoniter.Marshal(p)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc create or update user: %v\n", err)
return nil, err
}
var resp = new(UserResponse)
if err := gout.PUT(getURI("/api/user")).SetHeader(getHeader()).SetBody(b).BindJSON(resp).Do(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc create or update user: %v\n", err)
return nil, err
}
return resp, nil
}
// Delete 删除用户
// https://docs.zincsearch.com/api/user/delete/
func (u *User) Delete(uid string) (*UserResponse, error) {
var resp = new(UserResponse)
if err := gout.DELETE(getURI("/api/user/" + uid)).SetHeader(getHeader()).BindJSON(resp).Do(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc create or update user: %v\n", err)
return nil, err
}
return resp, nil
}
// List 获取用户列表
// https://docs.zincsearch.com/api/user/list/
func (u *User) List() (*UserResponse, error) {
var resp = new(UserResponse)
if err := gout.GET(getURI("/api/user")).SetHeader(getHeader()).BindJSON(resp).Do(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "zinc get user list: %v\n", err)
return nil, err
}
return resp, nil
}