Skip to content

Commit

Permalink
Check HTTP status not response data
Browse files Browse the repository at this point in the history
  • Loading branch information
loganintech committed Feb 23, 2024
1 parent b13d205 commit fb6db35
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 deletions pkg/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ type BaseResponse struct {
Error string `json:"error"`
}

type BaseResponseData interface {
IsOk() bool
ErrorText() string
}

func (b BaseResponse) IsOk() bool {
return b.Ok
}

func (b BaseResponse) ErrorText() string {
return b.Error
}

type Pagination struct {
ResponseMetadata struct {
NextCursor string `json:"next_cursor"`
Expand Down Expand Up @@ -257,7 +244,7 @@ func (r *RateLimitError) Error() string {
return fmt.Sprintf("rate limited, retry after: %s", r.RetryAfter.String())
}

func (c *Client) doRequest(ctx context.Context, url string, res BaseResponseData, method string, payload []byte, values url.Values) error {
func (c *Client) doRequest(ctx context.Context, url string, res interface{}, method string, payload []byte, values url.Values) error {
reqBody := strings.NewReader(values.Encode())

req, err := http.NewRequestWithContext(ctx, method, url, reqBody)
Expand All @@ -278,7 +265,7 @@ func (c *Client) doRequest(ctx context.Context, url string, res BaseResponseData
return err
}

if !res.IsOk() && strings.Contains(res.ErrorText(), "ratelimited") {
if resp.StatusCode == http.StatusTooManyRequests {
retryAfter := resp.Header.Get("Retry-After")
retryAfterSec, err := strconv.Atoi(retryAfter)
if err != nil {
Expand Down

0 comments on commit fb6db35

Please sign in to comment.