diff options
Diffstat (limited to 'vendor/github.com/nlopes/slack/slack.go')
-rw-r--r-- | vendor/github.com/nlopes/slack/slack.go | 102 |
1 files changed, 64 insertions, 38 deletions
diff --git a/vendor/github.com/nlopes/slack/slack.go b/vendor/github.com/nlopes/slack/slack.go index 754117c1..6d1e7de9 100644 --- a/vendor/github.com/nlopes/slack/slack.go +++ b/vendor/github.com/nlopes/slack/slack.go @@ -5,20 +5,47 @@ import ( "errors" "fmt" "log" + "net/http" "net/url" "os" ) -var logger stdLogger // A logger that can be set by consumers -/* - Added as a var so that we can change this for testing purposes -*/ +// Added as a var so that we can change this for testing purposes var SLACK_API string = "https://slack.com/api/" var SLACK_WEB_API_FORMAT string = "https://%s.slack.com/api/users.admin.%s?t=%s" -type SlackResponse struct { - Ok bool `json:"ok"` - Error string `json:"error"` +// HTTPClient sets a custom http.Client +// deprecated: in favor of SetHTTPClient() +var HTTPClient = &http.Client{} + +var customHTTPClient HTTPRequester = HTTPClient + +// HTTPRequester defines the minimal interface needed for an http.Client to be implemented. +// +// Use it in conjunction with the SetHTTPClient function to allow for other capabilities +// like a tracing http.Client +type HTTPRequester interface { + Do(*http.Request) (*http.Response, error) +} + +// SetHTTPClient allows you to specify a custom http.Client +// Use this instead of the package level HTTPClient variable if you want to use a custom client like the +// Stackdriver Trace HTTPClient https://godoc.org/cloud.google.com/go/trace#HTTPClient +func SetHTTPClient(client HTTPRequester) { + customHTTPClient = client +} + +// ResponseMetadata holds pagination metadata +type ResponseMetadata struct { + Cursor string `json:"next_cursor"` +} + +func (t *ResponseMetadata) initialize() *ResponseMetadata { + if t != nil { + return t + } + + return &ResponseMetadata{} } type AuthTestResponse struct { @@ -35,41 +62,33 @@ type authTestResponseFull struct { } type Client struct { - config struct { - token string - } - info Info - debug bool + token string + info Info + debug bool + httpclient HTTPRequester } -// stdLogger is a logger interface compatible with both stdlib and some -// 3rd party loggers such as logrus. -type stdLogger interface { - Print(...interface{}) - Printf(string, ...interface{}) - Println(...interface{}) - - Fatal(...interface{}) - Fatalf(string, ...interface{}) - Fatalln(...interface{}) - - Panic(...interface{}) - Panicf(string, ...interface{}) - Panicln(...interface{}) +// Option defines an option for a Client +type Option func(*Client) - Output(int, string) error +// OptionHTTPClient - provide a custom http client to the slack client. +func OptionHTTPClient(c HTTPRequester) func(*Client) { + return func(s *Client) { + s.httpclient = c + } } -// SetLogger let's library users supply a logger, so that api debugging -// can be logged along with the application's debugging info. -func SetLogger(l stdLogger) { - logger = l -} +// New builds a slack client from the provided token and options. +func New(token string, options ...Option) *Client { + s := &Client{ + token: token, + httpclient: customHTTPClient, + } + + for _, opt := range options { + opt(s) + } -// New creates new Client. -func New(token string) *Client { - s := &Client{} - s.config.token = token return s } @@ -80,14 +99,19 @@ func (api *Client) AuthTest() (response *AuthTestResponse, error error) { // AuthTestContext tests if the user is able to do authenticated requests or not with a custom context func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestResponse, error error) { + api.Debugf("Challenging auth...") responseFull := &authTestResponseFull{} - err := post(ctx, "auth.test", url.Values{"token": {api.config.token}}, responseFull, api.debug) + err := postSlackMethod(ctx, api.httpclient, "auth.test", url.Values{"token": {api.token}}, responseFull, api.debug) if err != nil { + api.Debugf("failed to test for auth: %s", err) return nil, err } if !responseFull.Ok { + api.Debugf("auth response was not Ok: %s", responseFull.Error) return nil, errors.New(responseFull.Error) } + + api.Debugf("Auth challenge was successful with response %+v", responseFull.AuthTestResponse) return &responseFull.AuthTestResponse, nil } @@ -97,16 +121,18 @@ func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestRespo func (api *Client) SetDebug(debug bool) { api.debug = debug if debug && logger == nil { - logger = log.New(os.Stdout, "nlopes/slack", log.LstdFlags|log.Lshortfile) + SetLogger(log.New(os.Stdout, "nlopes/slack", log.LstdFlags|log.Lshortfile)) } } +// Debugf print a formatted debug line. func (api *Client) Debugf(format string, v ...interface{}) { if api.debug { logger.Output(2, fmt.Sprintf(format, v...)) } } +// Debugln print a debug line. func (api *Client) Debugln(v ...interface{}) { if api.debug { logger.Output(2, fmt.Sprintln(v...)) |