-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.go
77 lines (69 loc) · 2.2 KB
/
comment.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
package rpc
import (
"context"
"time"
"github.com/Alexdzk/dousheng/kitex_gen/comment"
"github.com/Alexdzk/dousheng/kitex_gen/comment/commentservice"
"github.com/Alexdzk/dousheng/pkg/constants"
"github.com/Alexdzk/dousheng/pkg/errno"
"github.com/Alexdzk/dousheng/pkg/middleware"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
trace "github.com/kitex-contrib/tracer-opentracing"
)
var commentClient commentservice.Client
func initCommentRpc() {
r, err := etcd.NewEtcdResolver([]string{constants.EtcdAddress})
if err != nil {
panic(err)
}
c, err := commentservice.NewClient(
constants.CommentServiceName,
client.WithMiddleware(middleware.CommonMiddleware),
client.WithInstanceMW(middleware.ClientMiddleware),
client.WithMuxConnection(1),
client.WithRPCTimeout(3*time.Second),
client.WithConnectTimeout(50*time.Millisecond),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithSuite(trace.NewDefaultClientSuite()),
client.WithResolver(r),
)
if err != nil {
panic(err)
}
commentClient = c
}
// CreateComment add comment
func CreateComment(ctx context.Context, req *comment.CreateCommentRequest) (*comment.Comment, error) {
resp, err := commentClient.CreateComment(ctx, req)
if err != nil {
return nil, err
}
if resp.BaseResp.StatusCode != 0 {
return nil, errno.NewErrNo(resp.BaseResp.StatusCode, resp.BaseResp.StatusMessage)
}
return resp.Comment, nil
}
// DeleteComment delete comment
func DeleteComment(ctx context.Context, req *comment.DeleteCommentRequest) (*comment.Comment, error) {
resp, err := commentClient.DeleteComment(ctx, req)
if err != nil {
return nil, err
}
if resp.BaseResp.StatusCode != 0 {
return nil, errno.NewErrNo(resp.BaseResp.StatusCode, resp.BaseResp.StatusMessage)
}
return resp.Comment, nil
}
// CommentList get comment list info
func CommentList(ctx context.Context, req *comment.CommentListRequest) ([]*comment.Comment, error) {
resp, err := commentClient.CommentList(ctx, req)
if err != nil {
return nil, err
}
if resp.BaseResp.StatusCode != 0 {
return nil, errno.NewErrNo(resp.BaseResp.StatusCode, resp.BaseResp.StatusMessage)
}
return resp.CommentList, nil
}