-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
149 lines (127 loc) · 4.43 KB
/
message.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
package service
import (
"fmt"
"time"
"github.com/go-resty/resty/v2"
"github.com/joeyscat/qim/wire/rpcc"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
type Message interface {
InsertUser(app string, req *rpcc.InsertMessageReq) (*rpcc.InsertMessageResp, error)
InsertGroup(app string, req *rpcc.InsertMessageReq) (*rpcc.InsertMessageResp, error)
SetAck(app string, req *rpcc.AckMessageReq) error
GetMessageIndex(app string, req *rpcc.GetOfflineMessageIndexReq) (*rpcc.GetOfflineMessageIndexResp, error)
GetMessageContent(app string, req *rpcc.GetOfflineMessageContentReq) (*rpcc.GetOfflineMessageContentResp, error)
}
type MessageHttp struct {
url string
cli *resty.Client
srv *resty.SRVRecord
lg *zap.Logger
}
func NewMessageService(url string, lg *zap.Logger) Message {
client := resty.New().SetRetryCount(3).SetTimeout(time.Second * 5)
client.SetHeader("Content-Type", "application/x-protobuf")
client.SetHeader("Accept", "application/x-protobuf")
return &MessageHttp{
url: url,
cli: client,
lg: lg,
}
}
func NewMessageServiceWithSRV(scheme string, srv *resty.SRVRecord, lg *zap.Logger) Message {
cli := resty.New().SetRetryCount(3).SetTimeout(time.Second * 5)
cli.SetHeader("Content-Type", "application/x-protobuf")
cli.SetHeader("Accept", "application/x-protobuf")
cli.SetScheme(scheme)
return &MessageHttp{
url: "",
cli: cli,
srv: srv,
lg: lg,
}
}
// GetMessageContent implements Message
func (m *MessageHttp) GetMessageContent(app string, req *rpcc.GetOfflineMessageContentReq) (*rpcc.GetOfflineMessageContentResp, error) {
path := fmt.Sprintf("%s/api/%s/offline/content", m.url, app)
body, _ := proto.Marshal(req)
response, err := m.Req().SetBody(body).Post(path)
if err != nil {
return nil, err
}
if response.StatusCode() != 200 {
return nil, fmt.Errorf("MessageHttp.GetMessageContent - http status code: %d", response.StatusCode())
}
var resp rpcc.GetOfflineMessageContentResp
_ = proto.Unmarshal(response.Body(), &resp)
return &resp, nil
}
// GetMessageIndex implements Message
func (m *MessageHttp) GetMessageIndex(app string, req *rpcc.GetOfflineMessageIndexReq) (*rpcc.GetOfflineMessageIndexResp, error) {
path := fmt.Sprintf("%s/api/%s/offline/index", m.url, app)
body, _ := proto.Marshal(req)
response, err := m.Req().SetBody(body).Post(path)
if err != nil {
return nil, err
}
if response.StatusCode() != 200 {
return nil, fmt.Errorf("MessageHttp.GetMessageIndex - http status code: %d", response.StatusCode())
}
var resp rpcc.GetOfflineMessageIndexResp
_ = proto.Unmarshal(response.Body(), &resp)
return &resp, nil
}
// InsertGroup implements Message
func (m *MessageHttp) InsertGroup(app string, req *rpcc.InsertMessageReq) (*rpcc.InsertMessageResp, error) {
path := fmt.Sprintf("%s/api/%s/message/group", m.url, app)
t1 := time.Now()
body, _ := proto.Marshal(req)
response, err := m.Req().SetBody(body).Post(path)
if err != nil {
return nil, err
}
if response.StatusCode() != 200 {
return nil, fmt.Errorf("MessageHttp.InsertGroup - http status code: %d", response.StatusCode())
}
var resp rpcc.InsertMessageResp
_ = proto.Unmarshal(response.Body(), &resp)
m.lg.Debug("MessageHttp.InsertGroup", zap.Duration("cost", time.Since(t1)), zap.String("resp", resp.String()))
return &resp, nil
}
// InsertUser implements Message
func (m *MessageHttp) InsertUser(app string, req *rpcc.InsertMessageReq) (*rpcc.InsertMessageResp, error) {
path := fmt.Sprintf("%s/api/%s/message/user", m.url, app)
t1 := time.Now()
body, _ := proto.Marshal(req)
response, err := m.Req().SetBody(body).Post(path)
if err != nil {
return nil, err
}
if response.StatusCode() != 200 {
return nil, fmt.Errorf("MessageHttp.InsertUser - http status code: %d", response.StatusCode())
}
var resp rpcc.InsertMessageResp
_ = proto.Unmarshal(response.Body(), &resp)
m.lg.Debug("MessageHttp.InsertUser", zap.Duration("cost", time.Since(t1)), zap.String("resp", resp.String()))
return &resp, nil
}
// SetAck implements Message
func (m *MessageHttp) SetAck(app string, req *rpcc.AckMessageReq) error {
path := fmt.Sprintf("%s/api/%s/message/ack", m.url, app)
body, _ := proto.Marshal(req)
response, err := m.Req().SetBody(body).Post(path)
if err != nil {
return err
}
if response.StatusCode() != 200 {
return fmt.Errorf("MessageHttp.SetAck - http status code: %d", response.StatusCode())
}
return nil
}
func (m MessageHttp) Req() *resty.Request {
if m.srv == nil {
return m.cli.R()
}
return m.cli.R().SetSRV(m.srv)
}