-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from pubnub/CE-3355-Unread-Message-Count-API
Message Counts API
- Loading branch information
Showing
13 changed files
with
558 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
# PubNub 4.1.6 client for Go | ||
# PubNub 4.1.7 client for Go | ||
* Go (1.9+) | ||
|
||
# Please direct all Support Questions and Concerns to [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.1.6 | ||
4.1.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package pubnub | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/pubnub/go/pnerr" | ||
"github.com/pubnub/go/utils" | ||
"reflect" | ||
"strings" | ||
|
||
"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 { | ||
builder := messageCountsBuilder{ | ||
opts: &messageCountsOpts{ | ||
pubnub: pubnub, | ||
}, | ||
} | ||
|
||
return &builder | ||
} | ||
|
||
func newMessageCountsBuilderWithContext(pubnub *PubNub, | ||
context Context) *messageCountsBuilder { | ||
builder := messageCountsBuilder{ | ||
opts: &messageCountsOpts{ | ||
pubnub: pubnub, | ||
ctx: context, | ||
}, | ||
} | ||
|
||
return &builder | ||
} | ||
|
||
// Channels sets the Channels for the MessageCounts request. | ||
func (b *messageCountsBuilder) Channels(channels []string) *messageCountsBuilder { | ||
b.opts.Channels = channels | ||
return b | ||
} | ||
|
||
// Timetoken sets the number of items to return in the MessageCounts request. | ||
func (b *messageCountsBuilder) Timetoken(timetoken string) *messageCountsBuilder { | ||
b.opts.Timetoken = timetoken | ||
return b | ||
} | ||
|
||
// ChannelsTimetoken sets the order of messages in the MessageCounts request. | ||
func (b *messageCountsBuilder) ChannelsTimetoken(channelsTimetoken []string) *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 { | ||
pubnub *PubNub | ||
|
||
Channels []string | ||
Timetoken string | ||
ChannelsTimetoken []string | ||
|
||
QueryParam map[string]string | ||
|
||
// nil hacks | ||
Transport http.RoundTripper | ||
|
||
ctx Context | ||
} | ||
|
||
func (o *messageCountsOpts) config() Config { | ||
return *o.pubnub.Config | ||
} | ||
|
||
func (o *messageCountsOpts) client() *http.Client { | ||
return o.pubnub.GetClient() | ||
} | ||
|
||
func (o *messageCountsOpts) context() Context { | ||
return o.ctx | ||
} | ||
|
||
func (o *messageCountsOpts) validate() error { | ||
if o.config().SubscribeKey == "" { | ||
return newValidationError(o, StrMissingSubKey) | ||
} | ||
|
||
if len(o.Channels) <= 0 { | ||
return newValidationError(o, StrMissingChannel) | ||
} | ||
|
||
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) | ||
|
||
q.Set("timetoken", o.Timetoken) | ||
q.Set("channelsTimetoken", strings.Join(o.ChannelsTimetoken, ",")) | ||
SetQueryParam(q, o.QueryParam) | ||
|
||
return q, nil | ||
} | ||
|
||
func (o *messageCountsOpts) jobQueue() chan *JobQItem { | ||
return o.pubnub.jobQueue | ||
} | ||
|
||
func (o *messageCountsOpts) buildBody() ([]byte, error) { | ||
return []byte{}, nil | ||
} | ||
|
||
func (o *messageCountsOpts) httpMethod() string { | ||
return "GET" | ||
} | ||
|
||
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 | ||
} | ||
|
||
func (o *messageCountsOpts) telemetryManager() *TelemetryManager { | ||
return o.pubnub.telemetryManager | ||
} | ||
|
||
// 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 | ||
} |
Oops, something went wrong.