4
4
"errors"
5
5
"net"
6
6
"net/http"
7
+ "time"
7
8
8
9
resty "github.com/go-resty/resty/v2"
9
10
"github.com/shellhub-io/shellhub/pkg/worker"
@@ -39,10 +40,14 @@ var (
39
40
ErrUnknown = errors .New ("unknown error" )
40
41
)
41
42
42
- func NewClient (opts ... clientOption ) (Client , error ) {
43
- cfg , err := DefaultConfig ()
44
- if err != nil {
45
- return nil , err
43
+ func NewClient (cfg * Config , opts ... clientOption ) (Client , error ) {
44
+ if cfg == nil {
45
+ var err error
46
+
47
+ cfg , err = NewConfigFromEnv ()
48
+ if err != nil {
49
+ return nil , err
50
+ }
46
51
}
47
52
48
53
httpClient := resty .New ()
@@ -65,8 +70,8 @@ func NewClient(opts ...clientOption) (Client, error) {
65
70
// NOTE: Avoid setting a global base URL on the Resty client. Calls to enterprise endpoints
66
71
// will use c.config.EnterpriseBaseURL explicitly when needed.
67
72
httpClient .SetRetryCount (c .config .RetryCount )
68
- httpClient .SetRetryWaitTime (c .config .RetryWaitTime )
69
- httpClient .SetRetryMaxWaitTime (c .config .RetryMaxWaitTime )
73
+ httpClient .SetRetryWaitTime (time . Duration ( c .config .RetryWaitTime ) * time . Second )
74
+ httpClient .SetRetryMaxWaitTime (time . Duration ( c .config .RetryMaxWaitTime ) * time . Second )
70
75
httpClient .AddRetryCondition (func (r * resty.Response , err error ) bool {
71
76
if _ , ok := err .(net.Error ); ok { // if the error is a network error, retry.
72
77
return true
@@ -104,7 +109,6 @@ func NewClient(opts ...clientOption) (Client, error) {
104
109
return c , nil
105
110
}
106
111
107
- // mustWorker panics if [client.worker] is nil.
108
112
func (c * client ) mustWorker () {
109
113
if c .worker == nil {
110
114
panic ("Client does not have any worker" )
0 commit comments