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

154 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"
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"
)
2019-09-07 20:46:58 +00:00
const (
// APIURL of the slack api.
APIURL = "https://slack.com/api/"
// WEBAPIURLFormat ...
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"`
2019-09-07 20:46:58 +00:00
// EnterpriseID is only returned when an enterprise id present
EnterpriseID string `json:"enterprise_id,omitempty"`
2016-09-05 14:34:37 +00:00
}
type authTestResponseFull struct {
SlackResponse
AuthTestResponse
}
2018-12-01 18:55:35 +00:00
// Client for the slack api.
2019-09-07 20:46:58 +00:00
type ParamOption func(*url.Values)
2016-09-05 14:34:37 +00:00
type Client struct {
2018-08-09 22:38:19 +00:00
token string
2019-09-07 20:46:58 +00:00
endpoint string
2018-08-09 22:38:19 +00:00
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
}
2019-09-07 20:46:58 +00:00
// OptionAPIURL set the url for the client. only useful for testing.
func OptionAPIURL(u string) func(*Client) {
return func(c *Client) { c.endpoint = u }
}
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,
2019-09-07 20:46:58 +00:00
endpoint: APIURL,
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{}
2019-09-07 20:46:58 +00:00
err = api.postMethod(ctx, "auth.test", url.Values{"token": {api.token}}, responseFull)
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
}
2019-09-07 20:46:58 +00:00
// post to a slack web method.
func (api *Client) postMethod(ctx context.Context, path string, values url.Values, intf interface{}) error {
return postForm(ctx, api.httpclient, api.endpoint+path, values, intf, api)
}
// get a slack web method.
func (api *Client) getMethod(ctx context.Context, path string, values url.Values, intf interface{}) error {
return getResource(ctx, api.httpclient, api.endpoint+path, values, intf, api)
}