Skip to content

Commit

Permalink
Improvements to bindings.dingtalk.webhook (dapr#1821)
Browse files Browse the repository at this point in the history
* Improvements to bindings.dingtalk.webhook

Spin-off from PR adding contexts to input bindings

Signed-off-by: ItalyPaleAle <[email protected]>

* Should not have been added

Signed-off-by: ItalyPaleAle <[email protected]>

Co-authored-by: Bernd Verst <[email protected]>
Co-authored-by: Dapr Bot <[email protected]>
  • Loading branch information
3 people authored and cmendible committed Jul 4, 2022
1 parent a05eba5 commit 2e344fc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
14 changes: 5 additions & 9 deletions bindings/alicloud/dingtalk/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -54,7 +53,7 @@ type outgoingWebhook struct {
handler bindings.Handler
}

var webhooks = struct { // nolint: gochecknoglobals
var webhooks = struct { //nolint: gochecknoglobals
sync.RWMutex
m map[string]*outgoingWebhook
}{m: make(map[string]*outgoingWebhook)}
Expand Down Expand Up @@ -176,20 +175,17 @@ func (t *DingTalkWebhook) sendMessage(ctx context.Context, req *bindings.InvokeR
return fmt.Errorf("dingtalk webhook error: post failed. %w", err)
}
defer func() {
// Drain before closing
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("dingtalk webhook error: post failed. status:%d", resp.StatusCode)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("dingtalk webhook error: read body failed. %w", err)
}

var rst webhookResult
err = json.Unmarshal(data, &rst)
err = json.NewDecoder(resp.Body).Decode(&rst)
if err != nil {
return fmt.Errorf("dingtalk webhook error: unmarshal body failed. %w", err)
}
Expand All @@ -206,7 +202,7 @@ func getPostURL(urlPath, secret string) (string, error) {
return urlPath, nil
}

timestamp := strconv.FormatInt(time.Now().Unix()*1000, 10)
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
sign, err := sign(secret, timestamp)
if err != nil {
return urlPath, err
Expand Down
4 changes: 2 additions & 2 deletions bindings/alicloud/dingtalk/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestPublishMsg(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)

req := &bindings.InvokeRequest{Data: []byte(msg), Operation: bindings.CreateOperation, Metadata: map[string]string{}}
_, err = d.Invoke(context.TODO(), req)
_, err = d.Invoke(context.Background(), req)
require.NoError(t, err)
}

Expand Down Expand Up @@ -95,7 +95,7 @@ func TestBindingReadAndInvoke(t *testing.T) { //nolint:paralleltest
require.NoError(t, err)

req := &bindings.InvokeRequest{Data: []byte(msg), Operation: bindings.GetOperation, Metadata: map[string]string{}}
_, err = d.Invoke(context.TODO(), req)
_, err = d.Invoke(context.Background(), req)
require.NoError(t, err)

select {
Expand Down

0 comments on commit 2e344fc

Please sign in to comment.