-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjms_chat.go
More file actions
82 lines (78 loc) · 2.1 KB
/
Copy pathjms_chat.go
File metadata and controls
82 lines (78 loc) · 2.1 KB
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
package impl
import (
"bytes"
"context"
"encoding/json"
"github.com/jumpserver/wisp/pkg/logger"
pb "github.com/jumpserver/wisp/protobuf-go/protobuf"
"google.golang.org/protobuf/types/known/structpb"
)
func (j *JMServer) GetAccountChat(ctx context.Context, req *pb.Empty) (*pb.AccountDetailResponse, error) {
var (
status pb.Status
)
accountChatDetail, err := j.apiClient.GetAccountsChat()
if err != nil {
logger.Errorf("GetAccountChat: %v", err)
status.Err = err.Error()
return &pb.AccountDetailResponse{
Status: &status,
}, nil
}
var m map[string]any
jsonBytes, err := json.Marshal(accountChatDetail)
if err != nil {
logger.Errorf("Encode accountChatDetail failed: %v", err)
status.Err = err.Error()
return &pb.AccountDetailResponse{
Status: &status,
}, nil
}
if err = json.Unmarshal(jsonBytes, &m); err != nil {
logger.Errorf("Unmarshal to map failed: %v", err)
status.Err = err.Error()
return &pb.AccountDetailResponse{
Status: &status,
}, nil
}
st, err1 := structpb.NewStruct(m)
if err1 != nil {
status.Err = err1.Error()
return &pb.AccountDetailResponse{
Status: &status,
}, nil
}
status.Ok = true
return &pb.AccountDetailResponse{Status: &status, Payload: st}, nil
}
func (j *JMServer) CallAPI(ctx context.Context, req *pb.HTTPRequest) (*pb.HTTPResponse, error) {
method := req.GetMethod()
reqUrl := req.GetPath()
query := req.GetQuery()
body := req.GetBody()
header := req.GetHeader()
apiClient := j.apiClient.Copy()
var (
status pb.Status
)
for k, v := range header {
apiClient.SetHeader(k, v)
}
var bodyObj any
if len(body) > 0 {
if err := json.NewDecoder(bytes.NewBuffer(body)).Decode(&bodyObj); err != nil {
logger.Errorf("Decode body failed: %v", err)
status.Err = err.Error()
return &pb.HTTPResponse{Status: &status}, err
}
}
var resJson bytes.Buffer
_, err := apiClient.Call(method, reqUrl, bodyObj, &resJson, query)
if err != nil {
logger.Errorf("Call: %v", err)
status.Err = err.Error()
return &pb.HTTPResponse{Status: &status}, err
}
status.Ok = true
return &pb.HTTPResponse{Status: &status, Body: resJson.Bytes()}, nil
}