Skip to content

Commit

Permalink
feat: ✨ httpsign: newclient with timeout (#12)
Browse files Browse the repository at this point in the history
* feat: ✨ httpsign: newclient with timeout

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

* refactor: ♻️ rename option

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

* test: ✅ httpsign: add option test

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

---------

Signed-off-by: thanhpp <[email protected]>
  • Loading branch information
thanhpp authored Apr 22, 2023
1 parent d33901a commit d531f50
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
17 changes: 17 additions & 0 deletions pkg/httpsign/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package httpsign

import (
"net/http"
"time"
)

type Option func(c *http.Client)

func WithTimeout(timeout time.Duration) Option {
return func(c *http.Client) {
if timeout == 0 {
return
}
c.Timeout = timeout
}
}
14 changes: 12 additions & 2 deletions pkg/httpsign/signclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import (
"net/http"
)

func NewClient(transport http.RoundTripper, key string, secret []byte) *http.Client {
return &http.Client{
func NewClient(transport http.RoundTripper, key string, secret []byte, opts ...Option) *http.Client {
c := &http.Client{
Transport: NewTransport(transport, key, secret),
}

for i := range opts {
if opts[i] == nil {
continue
}

opts[i](c)
}

return c
}

func NewTransport(inner http.RoundTripper, key string, secret []byte) HTTPSign {
Expand Down
11 changes: 11 additions & 0 deletions pkg/httpsign/signclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"io"
"net/http"
"testing"
"time"

"github.com/KyberNetwork/tradinglib/pkg/httpsign"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,3 +26,12 @@ func TestRequest(t *testing.T) {
require.NoError(t, err)
t.Log(string(out))
}

func TestOption(t *testing.T) {
timeout := time.Second * 3

client := httpsign.NewClient(
http.DefaultTransport, "..", []byte(".."), httpsign.WithTimeout(timeout),
)
assert.Equal(t, client.Timeout, timeout)
}

0 comments on commit d531f50

Please sign in to comment.