Skip to content

Commit

Permalink
UUID param in Set and Get State (#70) (#71)
Browse files Browse the repository at this point in the history
* UUID param in Set and Get State

* UUID param in Set and Get State
  • Loading branch information
davidnub authored Aug 1, 2019
1 parent 62dbd34 commit 490623c
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 8 deletions.
9 changes: 8 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
---
changelog:
-
changes:
-
text: "UUID param in Set and Get State"
type: improvement
date: Aug 1, 19
version: v4.2.5
-
changes:
-
Expand Down Expand Up @@ -363,4 +370,4 @@ supported-platforms:
- "Mac OS X 10.8 or later, amd64"
- "Windows 7 or later, amd64, 386"
version: "PubNub Go SDK"
version: v4.2.4
version: v4.2.5
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# PubNub 4.2.4 client for Go
# PubNub 4.2.5 client for Go
* Go (1.9+)

# Please direct all Support Questions and Concerns to [email protected]
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.4
4.2.5
4 changes: 2 additions & 2 deletions examples/cli/cli_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func setStateRequest(args []string) {
}
}

res, status, err := pn.SetState().Channels([]string{channel}).State(state).Execute()
res, status, err := pn.SetState().Channels([]string{channel}).State(state).UUID("nuuid").Execute()

fmt.Println("status===>", status)
if err != nil {
Expand All @@ -638,7 +638,7 @@ func getStateRequest(args []string) {
channel = args[0]
}

res, status, err := pn.GetState().Channels([]string{channel}).Execute()
res, status, err := pn.GetState().Channels([]string{channel}).UUID("").Execute()

fmt.Println("status===>", status)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion get_state_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ func (o *getStateOpts) buildPath() (string, error) {
channels = append(channels, utils.PamEncode(channel))
}

uuid := o.UUID
if uuid == "" {
uuid = o.pubnub.Config.UUID
}

return fmt.Sprintf(getStatePath,
o.pubnub.Config.SubscribeKey,
strings.Join(channels, ","),
utils.URLEncode(o.pubnub.Config.UUID)), nil
utils.URLEncode(uuid)), nil
}

