-
Notifications
You must be signed in to change notification settings - Fork 62
/
publish_request.go
420 lines (338 loc) · 10 KB
/
publish_request.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package pubnub
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"reflect"
"strconv"
"github.com/pubnub/go/v7/pnerr"
"github.com/pubnub/go/v7/utils"
"net/http"
"net/url"
)
const publishGetPath = "/publish/%s/%s/0/%s/%s/%s"
const publishPostPath = "/publish/%s/%s/0/%s/%s"
var emptyPublishResponse *PublishResponse
type publishOpts struct {
pubnub *PubNub
TTL int
Channel string
Message interface{}
Meta interface{}
UsePost bool
ShouldStore bool
Serialize bool
DoNotReplicate bool
QueryParam map[string]string
Transport http.RoundTripper
ctx Context
// nil hacks
setTTL bool
setShouldStore bool
}
// PublishResponse is the response after the execution on Publish and Fire operations.
type PublishResponse struct {
Timestamp int64
}
type publishBuilder struct {
opts *publishOpts
}
func newPublishResponse(jsonBytes []byte, status StatusResponse) (
*PublishResponse, StatusResponse, error) {
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 emptyPublishResponse, status, e
}
timeString, ok := value[2].(string)
if !ok {
return emptyPublishResponse, status, pnerr.NewResponseParsingError(fmt.Sprintf("Error unmarshalling response, %s %v", value[2], value), nil, nil)
}
timestamp, err := strconv.ParseInt(timeString, 10, 64)
if err != nil {
return emptyPublishResponse, status, err
}
return &PublishResponse{
Timestamp: timestamp,
}, status, nil
}
func newPublishBuilder(pubnub *PubNub) *publishBuilder {
builder := publishBuilder{
opts: &publishOpts{
pubnub: pubnub,
Serialize: true,
},
}
return &builder
}
func newPublishBuilderWithContext(pubnub *PubNub, context Context) *publishBuilder {
builder := publishBuilder{
opts: &publishOpts{
pubnub: pubnub,
ctx: context,
Serialize: true,
},
}
return &builder
}
// TTL sets the TTL (hours) for the Publish request.
func (b *publishBuilder) TTL(ttl int) *publishBuilder {
b.opts.TTL = ttl
b.opts.setTTL = true
return b
}
// Channel sets the Channel for the Publish request.
func (b *publishBuilder) Channel(ch string) *publishBuilder {
b.opts.Channel = ch
return b
}
// Message sets the Payload for the Publish request.
func (b *publishBuilder) Message(msg interface{}) *publishBuilder {
b.opts.Message = msg
return b
}
// Meta sets the Meta Payload for the Publish request.
func (b *publishBuilder) Meta(meta interface{}) *publishBuilder {
b.opts.Meta = meta
return b
}
// UsePost sends the Publish request using HTTP POST.
func (b *publishBuilder) UsePost(post bool) *publishBuilder {
b.opts.UsePost = post
return b
}
// ShouldStore if true the messages are stored in History
func (b *publishBuilder) ShouldStore(store bool) *publishBuilder {
b.opts.ShouldStore = store
b.opts.setShouldStore = true
return b
}
// Serialize when true (default) serializes the payload before publish.
// Set to false if pre serialized payload is being used.
func (b *publishBuilder) Serialize(serialize bool) *publishBuilder {
b.opts.Serialize = serialize
return b
}
// DoNotReplicate stores the message in one DC.
func (b *publishBuilder) DoNotReplicate(repl bool) *publishBuilder {
b.opts.DoNotReplicate = repl
return b
}
// Transport sets the Transport for the Publish request.
func (b *publishBuilder) Transport(tr http.RoundTripper) *publishBuilder {
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 *publishBuilder) QueryParam(queryParam map[string]string) *publishBuilder {
b.opts.QueryParam = queryParam
return b
}
// Execute runs the Publish request.
func (b *publishBuilder) Execute() (*PublishResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyPublishResponse, status, err
}
return newPublishResponse(rawJSON, status)
}
func (o *publishOpts) config() Config {
return *o.pubnub.Config
}
func (o *publishOpts) client() *http.Client {
return o.pubnub.GetClient()
}
func (o *publishOpts) context() Context {
return o.ctx
}
func (o *publishOpts) validate() error {
if o.config().PublishKey == "" {
return newValidationError(o, StrMissingPubKey)
}
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if o.Channel == "" {
return newValidationError(o, StrMissingChannel)
}
if o.Message == nil {
return newValidationError(o, StrMissingMessage)
}
return nil
}
func (o *publishOpts) encryptProcessing(cipherKey string) (string, error) {
var msg string
var errJSONMarshal error
o.pubnub.Config.Log.Println("EncryptString: encrypting", fmt.Sprintf("%s", o.Message))
if o.pubnub.Config.DisablePNOtherProcessing {
if msg, errJSONMarshal = utils.SerializeEncryptAndSerialize(o.Message, cipherKey, o.Serialize, o.pubnub.Config.UseRandomInitializationVector); errJSONMarshal != nil {
o.pubnub.Config.Log.Printf("error in serializing: %v\n", errJSONMarshal)
return "", errJSONMarshal
}
} else {
//encrypt pn_other only
o.pubnub.Config.Log.Println("encrypt pn_other only", "reflect.TypeOf(data).Kind()", reflect.TypeOf(o.Message).Kind(), o.Message)
switch v := o.Message.(type) {
case map[string]interface{}:
msgPart, ok := v["pn_other"].(string)
if ok {
o.pubnub.Config.Log.Println(ok, msgPart)
encMsg, errJSONMarshal := utils.SerializeAndEncrypt(msgPart, cipherKey, o.Serialize, o.pubnub.Config.UseRandomInitializationVector)
if errJSONMarshal != nil {
o.pubnub.Config.Log.Printf("error in serializing: %v\n", errJSONMarshal)
return "", errJSONMarshal
}
v["pn_other"] = encMsg
jsonEncBytes, errEnc := json.Marshal(v)
if errEnc != nil {
o.pubnub.Config.Log.Printf("ERROR: Publish error: %s\n", errEnc.Error())
return "", errEnc
}
msg = string(jsonEncBytes)
} else {
if msg, errJSONMarshal = utils.SerializeEncryptAndSerialize(o.Message, cipherKey, o.Serialize, o.pubnub.Config.UseRandomInitializationVector); errJSONMarshal != nil {
o.pubnub.Config.Log.Printf("error in serializing: %v\n", errJSONMarshal)
return "", errJSONMarshal
}
}
break
default:
if msg, errJSONMarshal = utils.SerializeEncryptAndSerialize(o.Message, cipherKey, o.Serialize, o.pubnub.Config.UseRandomInitializationVector); errJSONMarshal != nil {
o.pubnub.Config.Log.Printf("error in serializing: %v\n", errJSONMarshal)
return "", errJSONMarshal
}
break
}
}
return msg, nil
}
func (o *publishOpts) buildPath() (string, error) {
if o.UsePost == true {
return fmt.Sprintf(publishPostPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0"), nil
}
var msg string
var errJSONMarshal error
if cipherKey := o.pubnub.Config.CipherKey; cipherKey != "" {
if msg, errJSONMarshal = o.encryptProcessing(cipherKey); errJSONMarshal != nil {
return "", errJSONMarshal
}
o.pubnub.Config.Log.Println("EncryptString: encrypted", msg)
} else {
if o.Serialize {
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)
o.pubnub.Config.Log.Println("len(jsonEncBytes)", len(jsonEncBytes))
} else {
if serializedMsg, ok := o.Message.(string); ok {
msg = serializedMsg
} else {
return "", pnerr.NewBuildRequestError("buildpath: Message is not JSON serialized.")
}
}
}
return fmt.Sprintf(publishGetPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0",
utils.URLEncode(msg)), nil
}
func (o *publishOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
if o.Meta != nil {
meta, err := utils.ValueAsString(o.Meta)
if err != nil {
return &url.Values{}, err
}
q.Set("meta", string(meta))
}
if o.setShouldStore {
if o.ShouldStore {
q.Set("store", "1")
} else {
q.Set("store", "0")
}
}
if o.setTTL {
if o.TTL > 0 {
q.Set("ttl", strconv.Itoa(o.TTL))
}
}
seqn := strconv.Itoa(o.pubnub.getPublishSequence())
o.pubnub.Config.Log.Println("seqn:", seqn)
q.Set("seqn", seqn)
SetQueryParam(q, o.QueryParam)
if o.DoNotReplicate == true {
q.Set("norep", "true")
}
o.pubnub.Config.Log.Println(q)
return q, nil
}
func (o *publishOpts) jobQueue() chan *JobQItem {
return o.pubnub.jobQueue
}
func (o *publishOpts) buildBody() ([]byte, error) {
if o.UsePost {
if cipherKey := o.pubnub.Config.CipherKey; cipherKey != "" {
msg, errJSONMarshal := o.encryptProcessing(cipherKey)
if errJSONMarshal != nil {
return []byte{}, errJSONMarshal
}
return []byte(msg), nil
}
if o.Serialize {
jsonEncBytes, errEnc := json.Marshal(o.Message)
if errEnc != nil {
o.pubnub.Config.Log.Printf("ERROR: Publish error: %s\n", errEnc.Error())
return []byte{}, errEnc
}
return jsonEncBytes, nil
}
serializedMsg, ok := o.Message.(string)
if ok {
return []byte(serializedMsg), nil
}
return []byte{}, pnerr.NewBuildRequestError("buildBody: Message is not JSON serialized.")
}
return []byte{}, nil
}
func (o *publishOpts) buildBodyMultipartFileUpload() (bytes.Buffer, *multipart.Writer, int64, error) {
return bytes.Buffer{}, nil, 0, errors.New("Not required")
}
func (o *publishOpts) httpMethod() string {
if o.UsePost {
return "POST"
}
return "GET"
}
func (o *publishOpts) isAuthRequired() bool {
return true
}
func (o *publishOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *publishOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *publishOpts) operationType() OperationType {
return PNPublishOperation
}
func (o *publishOpts) telemetryManager() *TelemetryManager {
return o.pubnub.telemetryManager
}
func (o *publishOpts) tokenManager() *TokenManager {
return o.pubnub.tokenManager
}