5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-30 01:31:36 +00:00
matterbridge/vendor/github.com/nlopes/slack/slack.go

141 lines
3.7 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"
2016-09-05 14:34:37 +00:00
"errors"
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"
)
2018-08-09 22:38:19 +00:00
// Added as a var so that we can change this for testing purposes
2016-09-05 14:34:37 +00:00
var SLACK_API string = "https://slack.com/api/"
var SLACK_WEB_API_FORMAT string = "https://%s.slack.com/api/users.admin.%s?t=%s"
2018-08-09 22:38:19 +00:00
// 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{}
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
}
type Client struct {
2018-08-09 22:38:19 +00:00
token string
info Info
debug bool
httpclient HTTPRequester
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.
func OptionHTTPClient(c HTTPRequester) func(*Client) {
return func(s *Client) {
s.httpclient = c
}
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,
httpclient: customHTTPClient,
}
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
func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestResponse, error error) {
2018-08-09 22:38:19 +00:00
api.Debugf("Challenging auth...")
2016-09-05 14:34:37 +00:00
responseFull := &authTestResponseFull{}
2018-08-09 22:38:19 +00:00
err := postSlackMethod(ctx, api.httpclient, "auth.test", url.Values{"token": {api.token}}, responseFull, api.debug)
2016-09-05 14:34:37 +00:00
if err != nil {
2018-08-09 22:38:19 +00:00
api.Debugf("failed to test for auth: %s", err)
2016-09-05 14:34:37 +00:00
return nil, err
}
if !responseFull.Ok {
2018-08-09 22:38:19 +00:00
api.Debugf("auth response was not Ok: %s", responseFull.Error)
2016-09-05 14:34:37 +00:00
return nil, errors.New(responseFull.Error)
}
2018-08-09 22:38:19 +00:00
api.Debugf("Auth challenge was successful with response %+v", responseFull.AuthTestResponse)
2016-09-05 14:34:37 +00:00
return &responseFull.AuthTestResponse, nil
}
// SetDebug switches the api into debug mode
// When in debug mode, it logs various info about what its doing
// If you ever use this in production, don't call SetDebug(true)
func (api *Client) SetDebug(debug bool) {
api.debug = debug
if debug && logger == nil {
2018-08-09 22:38:19 +00:00
SetLogger(log.New(os.Stdout, "nlopes/slack", log.LstdFlags|log.Lshortfile))
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-01-08 21:41:38 +00:00
logger.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-01-08 21:41:38 +00:00
logger.Output(2, fmt.Sprintln(v...))
2016-09-05 14:34:37 +00:00
}
}