feat(api): update client.Get with a new timeout parameter and default… (#2297)

* feat(api): update client.Get with a new timeout parameter and default to 5s

* fix(api): fix invalid type
pull/2248/merge
Anthony Lapenna 2018-09-24 12:09:12 +12:00 committed by GitHub
parent c3d80a1b21
commit d5dd362d53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 6 deletions

View File

@ -15,6 +15,7 @@ import (
const ( const (
errInvalidResponseStatus = portainer.Error("Invalid response status (expecting 200)") errInvalidResponseStatus = portainer.Error("Invalid response status (expecting 200)")
defaultHTTPTimeout = 5
) )
// HTTPClient represents a client to send HTTP requests. // HTTPClient represents a client to send HTTP requests.
@ -26,7 +27,7 @@ type HTTPClient struct {
func NewHTTPClient() *HTTPClient { func NewHTTPClient() *HTTPClient {
return &HTTPClient{ return &HTTPClient{
&http.Client{ &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 // Get executes a simple HTTP GET to the specified URL and returns
// the content of the response body. // the content of the response body. Timeout can be specified via the timeout parameter,
func Get(url string) ([]byte, error) { // will default to defaultHTTPTimeout if set to 0.
func Get(url string, timeout int) ([]byte, error) {
if timeout == 0 {
timeout = defaultHTTPTimeout
}
client := &http.Client{ client := &http.Client{
Timeout: time.Second * 3, Timeout: time.Second * time.Duration(timeout),
} }
response, err := client.Get(url) response, err := client.Get(url)

View File

@ -16,7 +16,7 @@ type motdResponse struct {
func (handler *Handler) motd(w http.ResponseWriter, r *http.Request) { 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 { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return

View File

@ -26,7 +26,7 @@ func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *ht
} }
} else { } else {
var templateData []byte var templateData []byte
templateData, err = client.Get(settings.TemplatesURL) templateData, err = client.Get(settings.TemplatesURL, 0)
if err != nil { if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err} return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err}
} }