summaryrefslogtreecommitdiff
path: root/internal/http/http.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-19 12:36:48 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-19 12:36:48 +0200
commit5f2e7ef988ac1267fe5038419b19b0154f9d5a84 (patch)
treebab00cad1d76c6572d4eb6023861c834da913dc7 /internal/http/http.go
parent4bf1273c3f446ac3195fb700ec41c7cae7d20ac9 (diff)
API + HTTP: Set the /disconnect timeout to 1 second
Diffstat (limited to 'internal/http/http.go')
-rw-r--r--internal/http/http.go12
1 files changed, 10 insertions, 2 deletions
diff --git a/internal/http/http.go b/internal/http/http.go
index 0ca444d..aa559fe 100644
--- a/internal/http/http.go
+++ b/internal/http/http.go
@@ -18,6 +18,7 @@ type HTTPOptionalParams struct {
Headers http.Header
URLParameters URLParameters
Body url.Values
+ Timeout time.Duration
}
// Construct an URL including on parameters
@@ -77,7 +78,7 @@ func httpOptionalURL(url string, opts *HTTPOptionalParams) (string, error) {
func httpOptionalHeaders(req *http.Request, opts *HTTPOptionalParams) {
// Add headers
- if opts != nil && req != nil {
+ if opts != nil && req != nil && opts.Headers != nil {
for k, v := range opts.Headers {
req.Header.Add(k, v[0])
}
@@ -105,8 +106,15 @@ func HTTPMethodWithOpts(
return nil, nil, urlErr
}
+ // Default timeout is 5 seconds
+ // If a different timeout is given, set it
+ var timeout time.Duration = 5
+ if opts != nil && opts.Timeout > 0 {
+ timeout = opts.Timeout
+ }
+
// Create a client
- client := &http.Client{Timeout: 5 * time.Second}
+ client := &http.Client{Timeout: timeout * time.Second}
errorMessage := fmt.Sprintf("failed HTTP request with method %s and url %s", method, url)