Skip to content

Commit 0fcea48

Browse files
Merge pull request #3 from PraneethChandraThota/request-timeout
LU-42 Go Http Request Timeout
2 parents bb5d37f + 37a16ce commit 0fcea48

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.dylib
77
debug
88
.vscode/
9+
.idea/
910

1011
# Test binary, build with `go test -c`
1112
*.test

examples/exampleconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ func EndpointURL() string {
4040
}
4141

4242
func ProxyURL() string {
43-
return "http://localhost:8888"
43+
return "http://localhost:4433"
4444
}

injectionapi/socketlabsclient.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"net/http"
77
"net/url"
88
"runtime"
9+
"time"
910

1011
"github.com/socketlabs/socketlabs-go/injectionapi/core"
1112
"github.com/socketlabs/socketlabs-go/injectionapi/core/serialization"
1213
"github.com/socketlabs/socketlabs-go/injectionapi/message"
1314
)
1415

1516
const endpointURL = "https://inject.socketlabs.com/api/v1/email"
17+
const requestTimeout = 120
1618

1719
// ISocketlabsClient is used to easily send messages through the Socketlabs Injection API
1820
type ISocketlabsClient interface {
@@ -29,8 +31,14 @@ type ISocketlabsClient interface {
2931
// SetProxyUrl sets the proxy url.
3032
SetProxyURL(proxyURL string)
3133

34+
// SetRequestTimeout sets the timeout.
35+
SetRequestTimeout(timeout int)
36+
3237
// GetEndpointURL retreives the API endpoint.
3338
GetEndpointURL() string
39+
40+
// GetRequestTimeout retreives the timeout.
41+
GetRequestTimeout() int
3442
}
3543

3644
// socketlabsClient is the default ISocketlabsClient implementation
@@ -39,6 +47,7 @@ type socketlabsClient struct {
3947
APIKey string
4048
EndpointURL string
4149
ProxyURL string
50+
RequestTimeout int
4251
}
4352

4453
// CreateClient instatiates new client using the specified credentials
@@ -47,6 +56,7 @@ func CreateClient(serverID int, apiKey string) ISocketlabsClient {
4756
ServerID: serverID,
4857
APIKey: apiKey,
4958
EndpointURL: endpointURL,
59+
RequestTimeout: requestTimeout,
5060
}
5161
}
5262

@@ -57,6 +67,7 @@ func CreateClientWithProxy(serverID int, apiKey string, proxyURL string) ISocket
5767
APIKey: apiKey,
5868
EndpointURL: endpointURL,
5969
ProxyURL: proxyURL,
70+
RequestTimeout: requestTimeout,
6071
}
6172
}
6273

@@ -70,11 +81,21 @@ func (socketlabsClient *socketlabsClient) SetProxyURL(proxyURL string) {
7081
socketlabsClient.ProxyURL = proxyURL
7182
}
7283

84+
// SetRequestTimeout sets the timeout
85+
func (socketlabsClient *socketlabsClient) SetRequestTimeout(timeout int) {
86+
socketlabsClient.RequestTimeout = timeout
87+
}
88+
7389
// GetEndpointURL retreives the API endpoint.
7490
func (socketlabsClient *socketlabsClient) GetEndpointURL() string {
7591
return socketlabsClient.EndpointURL
7692
}
7793

94+
// GetRequestTimeout retreives the timeout
95+
func (socketlabsClient *socketlabsClient) GetRequestTimeout() int {
96+
return socketlabsClient.RequestTimeout
97+
}
98+
7899
// SendBasic sends a basic email message and returns the response from the Injection API.
79100
func (socketlabsClient *socketlabsClient) SendBasic(message *message.BasicMessage) (SendResponse, error) {
80101

@@ -185,7 +206,11 @@ func (socketlabsClient *socketlabsClient) createHttpClient(proxyUrl string) (*ht
185206
return nil, err
186207
}
187208

209+
//converting timeout from int to time
210+
var timeout time.Duration
211+
timeout = time.Duration(socketlabsClient.RequestTimeout) * time.Second
212+
188213
//create client with proxy url
189-
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
214+
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}, Timeout: timeout}
190215
return client, nil
191216
}

0 commit comments

Comments
 (0)