fetch

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFetcher = Default(&Config{RandomUserAgent: true})

Functions

func Fetch

func Fetch(url string) (*http.Response, error)

func Get

func Get(url string, opts ...Option) (*http.Response, error)

func Post

func Post(url string, body io.Reader, opts ...Option) (*http.Response, error)

func Request

func Request(method, url string, body io.Reader, opts ...Option) (*http.Response, error)

func WithJSONBody

func WithJSONBody(v any) io.Reader

func WithURLEncodedBody

func WithURLEncodedBody(query map[string]string) io.Reader

Types

type Config

type Config struct {
	// Set User-Agent Header.
	UserAgent string

	// Set Referer Header.
	Referer string

	// Enable cookies.
	EnableCookies bool

	// Use random User-Agent.
	RandomUserAgent bool

	// Return error when status is not OK.
	RaiseForStatus bool

	// HTTP Request timeout.
	Timeout time.Duration

	// Custom HTTP Transport.
	Transport http.RoundTripper
}

type Context

type Context struct {
	Config
	// contains filtered or unexported fields
}

Context is used for each request.

type Fetcher

type Fetcher struct {
	// contains filtered or unexported fields
}

func Default

func Default(cfg *Config) *Fetcher

func New

func New(c *http.Client, cfg *Config) *Fetcher

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(url string) (resp *http.Response, err error)

func (*Fetcher) Get

func (f *Fetcher) Get(url string, opts ...Option) (resp *http.Response, err error)

func (*Fetcher) Post

func (f *Fetcher) Post(url string, body io.Reader, opts ...Option) (resp *http.Response, err error)

func (*Fetcher) Request

func (f *Fetcher) Request(method, url string, body io.Reader, opts ...Option) (resp *http.Response, err error)

type Option

type Option func(*Context)

func WithAuthorization

func WithAuthorization(token string) Option

func WithBasicAuth

func WithBasicAuth(username, password string) Option

func WithHeader

func WithHeader(key, value string) Option

func WithHeaders

func WithHeaders(headers map[string]string) Option

func WithQuery

func WithQuery(key, value string) Option

func WithQueryMap

func WithQueryMap(query map[string]string) Option

func WithQueryPairs

func WithQueryPairs(kv ...string) Option

func WithRaiseForStatus

func WithRaiseForStatus(v bool) Option

func WithRandomUserAgent

func WithRandomUserAgent() Option

func WithReferer

func WithReferer(referer string) Option

func WithRequest

func WithRequest(fn func(req *http.Request)) Option

func WithUserAgent

func WithUserAgent(ua string) Option

Jump to

Keyboard shortcuts

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