-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_request.go
244 lines (194 loc) · 5.26 KB
/
signal_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
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 emptySignalResponse *SignalResponse
const signalGetPath = "/signal/%s/%s/0/%s/%s/%s"
const signalPostPath = "/signal/%s/%s/0/%s/%s"
type signalBuilder struct {
opts *signalOpts
}
func newSignalBuilder(pubnub *PubNub) *signalBuilder {
builder := signalBuilder{
opts: &signalOpts{
pubnub: pubnub,
},
}
builder.opts.UsePost = false
return &builder
}
func newSignalBuilderWithContext(pubnub *PubNub,
context Context) *signalBuilder {
builder := signalBuilder{
opts: &signalOpts{
pubnub: pubnub,
ctx: context,
},
}
return &builder
}
// Channel sets the Channel for the Signal request.
func (b *signalBuilder) Channel(channel string) *signalBuilder {
b.opts.Channel = channel
return b
}
// Message sets the Payload for the Signal request.
func (b *signalBuilder) Message(msg interface{}) *signalBuilder {
b.opts.Message = msg
return b
}
// usePost sends the Signal request using HTTP POST. Not implemented
func (b *signalBuilder) usePost(post bool) *signalBuilder {
b.opts.UsePost = post
return b
}
// Transport sets the Transport for the objectAPICreateUsers request.
func (b *signalBuilder) Transport(tr http.RoundTripper) *signalBuilder {
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 *signalBuilder) QueryParam(queryParam map[string]string) *signalBuilder {
b.opts.QueryParam = queryParam
return b
}
// Execute runs the Signal request.
func (b *signalBuilder) Execute() (*SignalResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptySignalResponse, status, err
}
return newSignalResponse(rawJSON, b.opts, status)
}
type signalOpts struct {
pubnub *PubNub
Message interface{}
Channel string
UsePost bool
QueryParam map[string]string
Transport http.RoundTripper
ctx Context
}
func (o *signalOpts) config() Config {
return *o.pubnub.Config
}
func (o *signalOpts) client() *http.Client {
return o.pubnub.GetClient()
}
func (o *signalOpts) context() Context {
return o.ctx
}
func (o *signalOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if o.config().PublishKey == "" {
return newValidationError(o, StrMissingPubKey)
}
return nil
}
func (o *signalOpts) buildPath() (string, error) {
if o.UsePost == true {
return fmt.Sprintf(signalPostPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0"), 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(signalGetPath,
o.pubnub.Config.PublishKey,
o.pubnub.Config.SubscribeKey,
utils.URLEncode(o.Channel),
"0",
utils.URLEncode(msg),
), nil
}
func (o *signalOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *signalOpts) jobQueue() chan *JobQItem {
return o.pubnub.jobQueue
}
func (o *signalOpts) buildBody() ([]byte, error) {
if o.UsePost {
jsonEncBytes, errEnc := json.Marshal(o.Message)
if errEnc != nil {
o.pubnub.Config.Log.Printf("ERROR: Signal error: %s\n", errEnc.Error())
return []byte{}, errEnc
}
return jsonEncBytes, nil
}
return []byte{}, nil
}
func (o *signalOpts) buildBodyMultipartFileUpload() (bytes.Buffer, *multipart.Writer, int64, error) {
return bytes.Buffer{}, nil, 0, errors.New("Not required")
}
func (o *signalOpts) httpMethod() string {
if o.UsePost {
return "POST"
}
return "GET"
}
func (o *signalOpts) isAuthRequired() bool {
return true
}
func (o *signalOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *signalOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *signalOpts) operationType() OperationType {
return PNSignalOperation
}
func (o *signalOpts) telemetryManager() *TelemetryManager {
return o.pubnub.telemetryManager
}
// SignalResponse is the response to Signal request.
type SignalResponse struct {
Timestamp int64
}
func newSignalResponse(jsonBytes []byte, o *signalOpts,
status StatusResponse) (*SignalResponse, StatusResponse, error) {
resp := &SignalResponse{}
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 emptySignalResponse, status, e
}
if len(value) > 1 {
timeString, ok := value[2].(string)
if !ok {
return emptySignalResponse, 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 emptySignalResponse, status, err
}
return &SignalResponse{
Timestamp: timestamp,
}, status, nil
}
return resp, status, nil
}