summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nlopes/slack/misc.go
diff options
context:
space:
mode:
authorPatrick Connolly <patrick.c.connolly@gmail.com>2018-12-02 02:55:35 +0800
committerWim <wim@42.be>2018-12-01 19:55:35 +0100
commite538a4d30459e9da1a021400b5de85894b4efabc (patch)
treeead4f82d5459fe0d3465f7e3656243cd613c07a6 /vendor/github.com/nlopes/slack/misc.go
parentf94c2b40a3e8eb45054bda1b71eb6fd3f9bc9e6d (diff)
downloadmatterbridge-msglm-e538a4d30459e9da1a021400b5de85894b4efabc.tar.gz
matterbridge-msglm-e538a4d30459e9da1a021400b5de85894b4efabc.tar.bz2
matterbridge-msglm-e538a4d30459e9da1a021400b5de85894b4efabc.zip
Update nlopes/slack to 4.1-dev (#595)
Diffstat (limited to 'vendor/github.com/nlopes/slack/misc.go')
-rw-r--r--vendor/github.com/nlopes/slack/misc.go114
1 files changed, 70 insertions, 44 deletions
diff --git a/vendor/github.com/nlopes/slack/misc.go b/vendor/github.com/nlopes/slack/misc.go
index 69103384..30ae4628 100644
--- a/vendor/github.com/nlopes/slack/misc.go
+++ b/vendor/github.com/nlopes/slack/misc.go
@@ -19,6 +19,7 @@ import (
"time"
)
+// SlackResponse handles parsing out errors from the web api.
type SlackResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
@@ -64,47 +65,31 @@ func (e *RateLimitedError) Error() string {
return fmt.Sprintf("Slack rate limit exceeded, retry after %s", e.RetryAfter)
}
-func fileUploadReq(ctx context.Context, path, fieldname, filename string, values url.Values, r io.Reader) (*http.Request, error) {
- body := &bytes.Buffer{}
- wr := multipart.NewWriter(body)
+func fileUploadReq(ctx context.Context, path string, values url.Values, r io.Reader) (*http.Request, error) {
+ req, err := http.NewRequest("POST", path, r)
- ioWriter, err := wr.CreateFormFile(fieldname, filename)
- if err != nil {
- wr.Close()
- return nil, err
- }
- _, err = io.Copy(ioWriter, r)
- 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)
req = req.WithContext(ctx)
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 {
+func parseResponseBody(body io.ReadCloser, intf interface{}, d debug) 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))
+ if d.Debug() {
+ d.Debugln("parseResponseBody", string(response))
}
return json.Unmarshal(response, intf)
}
-func postLocalWithMultipartResponse(ctx context.Context, client HTTPRequester, path, fpath, fieldname string, values url.Values, intf interface{}, debug bool) error {
+func postLocalWithMultipartResponse(ctx context.Context, client httpClient, path, fpath, fieldname string, values url.Values, intf interface{}, d debug) error {
fullpath, err := filepath.Abs(fpath)
if err != nil {
return err
@@ -114,16 +99,38 @@ func postLocalWithMultipartResponse(ctx context.Context, client HTTPRequester, p
return err
}
defer file.Close()
- return postWithMultipartResponse(ctx, client, path, filepath.Base(fpath), fieldname, values, file, intf, debug)
+ return postWithMultipartResponse(ctx, client, path, filepath.Base(fpath), fieldname, values, file, intf, d)
}
-func postWithMultipartResponse(ctx context.Context, client HTTPRequester, 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)
+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)
if err != nil {
return err
}
+ req.Header.Add("Content-Type", wr.FormDataContentType())
req = req.WithContext(ctx)
resp, err := client.Do(req)
+
if err != nil {
return err
}
@@ -139,14 +146,18 @@ func postWithMultipartResponse(ctx context.Context, client HTTPRequester, path,
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
if resp.StatusCode != http.StatusOK {
- logResponse(resp, debug)
+ logResponse(resp, d)
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
}
-
- return parseResponseBody(resp.Body, intf, debug)
+ select {
+ case err = <-errc:
+ return err
+ default:
+ return parseResponseBody(resp.Body, intf, d)
+ }
}
-func doPost(ctx context.Context, client HTTPRequester, req *http.Request, intf interface{}, debug bool) error {
+func doPost(ctx context.Context, client httpClient, req *http.Request, intf interface{}, d debug) error {
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
@@ -164,15 +175,15 @@ func doPost(ctx context.Context, client HTTPRequester, req *http.Request, intf i
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
if resp.StatusCode != http.StatusOK {
- logResponse(resp, debug)
+ logResponse(resp, d)
return statusCodeError{Code: resp.StatusCode, Status: resp.Status}
}
- return parseResponseBody(resp.Body, intf, debug)
+ return parseResponseBody(resp.Body, intf, d)
}
// post JSON.
-func postJSON(ctx context.Context, client HTTPRequester, endpoint, token string, json []byte, intf interface{}, debug bool) error {
+func postJSON(ctx context.Context, client httpClient, endpoint, token string, json []byte, intf interface{}, d debug) error {
reqBody := bytes.NewBuffer(json)
req, err := http.NewRequest("POST", endpoint, reqBody)
if err != nil {
@@ -180,38 +191,53 @@ func postJSON(ctx context.Context, client HTTPRequester, endpoint, token string,
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
- return doPost(ctx, client, req, intf, debug)
+ return doPost(ctx, client, req, intf, d)
}
// post a url encoded form.
-func postForm(ctx context.Context, client HTTPRequester, endpoint string, values url.Values, intf interface{}, debug bool) error {
+func postForm(ctx context.Context, client httpClient, endpoint string, values url.Values, intf interface{}, d debug) 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)
+ return doPost(ctx, client, req, intf, d)
}
// 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)
+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)
}
-func parseAdminResponse(ctx context.Context, client HTTPRequester, method string, teamName string, values url.Values, intf interface{}, debug bool) error {
- endpoint := fmt.Sprintf(SLACK_WEB_API_FORMAT, teamName, method, time.Now().Unix())
- return postForm(ctx, client, endpoint, values, intf, debug)
+// 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)
}
-func logResponse(resp *http.Response, debug bool) error {
- if debug {
+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() {
text, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
-
- logger.Print(string(text))
+ d.Debugln(string(text))
}
return nil