-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage.go
177 lines (150 loc) · 4.8 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
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
package spvchannels
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"path"
)
func (c *Client) getMessageBaseEndpoint() string {
u := url.URL{
Scheme: c.cfg.httpScheme(),
Host: c.cfg.baseURL,
Path: path.Join(c.cfg.path, "/api", c.cfg.version),
}
return u.String()
}
// MessageHeadRequest hold data for HEAD message request
// It request the max sequence for a particular channel
type MessageHeadRequest struct {
ChannelID string `json:"channelid"`
}
// MessageWriteRequest hold data for write message request
type MessageWriteRequest struct {
ChannelID string `json:"channelid"`
Message string `json:"message"`
}
// MessageWriteReply hold data for write message reply
// It contains the id of the message in the database,
// the received timestamp, the content type, and the
// base64 encoding of the message content
type MessageWriteReply struct {
Sequence int64 `json:"sequence"`
Received string `json:"received"`
ContentType string `json:"content_type"`
Payload string `json:"payload"`
}
// MessagesRequest hold data for get messages request
type MessagesRequest struct {
ChannelID string `json:"channelid"`
UnRead bool `json:"unread"`
}
// MessagesReply hold data for get messages reply
type MessagesReply []MessageWriteReply
// MessageMarkRequest hold data for mark message request
//
// A particular message is identified by its sequence number
// and the channel id in which it belong to
type MessageMarkRequest struct {
ChannelID string `json:"channelid"`
Sequence int64 `json:"sequence"`
Older bool `json:"older"`
Read bool `json:"read"`
}
// MessageDeleteRequest hold data for delete message request
// A particular message is identified by its sequence number
// and the channel id in which it belong to
type MessageDeleteRequest struct {
ChannelID string `json:"channelid"`
Sequence int64 `json:"sequence"`
}
// MessageHead send HEAD message request. It request the max sequence for a particular channel
//
// The request should use bearer token authentification method.
// The token is provided by the TokenCreate endpoint
func (c *Client) MessageHead(ctx context.Context, r MessageHeadRequest) error {
req, err := http.NewRequestWithContext(
ctx,
http.MethodHead, fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID),
nil,
)
if err != nil {
return err
}
return c.sendRequest(req, nil)
}
// MessageWrite write a message to a particular channel
//
// The request should use bearer token authentification method.
// The token is provided by the TokenCreate endpoint
func (c *Client) MessageWrite(ctx context.Context, r MessageWriteRequest) (*MessageWriteReply, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID), bytes.NewBuffer([]byte(r.Message)),
)
if err != nil {
return nil, err
}
res := MessageWriteReply{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// Messages get messages list. It can query read/unread messages.
//
// The request should use bearer token authentification method.
// The token is provided by the TokenCreate endpoint
func (c *Client) Messages(ctx context.Context, r MessagesRequest) (MessagesReply, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID),
nil,
)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("unread", fmt.Sprintf("%t", r.UnRead))
req.URL.RawQuery = q.Encode()
res := MessagesReply{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return res, nil
}
// MessageMark mark a message
//
// The request should use bearer token authentification method.
// The token is provided by the TokenCreate endpoint
func (c *Client) MessageMark(ctx context.Context, r MessageMarkRequest) error {
payloadStr := fmt.Sprintf("{\"read\":%t}", r.Read)
channelURL := fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID)
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/%v", channelURL, r.Sequence), bytes.NewBuffer([]byte(payloadStr)),
)
if err != nil {
return err
}
q := req.URL.Query()
q.Add("older", fmt.Sprintf("%t", r.Older))
req.URL.RawQuery = q.Encode()
return c.sendRequest(req, nil)
}
// MessageDelete delete a message
//
// The request should use bearer token authentification method.
// The token is provided by the TokenCreate endpoint
func (c *Client) MessageDelete(ctx context.Context, r MessageDeleteRequest) error {
channelURL := fmt.Sprintf("%s/channel/%s", c.getMessageBaseEndpoint(), r.ChannelID)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/%v", channelURL, r.Sequence), nil)
if err != nil {
return err
}
return c.sendRequest(req, nil)
}