-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_state_request.go
250 lines (192 loc) · 5.2 KB
/
get_state_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
package pubnub
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/pubnub/go/pnerr"
"github.com/pubnub/go/utils"
)
const getStatePath = "/v2/presence/sub-key/%s/channel/%s/uuid/%s"
var emptyGetStateResp *GetStateResponse
type getStateBuilder struct {
opts *getStateOpts
}
func newGetStateBuilder(pubnub *PubNub) *getStateBuilder {
builder := getStateBuilder{
opts: &getStateOpts{
pubnub: pubnub,
},
}
return &builder
}
func newGetStateBuilderWithContext(pubnub *PubNub,
context Context) *getStateBuilder {
builder := getStateBuilder{
opts: &getStateOpts{
pubnub: pubnub,
ctx: context,
},
}
return &builder
}
// Channels sets the Channels for the Get State request.
func (b *getStateBuilder) Channels(ch []string) *getStateBuilder {
b.opts.Channels = ch
return b
}
// ChannelGroups sets the ChannelGroups for the Get State request.
func (b *getStateBuilder) ChannelGroups(cg []string) *getStateBuilder {
b.opts.ChannelGroups = cg
return b
}
// UUID sets the UUID for the Get State request.
func (b *getStateBuilder) UUID(uuid string) *getStateBuilder {
b.opts.UUID = uuid
return b
}
// Transport sets the Transport for the Get State request.
func (b *getStateBuilder) Transport(
tr http.RoundTripper) *getStateBuilder {
b.opts.Transport = tr
return b
}
// Execute runs the the Get State request.
func (b *getStateBuilder) Execute() (
*GetStateResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyGetStateResp, status, err
}
return newGetStateResponse(rawJSON, status)
}
type getStateOpts struct {
pubnub *PubNub
Channels []string
ChannelGroups []string
UUID string
Transport http.RoundTripper
ctx Context
}
func (o *getStateOpts) config() Config {
return *o.pubnub.Config
}
func (o *getStateOpts) client() *http.Client {
return o.pubnub.GetClient()
}
func (o *getStateOpts) context() Context {
return o.ctx
}
func (o *getStateOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if len(o.Channels) == 0 && len(o.ChannelGroups) == 0 {
return newValidationError(o, "Missing Channel or Channel Group")
}
return nil
}
func (o *getStateOpts) buildPath() (string, error) {
var channels []string
for _, channel := range o.Channels {
channels = append(channels, utils.PamEncode(channel))
}
return fmt.Sprintf(getStatePath,
o.pubnub.Config.SubscribeKey,
strings.Join(channels, ","),
utils.URLEncode(o.pubnub.Config.UUID)), nil
}
func (o *getStateOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
var groups []string
for _, group := range o.ChannelGroups {
groups = append(groups, utils.PamEncode(group))
}
q.Set("channel-group", strings.Join(groups, ","))
return q, nil
}
func (o *getStateOpts) buildBody() ([]byte, error) {
return []byte{}, nil
}
func (o *getStateOpts) httpMethod() string {
return "GET"
}
func (o *getStateOpts) isAuthRequired() bool {
return true
}
func (o *getStateOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *getStateOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *getStateOpts) operationType() OperationType {
return PNGetStateOperation
}
func (o *getStateOpts) telemetryManager() *TelemetryManager {
return o.pubnub.telemetryManager
}
// GetStateResponse is the struct returned when the Execute function of GetState is called.
type GetStateResponse struct {
State map[string]interface{}
}
func newGetStateResponse(jsonBytes []byte, status StatusResponse) (
*GetStateResponse, StatusResponse, error) {
resp := &GetStateResponse{}
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 emptyGetStateResp, status, e
}
v, ok := value.(map[string]interface{})
if !ok {
return emptyGetStateResp, status, errors.New("Response parsing error")
}
if v["error"] != nil {
message := ""
if v["message"] != nil {
if msg, ok := v["message"].(string); ok {
message = msg
}
}
return emptyGetStateResp, status, errors.New(message)
}
m := make(map[string]interface{})
if v["channel"] != nil {
if channel, ok2 := v["channel"].(string); ok2 {
if v["payload"] != nil {
val, ok := v["payload"].(interface{})
if !ok {
return emptyGetStateResp, status, errors.New("Response parsing payload")
}
m[channel] = val
} else {
return emptyGetStateResp, status, errors.New("Response parsing channel")
}
} else {
return emptyGetStateResp, status, errors.New("Response parsing channel 2")
}
} else {
if v["payload"] != nil {
val, ok := v["payload"].(map[string]interface{})
if !ok {
return emptyGetStateResp, status, errors.New("Response parsing payload 2")
}
channels, ok2 := val["channels"].(map[string]interface{})
if !ok2 {
return emptyGetStateResp, status, errors.New("Response parsing channels")
}
for ch, state := range channels {
m[ch] = state
}
}
}
resp.State = m
return resp, status, nil
}