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

241 lines
6.2 KiB
Go
Raw Normal View History

2016-09-05 07:34:37 -07:00
package slack
import (
"bytes"
2017-07-16 05:29:46 -07:00
"context"
2016-09-05 07:34:37 -07:00
"encoding/json"
2018-08-09 15:38:19 -07:00
"errors"
2016-09-05 07:34:37 -07:00
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
2016-11-05 16:07:24 -07:00
"net/http/httputil"
2016-09-05 07:34:37 -07:00
"net/url"
"os"
"path/filepath"
2018-01-08 13:41:38 -08:00
"strconv"
2017-07-16 05:29:46 -07:00
"strings"
2016-09-05 07:34:37 -07:00
"time"
)
2018-08-09 15:38:19 -07:00
type SlackResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
2017-07-16 05:29:46 -07:00
}
2018-08-09 15:38:19 -07: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 05:29:46 -07:00
2018-08-09 15:38:19 -07:00
return errors.New(t.Error)
}
2016-09-05 07:34:37 -07:00
2018-08-09 15:38:19 -07: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 07:34:37 -07:00
}
2018-08-09 15:38:19 -07: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 07:34:37 -07:00
2018-08-09 15:38:19 -07:00
func (t statusCodeError) HTTPStatusCode() int {
return t.Code
2016-09-05 07:34:37 -07:00
}
2018-01-08 13:41:38 -08: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 05:29:46 -07:00
func fileUploadReq(ctx context.Context, path, fieldname, filename string, values url.Values, r io.Reader) (*http.Request, error) {
2016-09-05 07:34:37 -07:00
body := &bytes.Buffer{}
wr := multipart.NewWriter(body)
2017-07-16 05:29:46 -07:00
ioWriter, err := wr.CreateFormFile(fieldname, filename)
2016-09-05 07:34:37 -07:00
if err != nil {
wr.Close()
return nil, err
}
2017-07-16 05:29:46 -07:00
_, err = io.Copy(ioWriter, r)
2016-09-05 07:34:37 -07: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 05:29:46 -07:00
req = req.WithContext(ctx)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", wr.FormDataContentType())
req.URL.RawQuery = (values).Encode()
return req, nil
}
2018-08-09 15:38:19 -07:00
func parseResponseBody(body io.ReadCloser, intf interface{}, debug bool) error {
2016-09-05 07:34:37 -07:00
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-08-09 15:38:19 -07:00
return json.Unmarshal(response, intf)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
func postLocalWithMultipartResponse(ctx context.Context, client HTTPRequester, path, fpath, fieldname string, values url.Values, intf interface{}, debug bool) error {
2017-07-16 05:29:46 -07: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-08-09 15:38:19 -07:00
return postWithMultipartResponse(ctx, client, path, filepath.Base(fpath), fieldname, values, file, intf, debug)
2017-07-16 05:29:46 -07:00
}
2018-08-09 15:38:19 -07:00
func postWithMultipartResponse(ctx context.Context, client HTTPRequester, path, name, fieldname string, values url.Values, r io.Reader, intf interface{}, debug bool) error {
2017-07-16 05:29:46 -07:00
req, err := fileUploadReq(ctx, SLACK_API+path, fieldname, name, values, r)
if err != nil {
return err
}
req = req.WithContext(ctx)
2018-08-09 15:38:19 -07:00
resp, err := client.Do(req)
2016-09-05 07:34:37 -07:00
if err != nil {
return err
}
defer resp.Body.Close()
2016-11-05 16:07:24 -07:00
2018-01-08 13:41:38 -08: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 16:07:24 -07:00
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
2018-01-08 13:41:38 -08:00
if resp.StatusCode != http.StatusOK {
2016-11-05 16:07:24 -07:00
logResponse(resp, debug)
2018-08-09 15:38:19 -07:00
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
2016-11-05 16:07:24 -07:00
}
2018-08-09 15:38:19 -07:00
return parseResponseBody(resp.Body, intf, debug)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
func doPost(ctx context.Context, client HTTPRequester, req *http.Request, intf interface{}, debug bool) error {
2017-07-16 05:29:46 -07:00
req = req.WithContext(ctx)
2018-08-09 15:38:19 -07:00
resp, err := client.Do(req)
2016-09-05 07:34:37 -07:00
if err != nil {
return err
}
defer resp.Body.Close()
2018-01-08 13:41:38 -08: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 05:29:46 -07:00
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
2018-01-08 13:41:38 -08:00
if resp.StatusCode != http.StatusOK {
2017-07-16 05:29:46 -07:00
logResponse(resp, debug)
2018-08-09 15:38:19 -07:00
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
}
return parseResponseBody(resp.Body, intf, debug)
}
// post JSON.
func postJSON(ctx context.Context, client HTTPRequester, endpoint, token string, json []byte, intf interface{}, debug bool) error {
reqBody := bytes.NewBuffer(json)
req, err := http.NewRequest("POST", endpoint, reqBody)
if err != nil {
return err
2017-07-16 05:29:46 -07:00
}
2018-08-09 15:38:19 -07:00
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return doPost(ctx, client, req, intf, debug)
}
2017-07-16 05:29:46 -07:00
2018-08-09 15:38:19 -07:00
// post a url encoded form.
func postForm(ctx context.Context, client HTTPRequester, 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")
return doPost(ctx, client, req, intf, debug)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
// post to a slack web method.
func postSlackMethod(ctx context.Context, client HTTPRequester, path string, values url.Values, intf interface{}, debug bool) error {
return postForm(ctx, client, SLACK_API+path, values, intf, debug)
2016-09-05 07:34:37 -07:00
}
2018-08-09 15:38:19 -07:00
func parseAdminResponse(ctx context.Context, client HTTPRequester, method string, teamName string, values url.Values, intf interface{}, debug bool) error {
2016-09-05 07:34:37 -07:00
endpoint := fmt.Sprintf(SLACK_WEB_API_FORMAT, teamName, method, time.Now().Unix())
2018-08-09 15:38:19 -07:00
return postForm(ctx, client, endpoint, values, intf, debug)
2016-09-05 07:34:37 -07:00
}
2016-11-05 16:07:24 -07: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 05:29:46 -07:00
logger.Print(string(text))
2016-11-05 16:07:24 -07:00
}
return nil
}
2017-07-16 05:29:46 -07:00
2018-08-09 15:38:19 -07: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 05:29:46 -07:00
2018-08-09 15:38:19 -07:00
func (t errorString) Error() string {
return string(t)
2017-07-16 05:29:46 -07:00
}
2018-08-09 15:38:19 -07: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 05:29:46 -07:00
}