forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_state_request.go
196 lines (143 loc) · 3.66 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
package pubnub
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/pubnub/go/pnerr"
"github.com/pubnub/go/utils"
)
const GET_STATE_PATH = "/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
}
func (b *getStateBuilder) Channels(ch []string) *getStateBuilder {
b.opts.Channels = ch
return b
}
func (b *getStateBuilder) ChannelGroups(cg []string) *getStateBuilder {
b.opts.ChannelGroups = cg
return b
}
func (b *getStateBuilder) Uuid(uuid string) *getStateBuilder {
b.opts.Uuid = uuid
return b
}
func (b *getStateBuilder) Transport(
tr http.RoundTripper) *getStateBuilder {
b.opts.Transport = tr
return b
}
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(GET_STATE_PATH,
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)
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
}
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
}
if parsedValue, ok := value.(map[string]interface{}); ok {
if payload, ok := parsedValue["payload"].(map[string]interface{}); ok {
resp.State = payload
}
}
return resp, status, nil
}