2024-03-19 05:01:53 -04:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Fetch(url string, maxRetryTimes int) (string, error) {
|
|
|
|
retryTime := 0
|
|
|
|
var err error
|
2024-05-23 04:57:19 -04:00
|
|
|
var resp *http.Response
|
2024-03-19 05:01:53 -04:00
|
|
|
for retryTime < maxRetryTimes {
|
2024-05-23 04:57:19 -04:00
|
|
|
resp, err = http.Get(url)
|
2024-03-19 05:01:53 -04:00
|
|
|
if err != nil {
|
|
|
|
retryTime++
|
|
|
|
continue
|
|
|
|
}
|
2024-05-23 04:57:19 -04:00
|
|
|
var data []byte
|
|
|
|
data, err = io.ReadAll(resp.Body)
|
2024-03-19 05:01:53 -04:00
|
|
|
if err != nil {
|
|
|
|
retryTime++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return string(data), err
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|