Debian Codegraph @

slack.go

raw symbols SCIP
1 package slack
2
3 import (
4 "context"
5 "fmt"
6 "log"
7 "net/http"
8 "net/url"
9 "os"
10 )
11
12 const (
13 // APIURL of the slack api.
14 APIURL = "https://slack.com/api/"
15 // WEBAPIURLFormat ...
16 WEBAPIURLFormat = "https://%s.slack.com/api/users.admin.%s?t=%d"
17 )
18
19 // httpClient defines the minimal interface needed for an http.Client to be implemented.
20 type httpClient interface {
21 Do(*http.Request) (*http.Response, error)
22 }
23
24 // ResponseMetadata holds pagination metadata
25 type ResponseMetadata struct {
26 Cursor string `json:"next_cursor"`
27 Messages []string `json:"messages"`
28 Warnings []string `json:"warnings"`
29 }
30
31 func (t *ResponseMetadata) initialize() *ResponseMetadata {
32 if t != nil {
33 return t
34 }
35
36 return &ResponseMetadata{}
37 }
38
39 // AuthTestResponse ...
40 type AuthTestResponse struct {
41 URL string `json:"url"`
42 Team string `json:"team"`
43 User string `json:"user"`
44 TeamID string `json:"team_id"`
45 UserID string `json:"user_id"`
46 // EnterpriseID is only returned when an enterprise id present
47 EnterpriseID string `json:"enterprise_id,omitempty"`
48 BotID string `json:"bot_id"`
49 }
50
51 type authTestResponseFull struct {
52 SlackResponse
53 AuthTestResponse
54 }
55
56 // Client for the slack api.
57 type ParamOption func(*url.Values)
58
59 type Client struct {
60 token string
61 appLevelToken string
62 endpoint string
63 debug bool
64 log ilogger
65 httpclient httpClient
66 }
67
68 // Option defines an option for a Client
69 type Option func(*Client)
70
71 // OptionHTTPClient - provide a custom http client to the slack client.
72 func OptionHTTPClient(client httpClient) func(*Client) {
73 return func(c *Client) {
74 c.httpclient = client
75 }
76 }
77
78 // OptionDebug enable debugging for the client
79 func OptionDebug(b bool) func(*Client) {
80 return func(c *Client) {
81 c.debug = b
82 }
83 }
84
85 // OptionLog set logging for client.
86 func OptionLog(l logger) func(*Client) {
87 return func(c *Client) {
88 c.log = internalLog{logger: l}
89 }
90 }
91
92 // OptionAPIURL set the url for the client. only useful for testing.
93 func OptionAPIURL(u string) func(*Client) {
94 return func(c *Client) { c.endpoint = u }
95 }
96
97 // OptionAppLevelToken sets an app-level token for the client.
98 func OptionAppLevelToken(token string) func(*Client) {
99 return func(c *Client) { c.appLevelToken = token }
100 }
101
102 // New builds a slack client from the provided token and options.
103 func New(token string, options ...Option) *Client {
104 s := &Client{
105 token: token,
106 endpoint: APIURL,
107 httpclient: &http.Client{},
108 log: log.New(os.Stderr, "slack-go/slack", log.LstdFlags|log.Lshortfile),
109 }
110
111 for _, opt := range options {
112 opt(s)
113 }
114
115 return s
116 }
117
118 // AuthTest tests if the user is able to do authenticated requests or not
119 func (api *Client) AuthTest() (response *AuthTestResponse, error error) {
120 return api.AuthTestContext(context.Background())
121 }
122
123 // AuthTestContext tests if the user is able to do authenticated requests or not with a custom context
124 func (api *Client) AuthTestContext(ctx context.Context) (response *AuthTestResponse, err error) {
125 api.Debugf("Challenging auth...")
126 responseFull := &authTestResponseFull{}
127 err = api.postMethod(ctx, "auth.test", url.Values{"token": {api.token}}, responseFull)
128 if err != nil {
129 return nil, err
130 }
131
132 return &responseFull.AuthTestResponse, responseFull.Err()
133 }
134
135 // Debugf print a formatted debug line.
136 func (api *Client) Debugf(format string, v ...interface{}) {
137 if api.debug {
138 api.log.Output(2, fmt.Sprintf(format, v...))
139 }
140 }
141
142 // Debugln print a debug line.
143 func (api *Client) Debugln(v ...interface{}) {
144 if api.debug {
145 api.log.Output(2, fmt.Sprintln(v...))
146 }
147 }
148
149 // Debug returns if debug is enabled.
150 func (api *Client) Debug() bool {
151 return api.debug
152 }
153
154 // post to a slack web method.
155 func (api *Client) postMethod(ctx context.Context, path string, values url.Values, intf interface{}) error {
156 return postForm(ctx, api.httpclient, api.endpoint+path, values, intf, api)
157 }
158
159 // get a slack web method.
160 func (api *Client) getMethod(ctx context.Context, path string, token string, values url.Values, intf interface{}) error {
161 return getResource(ctx, api.httpclient, api.endpoint+path, token, values, intf, api)
162 }