Skip to content

Commit

Permalink
handler for private links
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Feb 17, 2019
1 parent e676efe commit 55ee1e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 13 additions & 4 deletions v2/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ func (c *Client) GenerateKey(key, channel, permissions string, ttl int) (string,
}

// CreatePrivateLink sends a request to create a private link.
func (c *Client) CreatePrivateLink(key, channel, name string, subscribe bool, options ...Option) (*Link, error) {
func (c *Client) CreatePrivateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error) {
resp, err := c.request("link", &linkRequest{
Name: name,
Key: key,
Channel: formatTopic("", channel, options),
Subscribe: subscribe,
Subscribe: optionalHandler != nil,
Private: true,
})
if err != nil {
Expand All @@ -342,18 +342,23 @@ func (c *Client) CreatePrivateLink(key, channel, name string, subscribe bool, op

// Cast the response and return it
if result, ok := resp.(*Link); ok {
if optionalHandler != nil {
c.handlers.AddHandler(result.Channel, optionalHandler)
}

return result, nil
}

return nil, ErrUnmarshal
}

// CreateLink sends a request to create a default link.
func (c *Client) CreateLink(key, channel, name string, subscribe bool, options ...Option) (*Link, error) {
func (c *Client) CreateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error) {
resp, err := c.request("link", &linkRequest{
Name: name,
Key: key,
Channel: formatTopic("", channel, options),
Subscribe: subscribe,
Subscribe: optionalHandler != nil,
Private: false,
})

Expand All @@ -363,6 +368,10 @@ func (c *Client) CreateLink(key, channel, name string, subscribe bool, options .

// Cast the response and return it
if result, ok := resp.(*Link); ok {
if optionalHandler != nil {
c.handlers.AddHandler(result.Channel, optionalHandler)
}

return result, nil
}
return nil, ErrUnmarshal
Expand Down
4 changes: 3 additions & 1 deletion v2/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func clientB(t *testing.T) {

// Ask to create a private link
fmt.Println("[emitter] <- [B] creating a private link")
link, err := c.CreatePrivateLink(key, "sdk-integration-test/", "1", true)
link, err := c.CreatePrivateLink(key, "sdk-integration-test/", "1", func(_ *Client, msg Message) {
fmt.Printf("[emitter] -> [B] received from private link: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
})
assert.NoError(t, err)
assert.NotNil(t, link)

Expand Down
4 changes: 3 additions & 1 deletion v2/sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func clientB() {

// Ask to create a private link
fmt.Println("[emitter] <- [B] creating a private link")
link, _ := c.CreatePrivateLink(key, "sdk-integration-test/", "1", true)
link, _ := c.CreatePrivateLink(key, "sdk-integration-test/", "1", func(_ *emitter.Client, msg emitter.Message) {
fmt.Printf("[emitter] -> [B] received from private link: '%s' topic: '%s'\n", msg.Payload(), msg.Topic())
})
fmt.Println("[emitter] -> [B] received link " + link.Channel)

// Publish to the private link
Expand Down

0 comments on commit 55ee1e2

Please sign in to comment.