fetcher

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package fetcher provides an HTTP client with header rotation, proxy support, cookie handling, and retry logic. It produces unified Response objects that integrate with the parser for immediate HTML extraction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fetcher

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

Fetcher is an HTTP client with browser-like header rotation and retry logic.

func New

func New(opts ...Option) (*Fetcher, error)

New creates a new Fetcher with the given options.

func (*Fetcher) Close

func (f *Fetcher) Close()

Close releases resources held by the fetcher.

func (*Fetcher) Do

func (f *Fetcher) Do(ctx context.Context, method, rawURL string, body io.Reader) (*Response, error)

Do performs an HTTP request with retry logic and header rotation. It retries on network errors and HTTP 5xx responses. For POST/PUT/PATCH requests with a body, the body is buffered so it can be replayed on retries.

func (*Fetcher) Get

func (f *Fetcher) Get(ctx context.Context, rawURL string) (*Response, error)

Get performs an HTTP GET request.

func (*Fetcher) Post

func (f *Fetcher) Post(ctx context.Context, rawURL string, body io.Reader) (*Response, error)

Post performs an HTTP POST request.

func (*Fetcher) SetCookies

func (f *Fetcher) SetCookies(rawURL string, cookies []*http.Cookie) error

SetCookies sets cookies for a given URL.

type Option

type Option func(*Fetcher)

Option configures a Fetcher.

func WithHeaders

func WithHeaders(headers http.Header) Option

WithHeaders sets custom HTTP headers.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries.

func WithProxy

func WithProxy(proxyURL string) Option

WithProxy sets the proxy URL.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the HTTP request timeout.

type Response

type Response struct {
	URL        string
	StatusCode int
	Headers    http.Header
	Body       []byte
	Encoding   string
	Cookies    []*http.Cookie
}

Response wraps an HTTP response with parsed HTML body access.

func (*Response) ContentLength

func (r *Response) ContentLength() int

ContentLength returns the Content-Length header as int, or -1 if not set or if the value overflows.

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType returns the Content-Type header value.

func (*Response) Cookie

func (r *Response) Cookie(name string) *http.Cookie

Cookie returns a specific cookie by name, or nil if not found.

func (*Response) OK

func (r *Response) OK() bool

OK returns true if the status code is 2xx.

func (*Response) Parse

func (r *Response) Parse() (*parser.Adaptable, error)

Parse parses the response body as HTML and returns the root Adaptable node.

func (*Response) Text

func (r *Response) Text() string

Text returns the response body as a string.

Jump to

Keyboard shortcuts

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