I need to make quite a few http GET/POST requests in my script so I moved the request part to a package and added simple retry in case the first request fails.
This is my code:
func Request(req *http.Request, someclient *http.Client, counter int) ([]byte, error) {
resp, httperr := client.Do(req)
if httperr != nil {
switch counter {
case 0:
fmt.Println("Retrying second time...")
counter += 1
return Request(req, client, counter)
case 1:
fmt.Println("Retrying third time...")
counter += 1
return Request(req, client, counter)
case 2:
return nil, fmt.Errorf("Couldn't get a response from remote server: ", httperr)
}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error while reading the response body: ", err)
}
resp.Body.Close()
return body, httperr
}
Since I’m a noob to Go and programming in general, could anybody review my function?
Any pointers on making this more robust or cleaner? Like calling time.Sleep for 2 seconds between each recursive call?