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

115 lines
2.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"
"net/url"
"os"
)
2018-01-08 21:41:38 +00:00
var logger stdLogger // A logger that can be set by consumers
2016-09-05 14:34:37 +00:00
/*
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"`
}
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 {
config struct {
token string
}
info Info
debug bool
}
2018-01-08 21:41:38 +00:00
// 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{})
Output(int, string) error
}
2016-09-05 14:34:37 +00:00
// SetLogger let's library users supply a logger, so that api debugging
// can be logged along with the application's debugging info.
2018-01-08 21:41:38 +00:00
func SetLogger(l stdLogger) {
2016-09-05 14:34:37 +00:00
logger = l
}
2018-01-08 21:41:38 +00:00
// New creates new Client.
2016-09-05 14:34:37 +00:00
func New(token string) *Client {
s := &Client{}
s.config.token = token
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) {
2016-09-05 14:34:37 +00:00
responseFull := &authTestResponseFull{}
2017-07-16 12:29:46 +00:00
err := post(ctx, "auth.test", url.Values{"token": {api.config.token}}, responseFull, api.debug)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
if !responseFull.Ok {
return nil, errors.New(responseFull.Error)
}
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 {
2017-07-16 12:29:46 +00:00
logger = log.New(os.Stdout, "nlopes/slack", log.LstdFlags|log.Lshortfile)
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
}
}
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
}
}