matterbridge/vendor/github.com/nlopes/slack/misc.go

204 lines
5.2 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
"bytes"
2017-07-16 12:29:46 +00:00
"context"
2016-09-05 14:34:37 +00:00
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
2016-11-05 23:07:24 +00:00
"net/http/httputil"
2016-09-05 14:34:37 +00:00
"net/url"
"os"
"path/filepath"
2018-01-08 21:41:38 +00:00
"strconv"
2017-07-16 12:29:46 +00:00
"strings"
2016-09-05 14:34:37 +00:00
"time"
)
2017-07-16 12:29:46 +00:00
// 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)
}
var customHTTPClient HTTPRequester
// HTTPClient sets a custom http.Client
// deprecated: in favor of SetHTTPClient()
2016-09-05 14:34:37 +00:00
var HTTPClient = &http.Client{}
type WebResponse struct {
Ok bool `json:"ok"`
Error *WebError `json:"error"`
}
type WebError string
func (s WebError) Error() string {
return string(s)
}
2018-01-08 21:41:38 +00:00
type RateLimitedError struct {
RetryAfter time.Duration
}
func (e *RateLimitedError) Error() string {
return fmt.Sprintf("Slack rate limit exceeded, retry after %s", e.RetryAfter)
}
2017-07-16 12:29:46 +00:00
func fileUploadReq(ctx context.Context, path, fieldname, filename string, values url.Values, r io.Reader) (*http.Request, error) {
2016-09-05 14:34:37 +00:00
body := &bytes.Buffer{}
wr := multipart.NewWriter(body)
2017-07-16 12:29:46 +00:00
ioWriter, err := wr.CreateFormFile(fieldname, filename)
2016-09-05 14:34:37 +00:00
if err != nil {
wr.Close()
return nil, err
}
2017-07-16 12:29:46 +00:00
_, err = io.Copy(ioWriter, r)
2016-09-05 14:34:37 +00:00
if err != nil {
wr.Close()
return nil, err
}
// Close the multipart writer or the footer won't be written
wr.Close()
req, err := http.NewRequest("POST", path, body)
2017-07-16 12:29:46 +00:00
req = req.WithContext(ctx)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", wr.FormDataContentType())
req.URL.RawQuery = (values).Encode()
return req, nil
}
func parseResponseBody(body io.ReadCloser, intf *interface{}, debug bool) error {
response, err := ioutil.ReadAll(body)
if err != nil {
return err
}
// FIXME: will be api.Debugf
if debug {
logger.Printf("parseResponseBody: %s\n", string(response))
}
2018-01-08 21:41:38 +00:00
return json.Unmarshal(response, &intf)
2016-09-05 14:34:37 +00:00
}
2017-07-16 12:29:46 +00:00
func postLocalWithMultipartResponse(ctx context.Context, path, fpath, fieldname string, values url.Values, intf interface{}, debug bool) error {
fullpath, err := filepath.Abs(fpath)
if err != nil {
return err
}
file, err := os.Open(fullpath)
if err != nil {
return err
}
defer file.Close()
return postWithMultipartResponse(ctx, path, filepath.Base(fpath), fieldname, values, file, intf, debug)
}
func postWithMultipartResponse(ctx context.Context, path, name, fieldname string, values url.Values, r io.Reader, intf interface{}, debug bool) error {
req, err := fileUploadReq(ctx, SLACK_API+path, fieldname, name, values, r)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := getHTTPClient().Do(req)
2016-09-05 14:34:37 +00:00
if err != nil {
return err
}
defer resp.Body.Close()
2016-11-05 23:07:24 +00:00
2018-01-08 21:41:38 +00:00
if resp.StatusCode == http.StatusTooManyRequests {
retry, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
if err != nil {
return err
}
return &RateLimitedError{time.Duration(retry) * time.Second}
}
2016-11-05 23:07:24 +00:00
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
2018-01-08 21:41:38 +00:00
if resp.StatusCode != http.StatusOK {
2016-11-05 23:07:24 +00:00
logResponse(resp, debug)
return fmt.Errorf("Slack server error: %s.", resp.Status)
}
2016-09-05 14:34:37 +00:00
return parseResponseBody(resp.Body, &intf, debug)
}
2017-07-16 12:29:46 +00:00
func postForm(ctx context.Context, endpoint string, values url.Values, intf interface{}, debug bool) error {
reqBody := strings.NewReader(values.Encode())
req, err := http.NewRequest("POST", endpoint, reqBody)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
resp, err := getHTTPClient().Do(req)
2016-09-05 14:34:37 +00:00
if err != nil {
return err
}
defer resp.Body.Close()
2018-01-08 21:41:38 +00:00
if resp.StatusCode == http.StatusTooManyRequests {
retry, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
if err != nil {
return err
}
return &RateLimitedError{time.Duration(retry) * time.Second}
}
2017-07-16 12:29:46 +00:00
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
2018-01-08 21:41:38 +00:00
if resp.StatusCode != http.StatusOK {
2017-07-16 12:29:46 +00:00
logResponse(resp, debug)
return fmt.Errorf("Slack server error: %s.", resp.Status)
}
2016-09-05 14:34:37 +00:00
return parseResponseBody(resp.Body, &intf, debug)
}
2017-07-16 12:29:46 +00:00
func post(ctx context.Context, path string, values url.Values, intf interface{}, debug bool) error {
return postForm(ctx, SLACK_API+path, values, intf, debug)
2016-09-05 14:34:37 +00:00
}
2017-07-16 12:29:46 +00:00
func parseAdminResponse(ctx context.Context, method string, teamName string, values url.Values, intf interface{}, debug bool) error {
2016-09-05 14:34:37 +00:00
endpoint := fmt.Sprintf(SLACK_WEB_API_FORMAT, teamName, method, time.Now().Unix())
2017-07-16 12:29:46 +00:00
return postForm(ctx, endpoint, values, intf, debug)
2016-09-05 14:34:37 +00:00
}
2016-11-05 23:07:24 +00:00
func logResponse(resp *http.Response, debug bool) error {
if debug {
text, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
2017-07-16 12:29:46 +00:00
logger.Print(string(text))
2016-11-05 23:07:24 +00:00
}
return nil
}
2017-07-16 12:29:46 +00:00
func getHTTPClient() HTTPRequester {
if customHTTPClient != nil {
return customHTTPClient
}
return HTTPClient
}
// 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
}