fetch

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2023 License: AGPL-3.0 Imports: 31 Imported by: 4

Documentation

Overview

Package fetch the http resource

Index

Constants

View Source
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

View Source
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

func AddRoundRobinProxy(u string, proxyURLs ...string)

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

func DefaultTemplateFuncMap() template.FuncMap

DefaultTemplateFuncMap The default template function map

func RoundRobinProxy

func RoundRobinProxy(req *http.Request) (*url.URL, error)

RoundRobinProxy returns a proxy URL on specific request.

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

func NewFetcher

func NewFetcher(opt Options) Fetch

NewFetcher returns a new Fetch instance

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

func NewRequest(method, u string, body any, headers map[string]string) (*Request, error)

NewRequest returns a new Request given a method, URL, optional body, optional headers.

func NewTemplateRequest

func NewTemplateRequest(funcs template.FuncMap, tpl string, arg any) (*Request, error)

NewTemplateRequest returns a new Request given a http template with argument.

func (*Request) Cancel

func (r *Request) Cancel()

Cancel request.

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

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

type Response struct {
	*http.Response

	// Response Body
	Body []byte
}

Response type wraps http.Response

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType returns Response Header Content type

func (*Response) String

func (r *Response) String() string

String returns Response string Body

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL