5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-12-27 12:25:40 +00:00
matterbridge/vendor/github.com/nlopes/slack/slack.go

132 lines
3.0 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
2017-07-16 12:29:46 +00:00
"context"
2018-01-08 21:41:38 +00:00
"fmt"
2016-09-05 14:34:37 +00:00
"log"
2018-08-09 22:38:19 +00:00
"net/http"
2016-09-05 14:34:37 +00:00
"net/url"
"os"
)
// APIURL added as a var so that we can change this for testing purposes
2018-12-01 18:55:35 +00:00
var APIURL = "https://slack.com/api/"
2016-09-05 14:34:37 +00:00
2018-12-01 18:55:35 +00:00
// WEBAPIURLFormat ...
const WEBAPIURLFormat = "https://%s.slack.com/api/users.admin.%s?t=%d"
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
// httpClient defines the minimal interface needed for an http.Client to be implemented.
type httpClient interface {
2018-08-09 22:38:19 +00:00
Do(*http.Request) (*http.Response, error)
}
// ResponseMetadata holds pagination metadata
type ResponseMetadata struct {
Cursor string `json:"next_cursor"`
}
func (t *ResponseMetadata) initialize() *ResponseMetadata {
if t != nil {
return t
}
return &ResponseMetadata{}
2016-09-05 14:34:37 +00:00
}
2018-12-01 18:55:35 +00:00
// AuthTestResponse ...
2016-09-05 14:34:37 +00:00
type AuthTestResponse struct {
URL string `json:"url"`
Team string `json:"team"`
User string `json:"user"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
}
type authTestResponseFull struct {
SlackResponse
AuthTestResponse
}
2018-12-01 18:55:35 +00:00
// Client for the slack api.
2016-09-05 14:34:37 +00:00
type Client struct {
2018-08-09 22:38:19 +00:00
token string
debug bool
2018-12-01 18:55:35 +00:00
log ilogger
httpclient httpClient
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
// Option defines an option for a Client
type Option func(*Client)
2018-01-08 21:41:38 +00:00
2018-08-09 22:38:19 +00:00
// OptionHTTPClient - provide a custom http client to the slack client.
2018-12-01 18:55:35 +00:00
func OptionHTTPClient(client httpClient) func(*Client) {
return func(c *Client) {
c.httpclient = client
}
}
// OptionDebug enable debugging for the client
func OptionDebug(b bool) func(*Client) {
return func(c *Client) {
c.debug = b
}
}
// OptionLog set logging for client.
func OptionLog(l logger) func(*Client) {
return func(c *Client) {
c.log = internalLog{logger: l}
2018-08-09 22:38:19 +00:00
}
2018-01-08 21:41:38 +00:00
}
2018-08-09 22:38:19 +00:00
// New builds a slack client from the provided token and options.
func New(token string, options ...Option) *Client {
s := &Client{
token: token,
2018-12-01 18:55:35 +00:00
httpclient: &http.Client{},
log: log.New(os.Stderr, "nlopes/slack", log.LstdFlags|log.Lshortfile),
2018-08-09 22:38:19 +00:00
}
for _, opt := range options {
opt(s)
}
2016-09-05 14:34:37 +00:00
return s
}
// AuthTest tests if the user is able to do authenticated requests or not
func (api *Client) AuthTest() (response *AuthTestResponse, error error) {
2017-07-16 12:29:46 +00:00
return api.AuthTestContext(context.Background())
}
// AuthTestContext tests if the user is able to do authenticated requests or not with a custom context
2018-12-01 18:55:35 +00:00
func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestResponse, err error) {
2018-08-09 22:38:19 +00:00
api.Debugf("Challenging auth...")
2016-09-05 14:34:37 +00:00
responseFull := &authTestResponseFull{}
2018-12-01 18:55:35 +00:00
err = postSlackMethod(ctx, api.httpclient, "auth.test", url.Values{"token": {api.token}}, responseFull, api)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
2018-08-09 22:38:19 +00:00
2018-12-01 18:55:35 +00:00
return &responseFull.AuthTestResponse, responseFull.Err()
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
// Debugf print a formatted debug line.
2016-09-05 14:34:37 +00:00
func (api *Client) Debugf(format string, v ...interface{}) {
if api.debug {
2018-12-01 18:55:35 +00:00
api.log.Output(2, fmt.Sprintf(format, v...))
2016-09-05 14:34:37 +00:00
}
}
2018-08-09 22:38:19 +00:00
// Debugln print a debug line.
2016-09-05 14:34:37 +00:00
func (api *Client) Debugln(v ...interface{}) {
if api.debug {
2018-12-01 18:55:35 +00:00
api.log.Output(2, fmt.Sprintln(v...))
2016-09-05 14:34:37 +00:00
}
}
2018-12-01 18:55:35 +00:00
// Debug returns if debug is enabled.
func (api *Client) Debug() bool {
return api.debug
}