Documentation
¶
Overview ¶
Package fetch the http resource
Index ¶
Constants ¶
const ( // DefaultMaxBodySize fetch.Response default max body size DefaultMaxBodySize int64 = 1024 * 1024 * 1024 // DefaultRetryTimes fetch.Request retry times DefaultRetryTimes = 3 // DefaultTimeout fetch.Request timeout DefaultTimeout = time.Minute )
Variables ¶
var ( // DefaultRetryHTTPCodes retry fetch.Request error status code DefaultRetryHTTPCodes = []int{http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout, http.StatusRequestTimeout} // DefaultHeaders defaults fetch.Request headers DefaultHeaders = map[string]string{ "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;", "User-Agent": fmt.Sprintf("cloudcat/%v", consts.Version), } // ErrRequestCancel fetch.Request cancel error ErrRequestCancel = errors.New("request canceled") )
Functions ¶
func AddRoundRobinProxy ¶
AddRoundRobinProxy add the proxy URLs for the specified URL. The proxy type is determined by the URL scheme. "http", "https" and "socks5" are supported. If the scheme is empty, "http" is assumed.
func DefaultTemplateFuncMap ¶
DefaultTemplateFuncMap The default template function map
Types ¶
type Fetch ¶
type Fetch interface {
// Get issues a GET to the specified URL string and optional headers.
Get(url string, headers map[string]string) (*Response, error)
// Post issues a POST to the specified URL string, body and optional headers.
Post(url string, body any, headers map[string]string) (*Response, error)
// Head issues a HEAD to the specified URL string and optional headers.
Head(url string, headers map[string]string) (*Response, error)
// Request sends request with specified method, url, body, headers; returns an HTTP response.
Request(method, url string, body any, headers map[string]string) (*Response, error)
// DoRequest sends a fetch.Request and returns an HTTP response.
DoRequest(*Request) (*Response, error)
}
Fetch http client interface
type Options ¶
type Options struct {
CharsetDetectDisabled bool `yaml:"charset-detect-disabled"`
MaxBodySize int64 `yaml:"max-body-size"`
RetryTimes int `yaml:"retry-times"`
RetryHTTPCodes []int `yaml:"retry-http-codes"`
Timeout time.Duration `yaml:"timeout"`
CachePolicy cache.Policy `yaml:"cache-policy"`
ProxyFunc func(*http.Request) (*url.URL, error) `yaml:"-"`
}
Options The Fetch instance options
type Request ¶
type Request struct {
*http.Request
// Proxy on this Request
Proxy []string
// Optional response body encoding. Leave empty for automatic detection.
// If you're having issues with auto-detection, set this.
Encoding string
// Set this true to cancel Request. Should be used on middlewares.
Cancelled bool
// contains filtered or unexported fields
}
Request is a small wrapper around *http.Request
func NewRequest ¶
NewRequest returns a new Request given a method, URL, optional body, optional headers.
func NewTemplateRequest ¶
NewTemplateRequest returns a new Request given a http template with argument.
func (*Request) WithContext ¶
WithContext returns a shallow copy of r with its context changed to ctx. The provided ctx must be non-nil.
For outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body.
To create a new request with a context, use NewRequestWithContext. To change the context of a request, such as an incoming request you want to modify before sending back out, use Request.Clone. Between those two uses, it's rare to need WithContext.
type Response ¶
Response type wraps http.Response
func (*Response) ContentType ¶
ContentType returns Response Header Content type