Skip to content

Commit

Permalink
Custom command crud (#96)
Browse files Browse the repository at this point in the history
Added Command methods + Tests to Client:

CreateCommand
GetCommand
ListCommands
UpdateCommand
DeleteCommand
  • Loading branch information
gumuz authored Sep 24, 2020
1 parent 209934f commit 43b4132
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 7 deletions.
7 changes: 0 additions & 7 deletions channel_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ type Permission struct {
Priority int `json:"priority"` // required
}

type Command struct {
Name string `json:"name"`
Description string `json:"description"`
Args string `json:"args"`
Set string `json:"set"`
}

type ChannelType struct {
ChannelConfig

Expand Down
96 changes: 96 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package stream_chat // nolint: golint
import (
"errors"
"net/http"
"net/url"
"path"
)

// Command represents a custom command.
type Command struct {
Name string `json:"name"`
Description string `json:"description"`
Args string `json:"args"`
Set string `json:"set"`
}

// commandResponse represents an API response containing one Command.
type commandResponse struct {
Command *Command
}

// commandsResponse represents an API response containing a list of Command.
type commandsResponse struct {
Commands []*Command
}

// CreateCommand registers a new custom command.
func (c *Client) CreateCommand(cmd *Command) (*Command, error) {
if cmd == nil {
return nil, errors.New("command is nil")
}

var resp commandResponse

err := c.makeRequest(http.MethodPost, "commands", nil, cmd, &resp)
if err != nil {
return nil, err
}
if resp.Command == nil {
return nil, errors.New("unexpected error: command response is nil")
}

return resp.Command, nil
}

// GetCommand retrieves a custom command referenced by cmdName.
func (c *Client) GetCommand(cmdName string) (*Command, error) {
if cmdName == "" {
return nil, errors.New("command name is empty")
}

p := path.Join("commands", url.PathEscape(cmdName))

cmd := Command{}

err := c.makeRequest(http.MethodGet, p, nil, nil, &cmd)

return &cmd, err
}

// DeleteCommand deletes a custom command referenced by cmdName.
func (c *Client) DeleteCommand(cmdName string) error {
if cmdName == "" {
return errors.New("command name is empty")
}

p := path.Join("commands", url.PathEscape(cmdName))

return c.makeRequest(http.MethodDelete, p, nil, nil, nil)
}

// ListCommands returns a list of custom commands.
func (c *Client) ListCommands() ([]*Command, error) {
var resp commandsResponse

err := c.makeRequest(http.MethodGet, "commands", nil, nil, &resp)

return resp.Commands, err
}

// UpdateCommand updates a custom command referenced by cmdName.
func (c *Client) UpdateCommand(cmdName string, options map[string]interface{}) (*Command, error) {
switch {
case cmdName == "":
return nil, errors.New("command name is empty")
case len(options) == 0:
return nil, errors.New("options are empty")
}

p := path.Join("commands", url.PathEscape(cmdName))

var resp commandResponse

err := c.makeRequest(http.MethodPut, p, nil, options, &resp)
return resp.Command, err
}
104 changes: 104 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package stream_chat // nolint: golint

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func prepareCommand(t *testing.T, c *Client) *Command {
cmd := &Command{
Name: randomString(10),
Description: "test command",
}

cmd, err := c.CreateCommand(cmd)
require.NoError(t, err, "create command")

return cmd
}

func TestClient_GetCommand(t *testing.T) {
c := initClient(t)

cmd := prepareCommand(t, c)
defer func() {
_ = c.DeleteCommand(cmd.Name)
}()

got, err := c.GetCommand(cmd.Name)
require.NoError(t, err, "get command")

assert.Equal(t, cmd.Name, got.Name)
assert.Equal(t, cmd.Description, got.Description)
}

func TestClient_ListCommands(t *testing.T) {
c := initClient(t)

cmd := prepareCommand(t, c)
defer func() {
_ = c.DeleteCommand(cmd.Name)
}()

got, err := c.ListCommands()
require.NoError(t, err, "list commands")

assert.Contains(t, got, cmd)
}

func TestClient_UpdateCommand(t *testing.T) {
c := initClient(t)

cmd := prepareCommand(t, c)
defer func() {
_ = c.DeleteCommand(cmd.Name)
}()

got, err := c.UpdateCommand(cmd.Name, map[string]interface{}{
"description": "new description",
})
require.NoError(t, err, "update command")

assert.Equal(t, cmd.Name, got.Name)
assert.Equal(t, "new description", got.Description)
}

// See https://getstream.io/chat/docs/custom_commands/ for more details.
func ExampleClient_CreateCommand() {
client := &Client{}

newCommand := &Command{
Name: "my-command",
Description: "my command",
Args: "[@username]",
Set: "custom_cmd_set",
}

_, _ = client.CreateCommand(newCommand)
}

func ExampleClient_ListCommands() {
client := &Client{}
_, _ = client.ListCommands()
}

func ExampleClient_GetCommand() {
client := &Client{}
_, _ = client.GetCommand("my-command")
}

func ExampleClient_UpdateCommand() {
client := &Client{}

_, _ = client.UpdateCommand("my-command", map[string]interface{}{
"description": "updated description",
})
}

func ExampleClient_DeleteCommand() {
client := &Client{}

_ = client.DeleteCommand("my-command")
}
7 changes: 7 additions & 0 deletions stream_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ type StreamClient interface {
ListChannelTypes() (map[string]*ChannelType, error)
UpdateChannelType(name string, options map[string]interface{}) error

// command.go
CreateCommand(cmd *Command) (*Command, error)
DeleteCommand(cmdName string) error
GetCommand(cmdName string) (*Command, error)
ListCommands() ([]*Command, error)
UpdateCommand(cmdName string, options map[string]interface{}) (*Command, error)

// client.go
CreateToken(userID string, expire time.Time) ([]byte, error)
VerifyWebhook(body []byte, signature []byte) (valid bool)
Expand Down

0 comments on commit 43b4132

Please sign in to comment.