-
Notifications
You must be signed in to change notification settings - Fork 31
/
command.go
101 lines (82 loc) · 2.58 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package stream_chat
import (
"context"
"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 `json:"command"`
Response
}
// CreateCommand registers a new custom command.
func (c *Client) CreateCommand(ctx context.Context, cmd *Command) (*CommandResponse, error) {
if cmd == nil {
return nil, errors.New("command is nil")
}
var resp CommandResponse
err := c.makeRequest(ctx, 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, nil
}
type GetCommandResponse struct {
*Command
Response
}
// GetCommand retrieves a custom command referenced by cmdName.
func (c *Client) GetCommand(ctx context.Context, cmdName string) (*GetCommandResponse, error) {
if cmdName == "" {
return nil, errors.New("command name is empty")
}
p := path.Join("commands", url.PathEscape(cmdName))
var resp GetCommandResponse
err := c.makeRequest(ctx, http.MethodGet, p, nil, nil, &resp)
return &resp, err
}
// DeleteCommand deletes a custom command referenced by cmdName.
func (c *Client) DeleteCommand(ctx context.Context, cmdName string) (*Response, error) {
if cmdName == "" {
return nil, errors.New("command name is empty")
}
p := path.Join("commands", url.PathEscape(cmdName))
var resp Response
err := c.makeRequest(ctx, http.MethodDelete, p, nil, nil, &resp)
return &resp, err
}
// CommandsResponse represents an API response containing a list of Command.
type CommandsResponse struct {
Commands []*Command
}
// ListCommands returns a list of custom commands.
func (c *Client) ListCommands(ctx context.Context) (*CommandsResponse, error) {
var resp CommandsResponse
err := c.makeRequest(ctx, http.MethodGet, "commands", nil, nil, &resp)
return &resp, err
}
// UpdateCommand updates a custom command referenced by cmdName.
func (c *Client) UpdateCommand(ctx context.Context, cmdName string, update *Command) (*CommandResponse, error) {
switch {
case cmdName == "":
return nil, errors.New("command name is empty")
case update == nil:
return nil, errors.New("update should not be nil")
}
p := path.Join("commands", url.PathEscape(cmdName))
var resp CommandResponse
err := c.makeRequest(ctx, http.MethodPut, p, nil, update, &resp)
return &resp, err
}