-
Notifications
You must be signed in to change notification settings - Fork 63
/
fire_request.go
253 lines (205 loc) · 5.13 KB
/
fire_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
package pubnub
import (
"fmt"
"strconv"
"github.com/pubnub/go/v7/pnerr"
"github.com/pubnub/go/v7/utils"
"net/http"
"net/url"
)
func newFireOpts(pubnub *PubNub, ctx Context) *fireOpts {
return &fireOpts{
endpointOpts: endpointOpts{
pubnub: pubnub,
ctx: ctx,
},
Serialize: true,
}
}
type fireOpts struct {
endpointOpts
TTL int
Channel string
Message interface{}
Meta interface{}
UsePost bool
Serialize bool
ShouldStore bool
DoNotReplicate bool
Transport http.RoundTripper
QueryParam map[string]string
// nil hacks
setTTL bool
setShouldStore bool
}
type fireBuilder struct {
opts *fireOpts
}
func newFireBuilder(pubnub *PubNub) *fireBuilder {
return newFireBuilderWithContext(pubnub, pubnub.ctx)
}
func newFireBuilderWithContext(pubnub *PubNub, context Context) *fireBuilder {
return &fireBuilder{
opts: newFireOpts(
pubnub,
context,
),
}
}
// TTL sets the TTL (hours) for the Fire request.
func (b *fireBuilder) TTL(ttl int) *fireBuilder {
b.opts.TTL = ttl
return b
}
// Channel sets the Channel for the Fire request.
func (b *fireBuilder) Channel(ch string) *fireBuilder {
b.opts.Channel = ch
return b
}
// Message sets the Payload for the Fire request.
func (b *fireBuilder) Message(msg interface{}) *fireBuilder {
b.opts.Message = msg
return b
}
// Meta sets the Meta Payload for the Fire request.
func (b *fireBuilder) Meta(meta interface{}) *fireBuilder {
b.opts.Meta = meta
return b
}
// UsePost sends the Fire request using HTTP POST.
func (b *fireBuilder) UsePost(post bool) *fireBuilder {
b.opts.UsePost = post
return b
}
// Serialize when true (default) serializes the payload before publish.
// Set to false if pre serialized payload is being used.
func (b *fireBuilder) Serialize(serialize bool) *fireBuilder {
b.opts.Serialize = serialize
return b
}
// Transport sets the Transport for the Fire request.
func (b *fireBuilder) Transport(tr http.RoundTripper) *fireBuilder {
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 *fireBuilder) QueryParam(queryParam map[string]string) *fireBuilder {
b.opts.QueryParam = queryParam
return b
}
// Execute runs the Fire request.
func (b *fireBuilder) Execute() (*PublishResponse, StatusResponse, error) {
b.opts.ShouldStore = false
b.opts.DoNotReplicate = true
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyPublishResponse, status, err
}
return newPublishResponse(rawJSON, status)
}
func (o *fireOpts) 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 *fireOpts) 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 message []byte
var err error
if o.pubnub.getCryptoModule() != nil {
var msg string
if msg, err = serializeEncryptAndSerialize(o.pubnub.getCryptoModule(), o.Message, o.Serialize); err != nil {
o.pubnub.Config.Log.Printf("error in serializing: %v\n", err)
return "", err
}
message = []byte(msg)
} else {
message, err = utils.ValueAsString(o.Message)
if err != nil {
return "", err
}
}
return fmt.Sprintf(publishGetPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0",
utils.URLEncode(string(message))), nil
}
func (o *fireOpts) 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))
}
q.Set("store", "0")
q.Set("norep", "true")
if o.setTTL {
if o.TTL > 0 {
q.Set("ttl", strconv.Itoa(o.TTL))
}
}
q.Set("seqn", strconv.Itoa(o.pubnub.getPublishSequence()))
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *fireOpts) buildBody() ([]byte, error) {
if o.UsePost {
var msg []byte
if o.Serialize {
m, err := utils.ValueAsString(o.Message)
if err != nil {
return []byte{}, err
}
msg = m
} else {
if s, ok := o.Message.(string); ok {
msg = []byte(s)
} else {
err := pnerr.NewBuildRequestError("Type error, only string is expected")
return []byte{}, err
}
}
if o.pubnub.getCryptoModule() != nil {
enc, err := encryptString(o.pubnub.getCryptoModule(), string(msg))
if err != nil {
return []byte{}, err
}
m, err := utils.ValueAsString(enc)
if err != nil {
return []byte{}, err
}
return m, nil
}
return msg, nil
}
return []byte{}, nil
}
func (o *fireOpts) httpMethod() string {
if o.UsePost {
return "POST"
}
return "GET"
}
func (o *fireOpts) operationType() OperationType {
return PNFireOperation
}