-
Notifications
You must be signed in to change notification settings - Fork 63
/
message_counts_request.go
201 lines (158 loc) · 5.84 KB
/
message_counts_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
package pubnub
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"reflect"
"strconv"
"strings"
"github.com/pubnub/go/v7/pnerr"
"github.com/pubnub/go/v7/utils"
"net/http"
"net/url"
)
var emptyMessageCountsResp *MessageCountsResponse
const messageCountsPath = "/v3/history/sub-key/%s/message-counts/%s"
type messageCountsBuilder struct {
opts *messageCountsOpts
}
func newMessageCountsBuilder(pubnub *PubNub) *messageCountsBuilder {
return newMessageCountsBuilderWithContext(pubnub, pubnub.ctx)
}
func newMessageCountsOpts(pubnub *PubNub, ctx Context) *messageCountsOpts {
return &messageCountsOpts{endpointOpts: endpointOpts{pubnub: pubnub, ctx: ctx}}
}
func newMessageCountsBuilderWithContext(pubnub *PubNub,
context Context) *messageCountsBuilder {
builder := messageCountsBuilder{
opts: newMessageCountsOpts(pubnub, context)}
return &builder
}
// Channels sets the Channels for the MessageCounts request.
func (b *messageCountsBuilder) Channels(channels []string) *messageCountsBuilder {
b.opts.Channels = channels
return b
}
// Deprecated: Use ChannelsTimetoken instead, pass one value in ChannelsTimetoken to achieve the same results.
// TODO: Remove in next major version bump
func (b *messageCountsBuilder) Timetoken(timetoken int64) *messageCountsBuilder {
b.opts.Timetoken = timetoken
return b
}
// ChannelsTimetoken Array of timetokens, in order of the channels list..
func (b *messageCountsBuilder) ChannelsTimetoken(channelsTimetoken []int64) *messageCountsBuilder {
b.opts.ChannelsTimetoken = channelsTimetoken
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 *messageCountsBuilder) QueryParam(queryParam map[string]string) *messageCountsBuilder {
b.opts.QueryParam = queryParam
return b
}
// Transport sets the Transport for the MessageCounts request.
func (b *messageCountsBuilder) Transport(tr http.RoundTripper) *messageCountsBuilder {
b.opts.Transport = tr
return b
}
// Execute runs the MessageCounts request.
func (b *messageCountsBuilder) Execute() (*MessageCountsResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyMessageCountsResp, status, err
}
return newMessageCountsResponse(rawJSON, b.opts, status)
}
type messageCountsOpts struct {
endpointOpts
Channels []string
Timetoken int64
ChannelsTimetoken []int64
QueryParam map[string]string
// nil hacks
Transport http.RoundTripper
}
func (o *messageCountsOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if len(o.Channels) <= 0 {
return newValidationError(o, StrMissingChannel)
}
if (len(o.ChannelsTimetoken) <= 0) && (o.Timetoken == 0) {
return newValidationError(o, StrChannelsTimetoken)
}
if (len(o.ChannelsTimetoken) > 1) && (len(o.Channels) != len(o.ChannelsTimetoken)) {
return newValidationError(o, StrChannelsTimetokenLength)
}
return nil
}
func (o *messageCountsOpts) buildPath() (string, error) {
channels := utils.JoinChannels(o.Channels)
return fmt.Sprintf(messageCountsPath,
o.pubnub.Config.SubscribeKey,
channels), nil
}
func (o *messageCountsOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
if (o.ChannelsTimetoken != nil) && (len(o.ChannelsTimetoken) == 1) {
q.Set("timetoken", strconv.FormatInt(o.ChannelsTimetoken[0], 10))
//q.Set("channelsTimetoken", "")
} else if o.ChannelsTimetoken != nil {
//q.Set("timetoken", "")
q.Set("channelsTimetoken", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(o.ChannelsTimetoken)), ","), "[]"))
} else {
// TODO: Remove in next major version bump
q.Set("timetoken", strconv.FormatInt(o.Timetoken, 10))
//q.Set("channelsTimetoken", "")
}
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *messageCountsOpts) isAuthRequired() bool {
return true
}
func (o *messageCountsOpts) requestTimeout() int {
return o.pubnub.Config.NonSubscribeRequestTimeout
}
func (o *messageCountsOpts) connectTimeout() int {
return o.pubnub.Config.ConnectTimeout
}
func (o *messageCountsOpts) operationType() OperationType {
return PNMessageCountsOperation
}
// MessageCountsResponse is the response to MessageCounts request. It contains a map of type MessageCountsResponseItem
type MessageCountsResponse struct {
Channels map[string]int
}
//http://ps.pndsn.com/v3/history/sub-key/demo/message-counts/my-channel,my-channel1?timestamp=1549982652&pnsdk=PubNub-Go/4.1.6&uuid=pn-82f145ea-adc3-4917-a11d-76a957347a82&timetoken=15499825804610610&channelsTimetoken=15499825804610610,15499925804610615&auth=akey&signature=pVDVge_suepcOlSMllpsXg_jpOjtEpW7B3HHFaViI4s=
//{"status": 200, "error": false, "error_message": "", "channels": {"my-channel1":1,"my-channel":2}}
func newMessageCountsResponse(jsonBytes []byte, o *messageCountsOpts,
status StatusResponse) (*MessageCountsResponse, StatusResponse, error) {
resp := &MessageCountsResponse{}
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 emptyMessageCountsResp, status, e
}
if result, ok := value.(map[string]interface{}); ok {
o.pubnub.Config.Log.Println(result["channels"])
if channels, ok1 := result["channels"].(map[string]interface{}); ok1 {
if channels != nil {
resp.Channels = make(map[string]int)
for ch, v := range channels {
resp.Channels[ch] = int(v.(float64))
}
} else {
o.pubnub.Config.Log.Printf("type assertion to map failed %v\n", result)
}
} else {
o.pubnub.Config.Log.Println("Assertion failed", reflect.TypeOf(result["channels"]))
}
} else {
o.pubnub.Config.Log.Printf("type assertion to map failed %v\n", value)
}
return resp, status, nil
}