init
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package alistsdk
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_USERAGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"
|
||||
DEFAULT_TIMEOUT = 30
|
||||
)
|
||||
|
||||
func do(method string, url string, body io.Reader, token string, options *RequestOptions) ([]byte, error) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(func() int {
|
||||
if options.Timeout > 0 {
|
||||
return options.Timeout
|
||||
} else {
|
||||
return DEFAULT_TIMEOUT
|
||||
}
|
||||
}()) * time.Second,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.Insecure},
|
||||
},
|
||||
}
|
||||
request, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
request.Header.Set("Authorization", token)
|
||||
request.Header.Set("User-Agent", DEFAULT_USERAGENT)
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
if options.RetryTimes < options.MaxRetryTimes {
|
||||
options.RetryTimes++
|
||||
time.Sleep(time.Duration(options.RetryIntervalSecond) * time.Second)
|
||||
return do(method, url, body, token, options) // retry
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
Reference in New Issue
Block a user