Codebase list golang-github-nlopes-slack / f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.2.0 slack.go
f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.2.0

Tree @f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.2.0 (Download .tar.gz)

slack.go @f282e598-9350-4d40-ba4b-90cbf2cd4600/v0.2.0

8258160
 
8384701
d4176bf
8384701
a1e1c30
16288f9
ba9c8fd
8384701
6b76b2d
8384701
8258160
0d12123
1b96f65
ece2352
8258160
ba9c8fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b96f65
8384701
 
8258160
 
1b96f65
6aeef4e
1b96f65
 
74f694f
 
8258160
 
1b96f65
 
 
8258160
 
16288f9
ba9c8fd
 
 
 
 
 
 
 
 
 
 
 
 
334c7e2
8258160
 
ba9c8fd
 
 
 
 
 
 
 
 
 
 
334c7e2
8258160
 
55d471b
16288f9
d4176bf
 
 
 
 
d86785d
55d471b
ba9c8fd
8258160
d86785d
1b96f65
8258160
55d471b
d86785d
8384701
ff68f18
d86785d
 
55d471b
8258160
 
55d471b
 
 
16288f9
1b96f65
6b76b2d
0d12123
6b76b2d
8258160
16288f9
0d12123
16288f9
 
a1e1c30
16288f9
 
 
0d12123
16288f9
 
d92fc05
16288f9
 
package slack

import (
	"context"
	"errors"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
)

// 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"

// 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
}

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 {
	token      string
	info       Info
	debug      bool
	httpclient HTTPRequester
}

// Option defines an option for a Client
type Option func(*Client)

// OptionHTTPClient - provide a custom http client to the slack client.
func OptionHTTPClient(c HTTPRequester) func(*Client) {
	return func(s *Client) {
		s.httpclient = c
	}
}

// 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)
	}

	return s
}

// AuthTest tests if the user is able to do authenticated requests or not
func (api *Client) AuthTest() (response *AuthTestResponse, error error) {
	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) {
	api.Debugf("Challenging auth...")
	responseFull := &authTestResponseFull{}
	err := post(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
}

// 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 {
		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...))
	}
}