diff --git a/authentication.go b/authentication.go index 5412356..af41341 100644 --- a/authentication.go +++ b/authentication.go @@ -15,7 +15,7 @@ type LoginPayload struct { } type LoginResponse struct { - Status string `json:"status"` + ErrStatus Data DataLogin `json:"data"` Message string `json:"message,omitempty"` } @@ -107,13 +107,14 @@ type Preferences struct { } type LogoutResponse struct { - Status string `json:"status"` - Data struct { + ErrStatus + Data struct { Message string `json:"message"` } `json:"data"` } type MeResponse struct { + ErrStatus ID string `json:"_id"` Services Services `json:"services"` Emails []Email `json:"emails"` @@ -132,7 +133,6 @@ type MeResponse struct { Language string `json:"language"` Email string `json:"email"` AvatarURL string `json:"avatarUrl"` - Success bool `json:"success"` } func (c *Client) Login(login *LoginPayload) (*LoginResponse, error) { diff --git a/autotranslate.go b/autotranslate.go index 482f0f3..64fc1f1 100644 --- a/autotranslate.go +++ b/autotranslate.go @@ -6,8 +6,8 @@ import ( ) type SupportedLanguageResp struct { + ErrStatus Languages []language `json:"languages"` - Success bool `json:"success"` } type language struct { diff --git a/channels.go b/channels.go index b356fec..364eb37 100644 --- a/channels.go +++ b/channels.go @@ -14,8 +14,8 @@ type AddAllRequest struct { } type AddAllResponse struct { + ErrStatus Channel Channel `json:"channel"` - Success bool `json:"success"` } type Channel struct { @@ -39,6 +39,7 @@ type ChannelCountersRequest struct { } type ChannelCountersResponse struct { + ErrStatus Joined bool `json:"joined"` Members int `json:"members"` Unreads int `json:"unreads"` @@ -46,7 +47,6 @@ type ChannelCountersResponse struct { Msgs int `json:"msgs"` Latest time.Time `json:"latest"` UserMentions int `json:"userMentions"` - Success bool `json:"success"` } type CreateChannelRequest struct { @@ -56,8 +56,8 @@ type CreateChannelRequest struct { } type CreateChannelResponse struct { + ErrStatus Channel Channel `json:"channel"` - Success bool `json:"success"` } type SimpleChannelRequest struct { @@ -76,8 +76,8 @@ type ChannelHistoryRequest struct { } type ChannelInfoResponse struct { + ErrStatus Channel ChannelInfo `json:"channel"` - Success bool `json:"success"` } type ChannelInfo struct { @@ -105,6 +105,7 @@ type InviteChannelRequest struct { } type InviteChannelResponse struct { + ErrStatus Channel struct { ID string `json:"_id"` Ts time.Time `json:"ts"` @@ -115,15 +116,14 @@ type InviteChannelResponse struct { UpdatedAt time.Time `json:"_updatedAt"` Lm time.Time `json:"lm"` } `json:"channel"` - Success bool `json:"success"` } type ChannelListResponse struct { + ErrStatus Channels []ChannelList `json:"channels"` Offset int `json:"offset"` Count int `json:"count"` Total int `json:"total"` - Success bool `json:"success"` } type ChannelList struct { @@ -140,18 +140,18 @@ type ChannelList struct { } type ChannelMembersResponse struct { + ErrStatus Members []Member `json:"members"` Count int `json:"count"` Offset int `json:"offset"` Total int `json:"total"` - Success bool `json:"success"` } type Member struct { + ErrStatus ID string `json:"_id"` Username string `json:"username"` Name string `json:"name"` - Status string `json:"status"` } type RenameChannelRequest struct { @@ -160,8 +160,8 @@ type RenameChannelRequest struct { } type RenameChannelResponse struct { + ErrStatus Channel ChannelList `json:"channel"` - Success bool `json:"success"` } type SetAnnouncementRequest struct { @@ -170,8 +170,8 @@ type SetAnnouncementRequest struct { } type SetAnnouncementResponse struct { + ErrStatus Announcement string `json:"announcement"` - Success bool `json:"success"` } type SetDescriptionRequest struct { @@ -180,8 +180,8 @@ type SetDescriptionRequest struct { } type SetDescriptionResponse struct { + ErrStatus Description string `json:"description"` - Success bool `json:"success"` } type SetTopicRequest struct { @@ -190,11 +190,11 @@ type SetTopicRequest struct { } type SetTopicResponse struct { - Topic string `json:"topic"` - Success bool `json:"success"` + ErrStatus + Topic string `json:"topic"` } -// Adds all of the users on the server to a channel. +// AddAllToChannel adds all of the users on the server to a channel. func (c *Client) AddAllToChannel(params *AddAllRequest) (*AddAllResponse, error) { opt, _ := json.Marshal(params) @@ -215,7 +215,7 @@ func (c *Client) AddAllToChannel(params *AddAllRequest) (*AddAllResponse, error) return &res, nil } -// Archives a channel. +// ArchiveChannel archives a channel. func (c *Client) ArchiveChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -236,7 +236,7 @@ func (c *Client) ArchiveChannel(param *SimpleChannelId) (*SimpleSuccessResponse, return &res, nil } -// Removes the channel from the user's list of channels. +// CloseChannel removes the channel from the user's list of channels. func (c *Client) CloseChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -257,7 +257,7 @@ func (c *Client) CloseChannel(param *SimpleChannelId) (*SimpleSuccessResponse, e return &res, nil } -// Gets channel counters. +// ChannelCounters gets channel counters. func (c *Client) ChannelCounters(param *ChannelCountersRequest) (*ChannelCountersResponse, error) { req, err := http.NewRequest("GET", @@ -290,7 +290,7 @@ func (c *Client) ChannelCounters(param *ChannelCountersRequest) (*ChannelCounter return &res, nil } -// Creates a new channel. +// CreateChannel creates a new channel. func (c *Client) CreateChannel(param *CreateChannelRequest) (*CreateChannelResponse, error) { opt, _ := json.Marshal(param) @@ -311,7 +311,7 @@ func (c *Client) CreateChannel(param *CreateChannelRequest) (*CreateChannelRespo return &res, nil } -// Delete channel. +// DeleteChannel deletes a channel. func (c *Client) DeleteChannel(param *SimpleChannelRequest) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -332,7 +332,7 @@ func (c *Client) DeleteChannel(param *SimpleChannelRequest) (*SimpleSuccessRespo return &res, nil } -// Get channel info. +// ChannelInfo gets channel info. func (c *Client) ChannelInfo(param *SimpleChannelRequest) (*ChannelInfoResponse, error) { req, err := http.NewRequest("GET", @@ -365,7 +365,7 @@ func (c *Client) ChannelInfo(param *SimpleChannelRequest) (*ChannelInfoResponse, return &res, nil } -// Adds a user to the channel. +// ChannelInvite adds a user to the channel. func (c *Client) ChannelInvite(param *InviteChannelRequest) (*InviteChannelResponse, error) { opt, _ := json.Marshal(param) @@ -386,7 +386,7 @@ func (c *Client) ChannelInvite(param *InviteChannelRequest) (*InviteChannelRespo return &res, nil } -// Kick a user from the channel. +// ChannelKick kicks a user from the channel. func (c *Client) ChannelKick(param *InviteChannelRequest) (*InviteChannelResponse, error) { opt, _ := json.Marshal(param) @@ -407,7 +407,7 @@ func (c *Client) ChannelKick(param *InviteChannelRequest) (*InviteChannelRespons return &res, nil } -// Get channels list +// ChannelList lists all the channels on the server. func (c *Client) ChannelList() (*ChannelListResponse, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/channels.list", c.baseURL, c.apiVersion), @@ -426,7 +426,7 @@ func (c *Client) ChannelList() (*ChannelListResponse, error) { return &res, nil } -// Gets channel members +// ChannelMembers gets channel members func (c *Client) ChannelMembers(param *SimpleChannelRequest) (*ChannelMembersResponse, error) { req, err := http.NewRequest("GET", @@ -459,7 +459,7 @@ func (c *Client) ChannelMembers(param *SimpleChannelRequest) (*ChannelMembersRes return &res, nil } -// Adds the channel back to the user's list of channels. +// OpenChannel adds the channel back to the user's list of channels. func (c *Client) OpenChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -480,7 +480,7 @@ func (c *Client) OpenChannel(param *SimpleChannelId) (*SimpleSuccessResponse, er return &res, nil } -// Changes a channel's name. +// RenameChannel changes a channel's name. func (c *Client) RenameChannel(param *RenameChannelRequest) (*RenameChannelResponse, error) { opt, _ := json.Marshal(param) @@ -501,7 +501,7 @@ func (c *Client) RenameChannel(param *RenameChannelRequest) (*RenameChannelRespo return &res, nil } -// Sets the announcement for the channel. +// SetAnnouncementChannel sets the announcement for the channel. func (c *Client) SetAnnouncementChannel(param *SetAnnouncementRequest) (*SetAnnouncementResponse, error) { opt, _ := json.Marshal(param) @@ -522,7 +522,7 @@ func (c *Client) SetAnnouncementChannel(param *SetAnnouncementRequest) (*SetAnno return &res, nil } -// Sets the Description for the channel. +// SetDescriptionChannel sets the description for the channel. func (c *Client) SetDescriptionChannel(param *SetDescriptionRequest) (*SetDescriptionResponse, error) { opt, _ := json.Marshal(param) @@ -543,7 +543,7 @@ func (c *Client) SetDescriptionChannel(param *SetDescriptionRequest) (*SetDescri return &res, nil } -// Sets the topic for the channel. +// SetTopicChannel sets the topic for the channel. func (c *Client) SetTopicChannel(param *SetTopicRequest) (*SetTopicResponse, error) { opt, _ := json.Marshal(param) @@ -564,7 +564,7 @@ func (c *Client) SetTopicChannel(param *SetTopicRequest) (*SetTopicResponse, err return &res, nil } -// Unarchive a channel. +// UnarchiveChannel unarchives a channel. func (c *Client) UnarchiveChannel(param *SimpleChannelId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) diff --git a/chat.go b/chat.go index fc655db..a4c5796 100644 --- a/chat.go +++ b/chat.go @@ -45,12 +45,10 @@ type AttachField struct { } type RespPostMessage struct { - Ts int64 `json:"ts"` - Channel string `json:"channel"` - Message RespMessageData `json:"message"` - Success bool `json:"success"` - Error string `json:"error,omitempty"` - ErrorType string `json:"errorType,omitempty"` + ErrStatus + Ts int64 `json:"ts"` + Channel string `json:"channel"` + Message RespMessageData `json:"message"` } type RespMessageData struct { @@ -75,8 +73,8 @@ type SingleMessageId struct { } type GetMessageResponse struct { + ErrStatus Message MessageResp `json:"message"` - Success bool `json:"success"` } type MessageResp struct { @@ -97,9 +95,9 @@ type DeleteMessageRequest struct { } type DeleteMessageResponse struct { - ID string `json:"_id"` - Ts int64 `json:"ts"` - Success bool `json:"success"` + ErrStatus + ID string `json:"_id"` + Ts int64 `json:"ts"` } type GetPinnedMsgRequest struct { @@ -109,11 +107,11 @@ type GetPinnedMsgRequest struct { } type GetPinnedMsgResponse struct { + ErrStatus Messages []PinnedMessage `json:"messages"` Count int `json:"count"` Offset int `json:"offset"` Total int `json:"total"` - Success bool `json:"success"` } type PinnedMessage struct { @@ -138,6 +136,7 @@ type PinnedMessage struct { } type PinMessageResponse struct { + ErrStatus Message struct { T string `json:"t"` Rid string `json:"rid"` @@ -157,10 +156,9 @@ type PinMessageResponse struct { UpdatedAt time.Time `json:"_updatedAt"` ID string `json:"_id"` } `json:"message"` - Success bool `json:"success"` } -// Posts a new chat message. +// PostMessage posts a new chat message. func (c *Client) PostMessage(msg *Message) (*RespPostMessage, error) { opt, _ := json.Marshal(msg) @@ -182,7 +180,7 @@ func (c *Client) PostMessage(msg *Message) (*RespPostMessage, error) { return &res, nil } -// Retrieves a single chat message by the provided id. +// GetMessage retrieves a single chat message by the provided id. // Callee must have permission to access the room where the message resides. func (c *Client) GetMessage(param *SingleMessageId) (*GetMessageResponse, error) { req, err := http.NewRequest("GET", @@ -212,7 +210,7 @@ func (c *Client) GetMessage(param *SingleMessageId) (*GetMessageResponse, error) return &res, nil } -// Chat Message Delete +// DeleteMessage deletes a chat message by ID. func (c *Client) DeleteMessage(param *DeleteMessageRequest) (*DeleteMessageResponse, error) { opt, _ := json.Marshal(param) @@ -233,7 +231,7 @@ func (c *Client) DeleteMessage(param *DeleteMessageRequest) (*DeleteMessageRespo return &res, nil } -// Callee must have permission to access the room where the message resides. +// GetPinnedMessages retrieves pinned messages from a room. Callee must have permission to access the room where the message resides. func (c *Client) GetPinnedMessages(param *GetPinnedMsgRequest) (*GetPinnedMsgResponse, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/chat.getPinnedMessages", c.baseURL, c.apiVersion), @@ -268,7 +266,7 @@ func (c *Client) GetPinnedMessages(param *GetPinnedMsgRequest) (*GetPinnedMsgRes return &res, nil } -// Pins a chat message to the message's channel. +// PinMessage pins a chat message to the message's channel. func (c *Client) PinMessage(param *SingleMessageId) (*PinMessageResponse, error) { opt, _ := json.Marshal(param) @@ -289,7 +287,7 @@ func (c *Client) PinMessage(param *SingleMessageId) (*PinMessageResponse, error) return &res, nil } -// Unpins a chat message to the message's channel. +// UnpinMessage unpins a chat message from the message's channel. func (c *Client) UnpinMessage(param *SingleMessageId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..54b4ab5 --- /dev/null +++ b/errors.go @@ -0,0 +1,64 @@ +package gorocket + +import ( + "fmt" + "strings" +) + +type Response interface { + OK(statusCode int, debug bool) error +} + +// ErrStatus handles error responses. +type ErrStatus struct { + Success bool `json:"success"` + ErrorMsg string `json:"error,omitempty"` + ErrorType string `json:"errorType,omitempty"` + + Status string `json:"status,omitempty"` + Message string `json:"message,omitempty"` + Details any `json:"details,omitempty"` + + statusCode int + debug bool +} + +func (s ErrStatus) Error() string { + var sb strings.Builder + fmt.Fprintf(&sb, "rocket.chat response error (status code %d): success:%t", s.statusCode, s.Success) + + if len(s.ErrorMsg) > 0 { + fmt.Fprintf(&sb, ", error:%q", s.ErrorMsg) + } + if len(s.ErrorType) > 0 { + fmt.Fprintf(&sb, ", errorType:%q", s.ErrorType) + } + if len(s.Status) > 0 { + fmt.Fprintf(&sb, ", status:%q", s.Status) + } + if len(s.Message) > 0 { + fmt.Fprintf(&sb, ", message:%q", s.Message) + } + + if s.debug && s.Details != nil { + fmt.Fprintf(&sb, ", details:%v", s.Details) + } + + return sb.String() +} + +func (s ErrStatus) OK(statusCode int, debug bool) error { + s.statusCode = statusCode + s.debug = debug + + // Only assume the request was successful if success is true, and/or status is success. + if s.Success { + return nil + } + + if s.Status == "success" { + return nil + } + + return s +} diff --git a/gorocket.go b/gorocket.go index 6b22238..0550521 100644 --- a/gorocket.go +++ b/gorocket.go @@ -14,6 +14,7 @@ type Client struct { userID string xToken string apiVersion string + debug bool HTTPClient *http.Client timeout time.Duration @@ -77,7 +78,15 @@ func WithXToken(xtoken string) Option { } } -func (c *Client) sendRequest(req *http.Request, v interface{}) error { +// WithDebug will cause API error response "details" to be added to error values, which may contain sensitive data such +// as the input values provided to the failed request. These may contain sensitive data you might not want to log. +func WithDebug() Option { + return func(c *Client) { + c.debug = true + } +} + +func (c *Client) sendRequest(req *http.Request, v Response) error { req.Header.Set("Accept", "application/json; charset=utf-8") req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Add("X-Auth-Token", c.xToken) @@ -105,7 +114,7 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error { return err } - return nil + return resp.OK(res.StatusCode, c.debug) } func (c *Client) Count(val int) *Client { diff --git a/groups.go b/groups.go index eccca20..f13265d 100644 --- a/groups.go +++ b/groups.go @@ -19,6 +19,7 @@ type GroupCountersRequest struct { } type GroupCountersResponse struct { + ErrStatus Joined bool `json:"joined"` Members int `json:"members"` Unreads int `json:"unreads"` @@ -26,7 +27,6 @@ type GroupCountersResponse struct { Msgs int `json:"msgs"` Latest time.Time `json:"latest"` UserMentions int `json:"userMentions"` - Success bool `json:"success"` } type CreateGroupRequest struct { @@ -36,8 +36,8 @@ type CreateGroupRequest struct { } type CreateGroupResponse struct { - Group Channel `json:"group"` - Success bool `json:"success"` + ErrStatus + Group Channel `json:"group"` } type SimpleGroupRequest struct { @@ -46,8 +46,8 @@ type SimpleGroupRequest struct { } type GroupInfoResponse struct { - Group groupInfo `json:"group"` - Success bool `json:"success"` + ErrStatus + Group groupInfo `json:"group"` } type groupInfo struct { @@ -75,6 +75,7 @@ type InviteGroupRequest struct { } type InviteGroupResponse struct { + ErrStatus Group struct { ID string `json:"_id"` Ts time.Time `json:"ts"` @@ -85,15 +86,14 @@ type InviteGroupResponse struct { UpdatedAt time.Time `json:"_updatedAt"` Lm time.Time `json:"lm"` } `json:"group"` - Success bool `json:"success"` } type GroupListResponse struct { - Groups []groupList `json:"groups"` - Offset int `json:"offset"` - Count int `json:"count"` - Total int `json:"total"` - Success bool `json:"success"` + ErrStatus + Groups []groupList `json:"groups"` + Offset int `json:"offset"` + Count int `json:"count"` + Total int `json:"total"` } type groupList struct { @@ -110,11 +110,11 @@ type groupList struct { } type GroupMembersResponse struct { + ErrStatus Members []Member `json:"members"` Count int `json:"count"` Offset int `json:"offset"` Total int `json:"total"` - Success bool `json:"success"` } type GroupMessage struct { @@ -131,11 +131,11 @@ type GroupMessage struct { } type GroupMessagesResponse struct { + ErrStatus Messages []GroupMessage `json:"messages"` Count int `json:"count"` Offset int `json:"offset"` Total int `json:"total"` - Success bool `json:"success"` } type RenameGroupRequest struct { @@ -144,8 +144,8 @@ type RenameGroupRequest struct { } type RenameGroupResponse struct { - Group groupList `json:"group"` - Success bool `json:"success"` + ErrStatus + Group groupList `json:"group"` } type AddGroupPermissionRequest struct { @@ -153,7 +153,7 @@ type AddGroupPermissionRequest struct { UserId string `json:"userId"` } -// Archives a group. +// ArchiveGroup archives a group. func (c *Client) ArchiveGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -174,7 +174,7 @@ func (c *Client) ArchiveGroup(param *SimpleGroupId) (*SimpleSuccessResponse, err return &res, nil } -// Removes the group from the user's list of groups. +// CloseGroup removes the group from the user's list of groups. func (c *Client) CloseGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -195,7 +195,7 @@ func (c *Client) CloseGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error return &res, nil } -// Gets group counters. +// GroupCounters gets group counters. func (c *Client) GroupCounters(param *GroupCountersRequest) (*GroupCountersResponse, error) { req, err := http.NewRequest("GET", @@ -228,7 +228,7 @@ func (c *Client) GroupCounters(param *GroupCountersRequest) (*GroupCountersRespo return &res, nil } -// Creates a new group. +// CreateGroup creates a new group. func (c *Client) CreateGroup(param *CreateGroupRequest) (*CreateGroupResponse, error) { opt, _ := json.Marshal(param) @@ -249,7 +249,7 @@ func (c *Client) CreateGroup(param *CreateGroupRequest) (*CreateGroupResponse, e return &res, nil } -// Delete group. +// DeleteGroup deletes a group. func (c *Client) DeleteGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -270,7 +270,7 @@ func (c *Client) DeleteGroup(param *SimpleGroupId) (*SimpleSuccessResponse, erro return &res, nil } -// Get group info. +// GroupInfo gets group info. func (c *Client) GroupInfo(param *SimpleGroupRequest) (*GroupInfoResponse, error) { req, err := http.NewRequest("GET", @@ -303,7 +303,7 @@ func (c *Client) GroupInfo(param *SimpleGroupRequest) (*GroupInfoResponse, error return &res, nil } -// Adds a user to the group. +// GroupInvite adds a user to the group. func (c *Client) GroupInvite(param *InviteGroupRequest) (*InviteGroupResponse, error) { opt, _ := json.Marshal(param) @@ -324,7 +324,7 @@ func (c *Client) GroupInvite(param *InviteGroupRequest) (*InviteGroupResponse, e return &res, nil } -// Kick a user from the group. +// GroupKick kicks a user from the group. func (c *Client) GroupKick(param *InviteGroupRequest) (*InviteGroupResponse, error) { opt, _ := json.Marshal(param) @@ -345,7 +345,7 @@ func (c *Client) GroupKick(param *InviteGroupRequest) (*InviteGroupResponse, err return &res, nil } -// Get groups list +// GroupList get groups list func (c *Client) GroupList() (*GroupListResponse, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/groups.list", c.baseURL, c.apiVersion), @@ -364,7 +364,7 @@ func (c *Client) GroupList() (*GroupListResponse, error) { return &res, nil } -// Gets group members +// GroupMembers gets group members func (c *Client) GroupMembers(param *SimpleGroupRequest) (*GroupMembersResponse, error) { req, err := http.NewRequest("GET", @@ -397,7 +397,7 @@ func (c *Client) GroupMembers(param *SimpleGroupRequest) (*GroupMembersResponse, return &res, nil } -// Gets group messages +// GroupMessages gets group messages func (c *Client) GroupMessages(param *SimpleGroupRequest) (*GroupMessagesResponse, error) { req, err := http.NewRequest("GET", @@ -430,7 +430,7 @@ func (c *Client) GroupMessages(param *SimpleGroupRequest) (*GroupMessagesRespons return &res, nil } -// Adds the group back to the user's list of groups. +// OpenGroup adds the group back to the user's list of groups. func (c *Client) OpenGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) @@ -451,7 +451,7 @@ func (c *Client) OpenGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) return &res, nil } -// Changes a group's name. +// RenameGroup changes a group's name. func (c *Client) RenameGroup(param *RenameGroupRequest) (*RenameGroupResponse, error) { opt, _ := json.Marshal(param) @@ -472,7 +472,7 @@ func (c *Client) RenameGroup(param *RenameGroupRequest) (*RenameGroupResponse, e return &res, nil } -// Add leader for the group. +// AddLeaderGroup adds leader for the group. func (c *Client) AddLeaderGroup(param *AddGroupPermissionRequest) (*SimpleSuccessResponse, error) { if param.UserId == "" && param.RoomId == "" { return nil, fmt.Errorf("False parameters") @@ -497,7 +497,7 @@ func (c *Client) AddLeaderGroup(param *AddGroupPermissionRequest) (*SimpleSucces return &res, nil } -// Add owner for the group. +// AddOwnerGroup adds owner for the group. func (c *Client) AddOwnerGroup(param *AddGroupPermissionRequest) (*SimpleSuccessResponse, error) { if param.UserId == "" && param.RoomId == "" { return nil, fmt.Errorf("False parameters") @@ -522,7 +522,7 @@ func (c *Client) AddOwnerGroup(param *AddGroupPermissionRequest) (*SimpleSuccess return &res, nil } -// Sets the announcement for the group. +// SetAnnouncementGroup sets the announcement for the group. func (c *Client) SetAnnouncementGroup(param *SetAnnouncementRequest) (*SetAnnouncementResponse, error) { opt, _ := json.Marshal(param) @@ -543,7 +543,7 @@ func (c *Client) SetAnnouncementGroup(param *SetAnnouncementRequest) (*SetAnnoun return &res, nil } -// Sets the Description for the group. +// SetDescriptionGroup sets the description for the group. func (c *Client) SetDescriptionGroup(param *SetDescriptionRequest) (*SetDescriptionResponse, error) { opt, _ := json.Marshal(param) @@ -564,7 +564,7 @@ func (c *Client) SetDescriptionGroup(param *SetDescriptionRequest) (*SetDescript return &res, nil } -// Sets the topic for the group. +// SetTopicGroup sets the topic for the group. func (c *Client) SetTopicGroup(param *SetTopicRequest) (*SetTopicResponse, error) { opt, _ := json.Marshal(param) @@ -585,7 +585,7 @@ func (c *Client) SetTopicGroup(param *SetTopicRequest) (*SetTopicResponse, error return &res, nil } -// Unarchive a group. +// UnarchiveGroup unarchives a group. func (c *Client) UnarchiveGroup(param *SimpleGroupId) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(param) diff --git a/hooks.go b/hooks.go index 0d1b549..d09bfe8 100644 --- a/hooks.go +++ b/hooks.go @@ -22,7 +22,7 @@ type HookAttachment struct { } type HookResponse struct { - Success bool `json:"success"` + ErrStatus } func (c *Client) Hooks(msg *HookMessage, token string) (*HookResponse, error) { diff --git a/info.go b/info.go index 5faee4c..f911ce0 100644 --- a/info.go +++ b/info.go @@ -7,13 +7,13 @@ import ( ) type RespInfo struct { + ErrStatus Info struct { Version string `json:"version"` Build Build `json:"build"` Commit Commit `json:"commit"` MarketplaceAPIVersion string `json:"marketplaceApiVersion"` } `json:"info"` - Success bool `json:"success"` } type Build struct { @@ -37,11 +37,11 @@ type Commit struct { } type RespDirectory struct { - Result []Result `json:"result"` - Count int `json:"count"` - Offset int `json:"offset"` - Total int `json:"total"` - Success bool `json:"success"` + ErrStatus + Result []Result `json:"result"` + Count int `json:"count"` + Offset int `json:"offset"` + Total int `json:"total"` } type Result struct { @@ -76,10 +76,9 @@ type U struct { } type RespSpotlight struct { - Users []UsersInfo `json:"users"` - Rooms []RoomsInfo `json:"rooms"` - Success bool `json:"success"` - Error string `json:"error,omitempty"` + ErrStatus + Users []UsersInfo `json:"users"` + Rooms []RoomsInfo `json:"rooms"` } type UsersInfo struct { @@ -98,6 +97,7 @@ type RoomsInfo struct { } type RespStatistics struct { + ErrStatus ID string `json:"_id"` Wizard Wizard `json:"wizard"` UniqueID string `json:"uniqueId"` @@ -154,7 +154,6 @@ type RespStatistics struct { PushQueue int `json:"pushQueue"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"_updatedAt"` - Success bool `json:"success"` } type Wizard struct { @@ -234,6 +233,7 @@ type Deploy struct { } type RespStatisticsList struct { + ErrStatus Statistics []struct { ID string `json:"_id"` Wizard Wizard `json:"wizard"` @@ -292,10 +292,9 @@ type RespStatisticsList struct { LastMessageSentAt time.Time `json:"lastMessageSentAt,omitempty"` LastSeenSubscription time.Time `json:"lastSeenSubscription,omitempty"` } `json:"statistics"` - Count int `json:"count"` - Offset int `json:"offset"` - Total int `json:"total"` - Success bool `json:"success"` + Count int `json:"count"` + Offset int `json:"offset"` + Total int `json:"total"` } func (c *Client) Info() (*RespInfo, error) { diff --git a/users.go b/users.go index 01323f7..85605a9 100644 --- a/users.go +++ b/users.go @@ -10,9 +10,9 @@ import ( ) type UsersPresenceResponse struct { - Users []user `json:"users"` - Full bool `json:"full"` - Success bool `json:"success"` + ErrStatus + Users []user `json:"users"` + Full bool `json:"full"` } type user struct { @@ -39,8 +39,8 @@ type NewUser struct { } type UserCreateResponse struct { - User userCreateInfo `json:"user"` - Success bool `json:"success"` + ErrStatus + User userCreateInfo `json:"user"` } type userCreateInfo struct { @@ -70,7 +70,7 @@ type UsersDelete struct { } type SimpleSuccessResponse struct { - Success bool `json:"success"` + ErrStatus } type SimpleUserRequest struct { @@ -79,11 +79,11 @@ type SimpleUserRequest struct { } type CreateTokenResponse struct { + ErrStatus Data struct { UserID string `json:"userId"` AuthToken string `json:"authToken"` } `json:"data"` - Success bool `json:"success"` } type DeactivateRequest struct { @@ -92,8 +92,8 @@ type DeactivateRequest struct { } type DeactivateResponse struct { - Count int `json:"count"` - Success bool `json:"success"` + ErrStatus + Count int `json:"count"` } type GetNewToken struct { @@ -102,20 +102,20 @@ type GetNewToken struct { } type NewTokenResponse struct { - Token string `json:"token"` - Success bool `json:"success"` + ErrStatus + Token string `json:"token"` } type GetStatusResponse struct { + ErrStatus Message string `json:"message"` ConnectionStatus string `json:"connectionStatus"` Status string `json:"status"` - Success bool `json:"success"` } type UsersInfoResponse struct { - User singleUserInfo `json:"user"` - Success bool `json:"success"` + ErrStatus + User singleUserInfo `json:"user"` } type singleUserInfo struct { @@ -160,8 +160,8 @@ type UserUpdateData struct { } type UserUpdateResponse struct { - User userUpdateInfo `json:"user"` - Success bool `json:"success"` + ErrStatus + User userUpdateInfo `json:"user"` } type userUpdateInfo struct { @@ -182,7 +182,7 @@ type userUpdateInfo struct { Name string `json:"name"` } -// Gets all connected users presence +// UsersPresence gets all connected users presence func (c *Client) UsersPresence(query string) (*UsersPresenceResponse, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/users.presence?from=%s", c.baseURL, c.apiVersion, query), nil) @@ -198,7 +198,7 @@ func (c *Client) UsersPresence(query string) (*UsersPresenceResponse, error) { return &res, nil } -// Create a new user. Requires create-user permission. +// UsersCreate creates a new user. Requires create-user permission. func (c *Client) UsersCreate(user *NewUser) (*UserCreateResponse, error) { opt, _ := json.Marshal(user) @@ -219,7 +219,7 @@ func (c *Client) UsersCreate(user *NewUser) (*UserCreateResponse, error) { return &res, nil } -// Deletes an existing user. Requires delete-user permission. +// UsersDelete deletes an existing user. Requires delete-user permission. func (c *Client) UsersDelete(user *UsersDelete) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(user) @@ -240,7 +240,7 @@ func (c *Client) UsersDelete(user *UsersDelete) (*SimpleSuccessResponse, error) return &res, nil } -// Create a user authentication token. +// UsersCreateToken creates a user authentication token. Requires rocket.chat env var CREATE_TOKENS_FOR_USERS=true. func (c *Client) UsersCreateToken(user *SimpleUserRequest) (*CreateTokenResponse, error) { opt, _ := json.Marshal(user) @@ -261,7 +261,7 @@ func (c *Client) UsersCreateToken(user *SimpleUserRequest) (*CreateTokenResponse return &res, nil } -// Deactivate Idle users. Requires edit-other-user-active-status permission. +// UsersDeactivateIdle deactivates idle users. Requires edit-other-user-active-status permission. func (c *Client) UsersDeactivateIdle(params *DeactivateRequest) (*DeactivateResponse, error) { opt, _ := json.Marshal(params) @@ -282,7 +282,7 @@ func (c *Client) UsersDeactivateIdle(params *DeactivateRequest) (*DeactivateResp return &res, nil } -// Deletes your own user. Requires Allow Users to Delete Own Account enabled. Accessible from Administration -> Accounts. +// UsersDeleteOwnAccount deletes your own user. Requires Allow Users to Delete Own Account enabled. Accessible from Administration -> Accounts. func (c *Client) UsersDeleteOwnAccount(pass string) (*SimpleSuccessResponse, error) { param := struct { @@ -310,7 +310,7 @@ func (c *Client) UsersDeleteOwnAccount(pass string) (*SimpleSuccessResponse, err return &res, nil } -// Send email to reset your password. +// UsersForgotPassword sends an email to reset your password. func (c *Client) UsersForgotPassword(email string) (*SimpleSuccessResponse, error) { param := struct { email string `json:"email"` @@ -337,7 +337,7 @@ func (c *Client) UsersForgotPassword(email string) (*SimpleSuccessResponse, erro return &res, nil } -// Generate Personal Access Token. Requires create-personal-access-tokens permission. +// UsersGeneratePersonalAccessToken generates a Personal Access Token. Requires create-personal-access-tokens permission. func (c *Client) UsersGeneratePersonalAccessToken(params *GetNewToken) (*NewTokenResponse, error) { opt, _ := json.Marshal(params) @@ -358,7 +358,7 @@ func (c *Client) UsersGeneratePersonalAccessToken(params *GetNewToken) (*NewToke return &res, nil } -// Gets a user's Status if the query string userId or username is provided, otherwise it gets the callee's. +// UsersGetStatus gets a user's Status if the query string userId or username is provided, otherwise it gets the callee's. func (c *Client) UsersGetStatus(user *SimpleUserRequest) (*GetStatusResponse, error) { req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/users.getStatus", c.baseURL, c.apiVersion), nil) @@ -387,7 +387,7 @@ func (c *Client) UsersGetStatus(user *SimpleUserRequest) (*GetStatusResponse, er return &res, nil } -// Retrieves information about a user, the result is only limited to what the callee has access to view. +// UsersInfo retrieves information about a user, the result is only limited to what the callee has access to view. func (c *Client) UsersInfo(user *SimpleUserRequest) (*UsersInfoResponse, error) { opt, _ := json.Marshal(user) @@ -421,7 +421,7 @@ func (c *Client) UsersInfo(user *SimpleUserRequest) (*UsersInfoResponse, error) return &res, nil } -// Register a new user. +// UsersRegister registers a new user. func (c *Client) UsersRegister(user *UserRegisterRequest) (*UsersInfoResponse, error) { opt, _ := json.Marshal(user) @@ -442,7 +442,7 @@ func (c *Client) UsersRegister(user *UserRegisterRequest) (*UsersInfoResponse, e return &res, nil } -// Sets a user Status when the status message and state is given. +// UsersSetStatus sets a user Status when the status message and state is given. func (c *Client) UsersSetStatus(status *SetStatus) (*SimpleSuccessResponse, error) { opt, _ := json.Marshal(status) @@ -463,7 +463,7 @@ func (c *Client) UsersSetStatus(status *SetStatus) (*SimpleSuccessResponse, erro return &res, nil } -// Update an existing user. +// UsersUpdate updates an existing user. func (c *Client) UsersUpdate(user *UserUpdateRequest) (*UserUpdateResponse, error) { opt, _ := json.Marshal(user)