forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_file_message.go
360 lines (290 loc) · 8.86 KB
/
publish_file_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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package pubnub
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"github.com/pubnub/go/pnerr"
"github.com/pubnub/go/utils"
"net/http"
"net/url"
"strconv"
)
var emptyPublishFileMessageResponse *PublishFileMessageResponse
const publishFileMessageGetPath = "/v1/files/publish-file/%s/%s/0/%s/%s/%s"
const publishFileMessagePostPath = "/v1/files/publish-file/%s/%s/0/%s/%s"
type publishFileMessageBuilder struct {
opts *publishFileMessageOpts
}
func newPublishFileMessageBuilder(pubnub *PubNub) *publishFileMessageBuilder {
builder := publishFileMessageBuilder{
opts: &publishFileMessageOpts{
pubnub: pubnub,
},
}
builder.opts.UsePost = false
return &builder
}
func newPublishFileMessageBuilderWithContext(pubnub *PubNub,
context Context) *publishFileMessageBuilder {
builder := publishFileMessageBuilder{
opts: &publishFileMessageOpts{
pubnub: pubnub,
ctx: context,
},
}
return &builder
}
// TTL sets the TTL (hours) for the Publish request.
func (b *publishFileMessageBuilder) TTL(ttl int) *publishFileMessageBuilder {
b.opts.TTL = ttl
b.opts.setTTL = true
return b
}
// Meta sets the Meta Payload for the Publish request.
func (b *publishFileMessageBuilder) Meta(meta interface{}) *publishFileMessageBuilder {
b.opts.Meta = meta
return b
}
// ShouldStore if true the messages are stored in History
func (b *publishFileMessageBuilder) ShouldStore(store bool) *publishFileMessageBuilder {
b.opts.ShouldStore = store
b.opts.setShouldStore = true
return b
}
// Channel sets the Channel for the PublishFileMessage request.
func (b *publishFileMessageBuilder) Channel(channel string) *publishFileMessageBuilder {
b.opts.Channel = channel
return b
}
// Message sets the Payload for the PublishFileMessage request.
func (b *publishFileMessageBuilder) FileName(name string) *publishFileMessageBuilder {
b.opts.FileName = name
return b
}
// Message sets the Payload for the PublishFileMessage request.
func (b *publishFileMessageBuilder) Message(msg PNPublishFileMessage) *publishFileMessageBuilder {
b.opts.Message = msg
return b
}
// Message sets the Payload for the PublishFileMessage request.
func (b *publishFileMessageBuilder) MessageText(msg string) *publishFileMessageBuilder {
b.opts.MessageText = msg
return b
}
// Message sets the Payload for the PublishFileMessage request.
func (b *publishFileMessageBuilder) FileID(id string) *publishFileMessageBuilder {
b.opts.FileID = id
return b
}
// usePost sends the PublishFileMessage request using HTTP POST. Not implemented
func (b *publishFileMessageBuilder) usePost(post bool) *publishFileMessageBuilder {
b.opts.UsePost = post
return b
}
// Transport sets the Transport for the objectAPICreateUsers request.
func (b *publishFileMessageBuilder) Transport(tr http.RoundTripper) *publishFileMessageBuilder {
b.opts.Transport = tr
return b
}
// QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
func (b *publishFileMessageBuilder) QueryParam(queryParam map[string]string) *publishFileMessageBuilder {
b.opts.QueryParam = queryParam
return b
}
// Execute runs the PublishFileMessage request.
func (b *publishFileMessageBuilder) Execute() (*PublishFileMessageResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyPublishFileMessageResponse, status, err
}
return newPublishFileMessageResponse(rawJSON, b.opts, status)
}
type publishFileMessageOpts struct {
pubnub *PubNub
Message interface{}
Channel string
UsePost bool
TTL int
Meta interface{}
ShouldStore bool
setTTL bool
setShouldStore bool
MessageText string
FileID string
FileName string
QueryParam map[string]string
Transport http.RoundTripper
ctx Context
}
func (o *publishFileMessageOpts) config() Config {
return *o.pubnub.Config
}
func (o *publishFileMessageOpts) client() *http.Client {
return o.pubnub.GetClient()
}
func (o *publishFileMessageOpts) context() Context {
return o.ctx
}
func (o *publishFileMessageOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if o.config().PublishKey == "" {
return newValidationError(o, StrMissingPubKey)
}
if (o.Message == nil) && (o.FileID == "") {
return newValidationError(o, StrMissingFileID)
}
if (o.Message == nil) && (o.FileName == "") {
return newValidationError(o, StrMissingFileName)
}
if o.Message != nil {
if filesPayload, okFile := o.Message.(PNPublishFileMessage); okFile {
if filesPayload.PNFile != nil {
if filesPayload.PNFile.ID == "" {
return newValidationError(o, StrMissingFileID)
}
if filesPayload.PNFile.Name == "" {
return newValidationError(o, StrMissingFileName)
}
} else {
return newValidationError(o, StrMissingFileID)
}
} else {
return newValidationError(o, StrMissingMessage)
}
}
return nil
}
func (o *publishFileMessageOpts) buildPath() (string, error) {
if o.UsePost == true {
return fmt.Sprintf(publishFileMessagePostPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0"), nil
}
if o.Message == nil {
m := &PNPublishMessage{
Text: o.MessageText,
}
file := &PNFileInfoForPublish{
ID: o.FileID,
Name: o.FileName,
}
o.Message = PNPublishFileMessage{
PNFile: file,
PNMessage: m,
}
}
if cipherKey := o.pubnub.Config.CipherKey; cipherKey != "" {
var msg string
var p *publishBuilder
if o.context() != nil {
p = newPublishBuilderWithContext(o.pubnub, o.context())
} else {
p = newPublishBuilder(o.pubnub)
}
p.opts.Message = o.Message
msg, errJSONMarshal := p.opts.encryptProcessing(cipherKey)
if errJSONMarshal != nil {
return "", errJSONMarshal
}
o.pubnub.Config.Log.Println("EncryptString: encrypted", msg)
return fmt.Sprintf(publishFileMessageGetPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0",
utils.URLEncode(msg)), nil
}
var msg string
jsonEncBytes, errEnc := json.Marshal(o.Message)
if errEnc != nil {
o.pubnub.Config.Log.Printf("ERROR: Publish error: %s\n", errEnc.Error())
return "", errEnc
}
msg = string(jsonEncBytes)
return fmt.Sprintf(publishFileMessageGetPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0",
utils.URLEncode(msg),
), nil
}
func (o *publishFileMessageOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *publishFileMessageOpts) jobQueue() chan *JobQItem {
return o.pubnub.jobQueue
}
func (o *publishFileMessageOpts) buildBody() ([]byte, error) {
if o.UsePost {
jsonEncBytes, errEnc := json.Marshal(o.Message)
if errEnc != nil {
o.pubnub.Config.Log.Printf("ERROR: PublishFileMessage error: %s\n", errEnc.Error())
return []byte{}, errEnc
}
return jsonEncBytes, nil
}
return []byte{}, nil
}
func (o *publishFileMessageOpts) buildBodyMultipartFileUpload() (bytes.Buffer, *multipart.Writer, int64, error) {
return bytes.Buffer{}, nil, 0, errors.New("Not required")
}
func (o *publishFileMessageOpts) httpMethod() string {
if o.UsePost {
return "POST"
}
return "GET"
}
func (o *publishFileMessageOpts) isAuthRequired() bool {
return true
}
func (o *publishFileMessageOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *publishFileMessageOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *publishFileMessageOpts) operationType() OperationType {
return PNPublishFileMessageOperation
}
func (o *publishFileMessageOpts) telemetryManager() *TelemetryManager {
return o.pubnub.telemetryManager
}
// PublishFileMessageResponse is the response to PublishFileMessage request.
type PublishFileMessageResponse struct {
Timestamp int64
}
func newPublishFileMessageResponse(jsonBytes []byte, o *publishFileMessageOpts,
status StatusResponse) (*PublishFileMessageResponse, StatusResponse, error) {
resp := &PublishFileMessageResponse{}
var value []interface{}
err := json.Unmarshal(jsonBytes, &value)
if err != nil {
e := pnerr.NewResponseParsingError("Error unmarshalling response",
ioutil.NopCloser(bytes.NewBufferString(string(jsonBytes))), err)
return emptyPublishFileMessageResponse, status, e
}
if len(value) > 1 {
timeString, ok := value[2].(string)
if !ok {
return emptyPublishFileMessageResponse, status, pnerr.NewResponseParsingError(fmt.Sprintf("Error unmarshalling response 2, %s %v", value[2], value), nil, nil)
}
timestamp, err := strconv.ParseInt(timeString, 10, 64)
if err != nil {
return emptyPublishFileMessageResponse, status, err
}
return &PublishFileMessageResponse{
Timestamp: timestamp,
}, status, nil
}
return resp, status, nil
}