Skip to content

Commit c8285b1

Browse files
committed
fix(client): Allow set global client timeout, and default to 30s
1 parent e4187e8 commit c8285b1

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

v2/api/client.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"os"
1414
"path"
15+
"strconv"
1516
"strings"
1617
"time"
1718
)
@@ -22,6 +23,7 @@ var (
2223
EnvCommandPassword = "KEYFACTOR_PASSWORD"
2324
EnvCommandDomain = "KEYFACTOR_DOMAIN"
2425
EnvCommandAPI = "KEYFACTOR_API_PATH"
26+
EnvCommandTimeout = "KEYFACTOR_TIMEOUT"
2527
DefaultAPIPath = "KeyfactorAPI"
2628
)
2729

@@ -41,6 +43,7 @@ type AuthConfig struct {
4143
Password string
4244
Domain string
4345
APIPath string
46+
Timeout int
4447
}
4548

4649
// NewKeyfactorClient creates a new Keyfactor client instance. A configured Client is returned with methods used to
@@ -51,6 +54,12 @@ func NewKeyfactorClient(auth *AuthConfig) (*Client, error) {
5154
return nil, err
5255
}
5356

57+
if auth.Timeout > 0 {
58+
c.httpClient = &http.Client{Timeout: time.Duration(auth.Timeout) * time.Second}
59+
} else {
60+
c.httpClient = &http.Client{Timeout: MAX_WAIT_SECONDS * time.Second}
61+
}
62+
5463
return c, nil
5564
}
5665

@@ -111,9 +120,21 @@ func loginToKeyfactor(auth *AuthConfig) (*Client, error) {
111120
Headers: headers,
112121
}
113122

123+
timeoutStr := os.Getenv(EnvCommandTimeout)
124+
timeout := MAX_WAIT_SECONDS
125+
if timeoutStr != "" {
126+
//convert to int and check if greater than 0
127+
timeoutInt, err := strconv.Atoi(timeoutStr)
128+
if err == nil && timeoutInt > 0 {
129+
timeout = timeoutInt
130+
}
131+
} else if auth.Timeout > 0 {
132+
timeout = auth.Timeout
133+
}
134+
114135
c := &Client{
115136
Hostname: auth.Hostname,
116-
httpClient: &http.Client{Timeout: 10 * time.Second},
137+
httpClient: &http.Client{Timeout: time.Duration(timeout) * time.Second},
117138
basicAuthString: buildBasicAuthString(auth),
118139
ApiPath: auth.APIPath,
119140
}

0 commit comments

Comments
 (0)