/
api.go
209 lines (179 loc) · 4.73 KB
/
api.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package welink
import (
"encoding/json"
"fmt"
"log"
"os"
"fhyx.online/welink-api-go/client"
)
const (
urlToken = "https://open.welink.huaweicloud.com/api/auth/v2/tickets"
urlUserGet = "https://open.welink.huaweicloud.com/api/contact/v1/users"
urlUserList = "https://open.welink.huaweicloud.com/api/contact/v1/user/users"
urlUserListSimp = "https://open.welink.huaweicloud.com/api/contact/v2/user/userid"
urlUserBulk = "https://open.welink.huaweicloud.com/api/contact/v1/users/bulk"
urlUserStatus = "https://open.welink.huaweicloud.com/api/contact/v1/users/status"
urlDeptList = "https://open.welink.huaweicloud.com/api/contact/v2/departments/list"
urlDeptSync = "https://open.welink.huaweicloud.com/api/contact/v2/departments/bulk"
urlDeptStatus = "https://open.welink.huaweicloud.com/api/contact/v2/departments/status"
)
type API struct {
corpID string
corpSecret string
c *client.Client
}
func NewAPI() *API {
return New(os.Getenv("WELINK_CORP_ID"), os.Getenv("WELINK_CORP_SECRET"))
}
// New ...
func New(corpID, corpSecret string) *API {
if corpID == "" || corpSecret == "" {
log.Printf("corpID or corpSecret are empty or not found")
}
c := client.NewClient(urlToken)
c.SetContentType("application/json")
c.SetCorp(corpID, corpSecret)
return &API{
corpID: corpID,
corpSecret: corpSecret,
c: c,
}
}
func (a *API) CorpID() string {
return a.corpID
}
func uriForUserGet(uid, at string) string {
switch at {
case "uid":
return fmt.Sprintf("%s?userId=%s", urlUserGet, uid)
case "mobile":
return fmt.Sprintf("%s?mobileNumber=%s", urlUserGet, uid)
default:
return fmt.Sprintf("%s?corpUserId=%s", urlUserGet, uid)
}
}
// GetUser get user with uid,mobile,cuid
func (a *API) GetUser(uid, at string) (*User, error) {
user := new(User)
err := a.c.GetJSON(uriForUserGet(uid, at), user)
if err != nil {
logger().Infow("get user fail", "at:"+at, uid, "err", err)
return nil, err
}
return user, nil
}
// ListUser ...
func (a *API) ListUser(deptID int) (data []User, err error) {
limit := 50
uri := fmt.Sprintf("%s?&deptCode=%d&pageSize=%d", urlUserList, deptID, limit)
var ret usersResponse
err = a.c.GetJSON(uri, &ret)
if err == nil {
data = ret.Users
}
return
}
func (a *API) ListDepartment(id int, recursive bool) (data Departments, err error) {
var recursiveflag int
if id > 0 && recursive {
recursiveflag = 1
}
uri := fmt.Sprintf("%s?deptCode=%d&recursiveflag=%d&limit=100", urlDeptList, id, recursiveflag)
var ret departmentResponse
err = a.c.GetJSON(uri, &ret)
if err == nil {
data = ret.Departments
}
if recursive && id == 0 {
for _, dept := range data {
var child Departments
child, err = a.ListDepartment(dept.ID, true)
if err != nil {
return
}
data = append(data, child...)
}
}
return
}
// SyncDepartment ...
func (a *API) SyncDepartment(data []DepartmentUp) (res []DeptRespItem, err error) {
var req deptBatchReq
req.Data = data
var buf []byte
buf, err = json.Marshal(&req)
if err != nil {
return
}
var resp deptBatchResp
err = a.c.PostJSON(urlDeptSync, buf, &resp)
if err != nil {
logger().Infow("sync department fail", "err", err)
return
}
res = resp.Data
logger().Infow("sync department ok", "resp", resp)
return
}
// StatusDepartment ...
func (a *API) StatusDepartment(data []DepartmentUp) (res []DeptRespItem, err error) {
var req deptStatusReq
for _, deptUp := range data {
req.Data = append(req.Data, deptStatusUp{deptUp.CorpDeptID})
}
var buf []byte
buf, err = json.Marshal(&req)
if err != nil {
return
}
var resp deptBatchResp
err = a.c.PostJSON(urlDeptStatus, buf, &resp)
if err != nil {
logger().Infow("status department fail", "err", err)
return
}
res = resp.Data
logger().Infow("status department ok", "resp", resp)
return
}
// SyncUser ...
func (a *API) SyncUser(data []UserUp) (res []UserRespItem, err error) {
var req userBatchReq
req.Data = data
var buf []byte
buf, err = json.Marshal(&req)
if err != nil {
return
}
var resp userBatchResp
err = a.c.PostJSON(urlUserBulk, buf, &resp)
if err != nil {
logger().Infow("sync User fail", "err", err)
return
}
res = resp.Data
logger().Infow("sync User ok", "resp", resp)
return
}
// StatusUser ...
func (a *API) StatusUser(data []UserUp) (res []UserRespItem, err error) {
var req userStatusReq
for _, userUp := range data {
req.Data = append(req.Data, userStatusUp{
CorpUID: userUp.CorpUID, Mobile: userUp.Mobile, Email: userUp.Email})
}
var buf []byte
buf, err = json.Marshal(&req)
if err != nil {
return
}
var resp userBatchResp
err = a.c.PostJSON(urlUserStatus, buf, &resp)
if err != nil {
logger().Infow("status User fail", "err", err)
return
}
res = resp.Data
logger().Infow("status User ok", "resp", resp)
return
}