From d5dd362d53ae60982b8ea5aa325d49d4f7f497c1 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Mon, 24 Sep 2018 12:09:12 +1200 Subject: [PATCH] =?UTF-8?q?feat(api):=20update=20client.Get=20with=20a=20n?= =?UTF-8?q?ew=20timeout=20parameter=20and=20default=E2=80=A6=20(#2297)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(api): update client.Get with a new timeout parameter and default to 5s * fix(api): fix invalid type --- api/http/client/client.go | 15 +++++++++++---- api/http/handler/motd/motd.go | 2 +- api/http/handler/templates/template_list.go | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/api/http/client/client.go b/api/http/client/client.go index 541ec8257..8892b6472 100644 --- a/api/http/client/client.go +++ b/api/http/client/client.go @@ -15,6 +15,7 @@ import ( const ( errInvalidResponseStatus = portainer.Error("Invalid response status (expecting 200)") + defaultHTTPTimeout = 5 ) // HTTPClient represents a client to send HTTP requests. @@ -26,7 +27,7 @@ type HTTPClient struct { func NewHTTPClient() *HTTPClient { return &HTTPClient{ &http.Client{ - Timeout: time.Second * 5, + Timeout: time.Second * time.Duration(defaultHTTPTimeout), }, } } @@ -67,10 +68,16 @@ func (client *HTTPClient) ExecuteAzureAuthenticationRequest(credentials *portain } // Get executes a simple HTTP GET to the specified URL and returns -// the content of the response body. -func Get(url string) ([]byte, error) { +// the content of the response body. Timeout can be specified via the timeout parameter, +// will default to defaultHTTPTimeout if set to 0. +func Get(url string, timeout int) ([]byte, error) { + + if timeout == 0 { + timeout = defaultHTTPTimeout + } + client := &http.Client{ - Timeout: time.Second * 3, + Timeout: time.Second * time.Duration(timeout), } response, err := client.Get(url) diff --git a/api/http/handler/motd/motd.go b/api/http/handler/motd/motd.go index fbe8b5acd..ca279d890 100644 --- a/api/http/handler/motd/motd.go +++ b/api/http/handler/motd/motd.go @@ -16,7 +16,7 @@ type motdResponse struct { func (handler *Handler) motd(w http.ResponseWriter, r *http.Request) { - motd, err := client.Get(portainer.MessageOfTheDayURL) + motd, err := client.Get(portainer.MessageOfTheDayURL, 0) if err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/api/http/handler/templates/template_list.go b/api/http/handler/templates/template_list.go index 24ca93bbd..7e802528f 100644 --- a/api/http/handler/templates/template_list.go +++ b/api/http/handler/templates/template_list.go @@ -26,7 +26,7 @@ func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *ht } } else { var templateData []byte - templateData, err = client.Get(settings.TemplatesURL) + templateData, err = client.Get(settings.TemplatesURL, 0) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err} }