func (o *getStateOpts) buildQuery() (*url.Values, error) {
Expand Down Expand Up @@ -191,6 +196,7 @@ func (o *getStateOpts) telemetryManager() *TelemetryManager {
// GetStateResponse is the struct returned when the Execute function of GetState is called.
type GetStateResponse struct {
State map[string]interface{}
UUID string
}

func newGetStateResponse(jsonBytes []byte, status StatusResponse) (
Expand Down Expand Up @@ -222,6 +228,9 @@ func newGetStateResponse(jsonBytes []byte, status StatusResponse) (
return emptyGetStateResp, status, errors.New(message)
}

if v["uuid"] != nil {
resp.UUID = v["uuid"].(string)
}
m := make(map[string]interface{})
if v["channel"] != nil {
if channel, ok2 := v["channel"].(string); ok2 {
Expand Down
34 changes: 34 additions & 0 deletions get_state_request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pubnub

import (
"fmt"
"net/url"
"testing"

Expand Down Expand Up @@ -97,6 +98,39 @@ func TestGetStateBasicRequest(t *testing.T) {
assert.Equal([]byte{}, body)
}

func TestGetStateBasicRequestWithUUID(t *testing.T) {
assert := assert.New(t)

uuid := "customuuid"

opts := &getStateOpts{
Channels: []string{"ch"},
ChannelGroups: []string{"cg"},
UUID: uuid,
pubnub: pubnub,
}

path, err := opts.buildPath()
assert.Nil(err)
u := &url.URL{
Path: path,
}
h.AssertPathsEqual(t,
fmt.Sprintf("/v2/presence/sub-key/sub_key/channel/ch/uuid/%s", uuid),
u.EscapedPath(), []int{})

query, err := opts.buildQuery()
assert.Nil(err)

expected := &url.Values{}
expected.Set("channel-group", "cg")
h.AssertQueriesEqual(t, expected, query, []string{"pnsdk", "uuid"}, []string{})

body, err := opts.buildBody()
assert.Nil(err)
assert.Equal([]byte{}, body)
}

func TestNewGetStateBuilder(t *testing.T) {
assert := assert.New(t)

Expand Down
2 changes: 1 addition & 1 deletion pubnub.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Default constants
const (
// Version :the version of the SDK
Version = "4.2.4"
Version = "4.2.5"
// MaxSequence for publish messages
MaxSequence = 65535
)
Expand Down
14 changes: 13 additions & 1 deletion set_state_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func (b *setStateBuilder) QueryParam(queryParam map[string]string) *setStateBuil
return b
}

// UUID sets the UUID for the Set State request.
func (b *setStateBuilder) UUID(uuid string) *setStateBuilder {
b.opts.UUID = uuid

return b
}

// Execute runs the the Set State request and returns the SetStateResponse
func (b *setStateBuilder) Execute() (*SetStateResponse, StatusResponse, error) {
stateOperation := StateOperation{}
Expand All @@ -87,6 +94,7 @@ type setStateOpts struct {
State map[string]interface{}
Channels []string
ChannelGroups []string
UUID string
QueryParam map[string]string
pubnub *PubNub
stringState string
Expand Down Expand Up @@ -129,11 +137,15 @@ func (o *setStateOpts) validate() error {

func (o *setStateOpts) buildPath() (string, error) {
channels := string(utils.JoinChannels(o.Channels))
uuid := o.UUID
if uuid == "" {
uuid = o.pubnub.Config.UUID
}

return fmt.Sprintf(setStatePath,
o.pubnub.Config.SubscribeKey,
channels,
utils.URLEncode(o.pubnub.Config.UUID),
utils.URLEncode(uuid),
), nil
}

Expand Down
21 changes: 21 additions & 0 deletions set_state_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func TestNewSetStateBuilder(t *testing.T) {
u.EscapedPath(), []int{})
}

func TestNewSetStateBuilderWithUUID(t *testing.T) {
assert := assert.New(t)

o := newSetStateBuilder(pubnub)
o.Channels([]string{"ch1", "ch2", "ch3"})
uuid := "customuuid"
o.UUID(uuid)

path, err := o.opts.buildPath()
assert.Nil(err)

u := &url.URL{
Path: path,
}

h.AssertPathsEqual(t,
fmt.Sprintf("/v2/presence/sub-key/sub_key/channel/ch1,ch2,ch3/uuid/%s/data",
uuid),
u.EscapedPath(), []int{})
}

func TestNewSetStateBuilderContext(t *testing.T) {
assert := assert.New(t)

Expand Down
36 changes: 36 additions & 0 deletions tests/e2e/set_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ func TestSetStateSucessNotStubbed(t *testing.T) {
}
}

func TestSetStateSucessNotStubbedWithUUID(t *testing.T) {
assert := assert.New(t)

pn := pubnub.NewPubNub(configCopy())
uuid := "nuuid"

state := make(map[string]interface{})
state["age"] = "20"

setStateRes, _, err := pn.SetState().State(state).UUID(uuid).Channels([]string{"ch"}).
ChannelGroups([]string{"cg"}).Execute()

assert.Nil(err)
if s, ok := setStateRes.State.(map[string]interface{}); ok {
assert.Equal("20", s["age"])
} else {
assert.Fail(fmt.Sprintf("!map[string]interface{} %v %v", reflect.TypeOf(setStateRes.State).Kind(), reflect.TypeOf(setStateRes.State)))
}

assert.Equal("OK", setStateRes.Message)

getStateRes, _, err := pn.GetState().
Channels([]string{"ch"}).
ChannelGroups([]string{"cg"}).
UUID(uuid).
Execute()

assert.Nil(err)
if s, ok := getStateRes.State["ch"].(map[string]interface{}); ok {
assert.Equal("20", s["age"])
} else {
assert.Fail(fmt.Sprintf("!map[string]interface{} %v %v", reflect.TypeOf(getStateRes.State["ch"]).Kind(), reflect.TypeOf(setStateRes.State)))
}
assert.Equal(uuid, getStateRes.UUID)
}

func TestSetStateSucessNotStubbedContext(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit 490623c

Please sign in to comment.