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

267 lines
6.8 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"
2018-08-09 22:38:19 +00:00
"errors"
2016-09-05 14:34:37 +00:00
"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"
)
2018-12-01 18:55:35 +00:00
// SlackResponse handles parsing out errors from the web api.
2018-08-09 22:38:19 +00:00
type SlackResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
2017-07-16 12:29:46 +00:00
}
2018-08-09 22:38:19 +00:00
func (t SlackResponse) Err() error {
if t.Ok {
return nil
}
// handle pure text based responses like chat.post
// which while they have a slack response in their data structure
// it doesn't actually get set during parsing.
if strings.TrimSpace(t.Error) == "" {
return nil
}
2017-07-16 12:29:46 +00:00
2018-08-09 22:38:19 +00:00
return errors.New(t.Error)
}
2016-09-05 14:34:37 +00:00
2018-08-09 22:38:19 +00:00
// StatusCodeError represents an http response error.
// type httpStatusCode interface { HTTPStatusCode() int } to handle it.
type statusCodeError struct {
Code int
Status string
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
func (t statusCodeError) Error() string {
// TODO: this is a bad error string, should clean it up with a breaking changes
// merger.
return fmt.Sprintf("Slack server error: %s.", t.Status)
}
2016-09-05 14:34:37 +00:00
2018-08-09 22:38:19 +00:00
func (t statusCodeError) HTTPStatusCode() int {
return t.Code
2016-09-05 14:34:37 +00:00
}
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)
}
2018-12-01 18:55:35 +00:00
func fileUploadReq(ctx context.Context, path string, values url.Values, r io.Reader) (*http.Request, error) {
req, err := http.NewRequest("POST", path, r)
2016-09-05 14:34:37 +00:00
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.URL.RawQuery = (values).Encode()
return req, nil
}
2018-12-01 18:55:35 +00:00
func parseResponseBody(body io.ReadCloser, intf interface{}, d debug) error {
2016-09-05 14:34:37 +00:00
response, err := ioutil.ReadAll(body)
if err != nil {
return err
}
2018-12-01 18:55:35 +00:00
if d.Debug() {
d.Debugln("parseResponseBody", string(response))
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
return json.Unmarshal(response, intf)
2016-09-05 14:34:37 +00:00
}
2018-12-01 18:55:35 +00:00
func postLocalWithMultipartResponse(ctx context.Context, client httpClient, path, fpath, fieldname string, values url.Values, intf interface{}, d debug) error {
2017-07-16 12:29:46 +00:00
fullpath, err := filepath.Abs(fpath)
if err != nil {
return err
}
file, err := os.Open(fullpath)
if err != nil {
return err
}
defer file.Close()
2018-12-01 18:55:35 +00:00
return postWithMultipartResponse(ctx, client, path, filepath.Base(fpath), fieldname, values, file, intf, d)
2017-07-16 12:29:46 +00:00
}
2018-12-01 18:55:35 +00:00
func postWithMultipartResponse(ctx context.Context, client httpClient, path, name, fieldname string, values url.Values, r io.Reader, intf interface{}, d debug) error {
pipeReader, pipeWriter := io.Pipe()
wr := multipart.NewWriter(pipeWriter)
errc := make(chan error)
go func() {
defer pipeWriter.Close()
ioWriter, err := wr.CreateFormFile(fieldname, name)
if err != nil {
errc <- err
return
}
_, err = io.Copy(ioWriter, r)
if err != nil {
errc <- err
return
}
if err = wr.Close(); err != nil {
errc <- err
return
}
}()
req, err := fileUploadReq(ctx, APIURL+path, values, pipeReader)
2017-07-16 12:29:46 +00:00
if err != nil {
return err
}
2018-12-01 18:55:35 +00:00
req.Header.Add("Content-Type", wr.FormDataContentType())
2017-07-16 12:29:46 +00:00
req = req.WithContext(ctx)
2018-08-09 22:38:19 +00:00
resp, err := client.Do(req)
2018-12-01 18:55:35 +00:00
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 {
2018-12-01 18:55:35 +00:00
logResponse(resp, d)
2018-08-09 22:38:19 +00:00
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
2016-11-05 23:07:24 +00:00
}
2018-12-01 18:55:35 +00:00
select {
case err = <-errc:
return err
default:
return parseResponseBody(resp.Body, intf, d)
}
2016-09-05 14:34:37 +00:00
}
2018-12-01 18:55:35 +00:00
func doPost(ctx context.Context, client httpClient, req *http.Request, intf interface{}, d debug) error {
2017-07-16 12:29:46 +00:00
req = req.WithContext(ctx)
2018-08-09 22:38:19 +00:00
resp, err := client.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 {
2018-12-01 18:55:35 +00:00
logResponse(resp, d)
2018-08-09 22:38:19 +00:00
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
}
2018-12-01 18:55:35 +00:00
return parseResponseBody(resp.Body, intf, d)
2018-08-09 22:38:19 +00:00
}
// post JSON.
2018-12-01 18:55:35 +00:00
func postJSON(ctx context.Context, client httpClient, endpoint, token string, json []byte, intf interface{}, d debug) error {
2018-08-09 22:38:19 +00:00
reqBody := bytes.NewBuffer(json)
req, err := http.NewRequest("POST", endpoint, reqBody)
if err != nil {
return err
2017-07-16 12:29:46 +00:00
}
2018-08-09 22:38:19 +00:00
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
2018-12-01 18:55:35 +00:00
return doPost(ctx, client, req, intf, d)
2018-08-09 22:38:19 +00:00
}
2017-07-16 12:29:46 +00:00
2018-08-09 22:38:19 +00:00
// post a url encoded form.
2018-12-01 18:55:35 +00:00
func postForm(ctx context.Context, client httpClient, endpoint string, values url.Values, intf interface{}, d debug) error {
2018-08-09 22:38:19 +00:00
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")
2018-12-01 18:55:35 +00:00
return doPost(ctx, client, req, intf, d)
2016-09-05 14:34:37 +00:00
}
2018-08-09 22:38:19 +00:00
// post to a slack web method.
2018-12-01 18:55:35 +00:00
func postSlackMethod(ctx context.Context, client httpClient, path string, values url.Values, intf interface{}, d debug) error {
return postForm(ctx, client, APIURL+path, values, intf, d)
2016-09-05 14:34:37 +00:00
}
2018-12-01 18:55:35 +00:00
// get a slack web method.
func getSlackMethod(ctx context.Context, client httpClient, path string, values url.Values, intf interface{}, d debug) error {
return getResource(ctx, client, APIURL+path, values, intf, d)
2016-09-05 14:34:37 +00:00
}
2016-11-05 23:07:24 +00:00
2018-12-01 18:55:35 +00:00
func getResource(ctx context.Context, client httpClient, endpoint string, values url.Values, intf interface{}, d debug) error {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.URL.RawQuery = values.Encode()
return doPost(ctx, client, req, intf, d)
}
func parseAdminResponse(ctx context.Context, client httpClient, method string, teamName string, values url.Values, intf interface{}, d debug) error {
endpoint := fmt.Sprintf(WEBAPIURLFormat, teamName, method, time.Now().Unix())
return postForm(ctx, client, endpoint, values, intf, d)
}
func logResponse(resp *http.Response, d debug) error {
if d.Debug() {
2016-11-05 23:07:24 +00:00
text, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
2018-12-01 18:55:35 +00:00
d.Debugln(string(text))
2016-11-05 23:07:24 +00:00
}
return nil
}
2017-07-16 12:29:46 +00:00
2018-08-09 22:38:19 +00:00
func okJSONHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(SlackResponse{
Ok: true,
})
rw.Write(response)
}
type errorString string
2017-07-16 12:29:46 +00:00
2018-08-09 22:38:19 +00:00
func (t errorString) Error() string {
return string(t)
2017-07-16 12:29:46 +00:00
}
2018-08-09 22:38:19 +00:00
// timerReset safely reset a timer, see time.Timer.Reset for details.
func timerReset(t *time.Timer, d time.Duration) {
if !t.Stop() {
<-t.C
}
t.Reset(d)
2017-07-16 12:29:46 +00:00
}