-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
69 lines (57 loc) · 1.63 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package slack
import (
"context"
"net/url"
"strings"
"github.com/kevinburke/rest/restclient"
)
const Version = "0.1.1"
var userAgent string
func init() {
userAgent = "slack-go/" + Version
}
func New(token string) *Client {
restClient := restclient.NewBearerClient(token, baseURL)
restClient.UploadType = restclient.FormURLEncoded
c := &Client{
Client: restClient,
baseURL: baseURL,
}
c.Chat = &ChatService{client: c}
c.Users = &UserService{client: c}
return c
}
const baseURL = "https://api.slack.com"
type Client struct {
*restclient.Client
baseURL string
Chat *ChatService
Users *UserService
}
// CreateResource makes a POST request to the given resource.
func (c *Client) CreateResource(ctx context.Context, pathPart string, data url.Values, v interface{}) error {
return c.MakeRequest(ctx, "POST", pathPart, data, v)
}
func (c *Client) ListResource(ctx context.Context, pathPart string, data url.Values, v interface{}) error {
return c.MakeRequest(ctx, "GET", pathPart, data, v)
}
// Make a request to the Slack API.
func (c *Client) MakeRequest(ctx context.Context, method string, pathPart string, data url.Values, v interface{}) error {
rb := new(strings.Reader)
if data != nil && (method == "POST" || method == "PUT") {
rb = strings.NewReader(data.Encode())
}
if method == "GET" && data != nil {
pathPart = pathPart + "?" + data.Encode()
}
req, err := c.NewRequestWithContext(ctx, method, pathPart, rb)
if err != nil {
return err
}
if ua := req.Header.Get("User-Agent"); ua == "" {
req.Header.Set("User-Agent", userAgent)
} else {
req.Header.Set("User-Agent", userAgent+" "+ua)
}
return c.Do(req, &v)
}