Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement channels.invite #13

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ func (c *ChannelsArchiveCall) Do(ctx context.Context) error {
const endpoint = "channels.archive"

var res SlackResponse

if err := c.service.client.postForm(ctx, endpoint, c.Values(), &res); err != nil {
return errors.Wrapf(err, `failed to post to %s`, endpoint)
}

if !res.OK {
return errors.New(res.Error.String())
if err := genericPost(ctx, c.service.client, endpoint, c.Values(), &res); err != nil {
return err
}

return nil
Expand Down Expand Up @@ -78,17 +73,10 @@ func (c *ChannelsCreateCall) Validate(b bool) *ChannelsCreateCall {

func (c *ChannelsCreateCall) Do(ctx context.Context) error {
const endpoint = "channels.archive"

var res SlackResponse

if err := c.service.client.postForm(ctx, endpoint, c.Values(), &res); err != nil {
return errors.Wrapf(err, `failed to post to %s`, endpoint)
}

if !res.OK {
return errors.New(res.Error.String())
if err := genericPost(ctx, c.service.client, endpoint, c.Values(), &res); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -177,12 +165,8 @@ func (c *ChannelsHistoryCall) Do(ctx context.Context) (*ChannelsHistoryResponse,
*ChannelsHistoryResponse
}

if err := c.service.client.postForm(ctx, endpoint, c.Values(), &res); err != nil {
return nil, errors.Wrapf(err, `failed to post to %s`, endpoint)
}

if !res.OK {
return nil, errors.New(res.Error.String())
if err := genericPost(ctx, c.service.client, endpoint, c.Values(), &res); err != nil {
return nil, err
}

return res.ChannelsHistoryResponse, nil
Expand Down Expand Up @@ -216,7 +200,7 @@ func (c *ChannelsInfoCall) Do(ctx context.Context) (*objects.Channel, error) {
*objects.Channel `json:"channel"`
}

if err := c.service.client.postForm(ctx, endpoint, c.Values(), &res); err != nil {
if err := genericPost(ctx, c.service.client, endpoint, c.Values(), &res); err != nil {
return nil, errors.Wrapf(err, `failed to post to %s`, endpoint)
}

Expand All @@ -227,6 +211,30 @@ func (c *ChannelsInfoCall) Do(ctx context.Context) (*objects.Channel, error) {
return res.Channel, nil
}

// ChannelsInviteCall is created via Channels.Invite() method
type ChannelsInviteCall struct {
service *ChannelsService
channel string
user string
}

func (s *ChannelsService) Invite(channelID, userID string) *ChannelsInviteCall {
return &ChannelsInviteCall{
service: s,
channel: channelID,
user: userID,
}
}

func (c *ChannelsInviteCall) Values() url.Values {
v := url.Values{
"token": {c.service.token},
"channel": {c.channel},
"user": {c.user},
}
return v
}

// ChannelsListCall is created via Channels.List() method
type ChannelsListCall struct {
service *ChannelsService
Expand Down Expand Up @@ -262,13 +270,8 @@ func (c *ChannelsListCall) Do(ctx context.Context) (objects.ChannelList, error)
objects.ChannelList `json:"channels"`
}

if err := c.service.client.postForm(ctx, endpoint, c.Values(), &res); err != nil {
return nil, errors.Wrapf(err, `failed to post to %s`, endpoint)
}

if !res.OK {
return nil, errors.New(res.Error.String())
if err := genericPost(ctx, c.service.client, endpoint, c.Values(), &res); err != nil {
return nil, err
}

return res.ChannelList, nil
}
24 changes: 24 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,27 @@ func (c *httpClient) get(octx context.Context, path string, f url.Values, data i
defer res.Body.Close()
return c.parseResponse(path, res.Body, data)
}

func (r SlackResponse) err() error {
return errors.New(r.Error.String())
}

func (r SlackResponse) ok() bool {
return r.OK
}

type genericResponse interface {
ok() bool
err() error
}

func genericPost(ctx context.Context, client *httpClient, endpoint string, v url.Values, res genericResponse) error {
if err := client.postForm(ctx, endpoint, v, res); err != nil {
return errors.Wrapf(err, `failed to post to %s`, endpoint)
}

if !res.ok() {
return res.err()
}
return nil
}