-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Command methods + Tests to Client: CreateCommand GetCommand ListCommands UpdateCommand DeleteCommand
- Loading branch information
Showing
4 changed files
with
207 additions
and
7 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 |
---|---|---|
@@ -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 | ||
} |
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,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") | ||
} |
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