fetcher

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fetcher

type Fetcher interface {
	// FetchURLAsString fetches the content of a URL as a string.
	FetchURLAsString(ctx context.Context, url string) (string, error)
	// FetchURL fetches the content of a URL as a byte slice.
	FetchURL(ctx context.Context, url string) ([]byte, error)
	// StreamURL streams the content of a URL to the provided writer.
	StreamURL(ctx context.Context, url string, writer io.Writer) error
}

func FromConfig

func FromConfig(cfg FetcherConfig) Fetcher

type FetcherConfig

type FetcherConfig struct {
	// UserAgent is the user agent string to use for HTTP requests
	UserAgent string `toml:"user_agent"`
	// Timeout for HTTP requests
	Timeout time.Duration `toml:"timeout"`
	// RateLimit is the time in seconds to wait between requests
	RateLimit float64 `toml:"rate_limit"`
}

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is an interface for HTTP clients.

type HTTPFetcher

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

HTTPFetcher provides utilities for fetching content via HTTP.

func NewHTTPFetcher

func NewHTTPFetcher(opts ...HTTPFetcherOption) *HTTPFetcher

NewHTTPFetcher creates a new HTTP fetcher with the given options.

func (*HTTPFetcher) FetchURL

func (f *HTTPFetcher) FetchURL(ctx context.Context, url string) ([]byte, error)

FetchURL retrieves content from a URL.

func (*HTTPFetcher) FetchURLAsString

func (f *HTTPFetcher) FetchURLAsString(ctx context.Context, url string) (string, error)

FetchURLAsString retrieves content from a URL as a string.

func (*HTTPFetcher) StreamURL added in v0.2.1

func (f *HTTPFetcher) StreamURL(ctx context.Context, url string, writer io.Writer) error

StreamURL streams content from a URL to the provided writer.

type HTTPFetcherOption

type HTTPFetcherOption func(*HTTPFetcher)

HTTPFetcherOption configures an HTTPFetcher.

func WithClient

func WithClient(client HTTPClient) HTTPFetcherOption

WithClient sets a custom HTTP client.

func WithTimeout

func WithTimeout(timeout time.Duration) HTTPFetcherOption

WithTimeout sets the timeout for HTTP requests.

func WithUserAgent

func WithUserAgent(userAgent string) HTTPFetcherOption

WithUserAgent sets the User-Agent header for requests.

type LimitedFetcher

type LimitedFetcher struct {
	Fetcher *HTTPFetcher
	// contains filtered or unexported fields
}

LimitedFetcher wraps an HTTP fetcher with rate limiting.

func NewLimitedFetcher

func NewLimitedFetcher(fetcher *HTTPFetcher, minInterval time.Duration) *LimitedFetcher

NewLimitedFetcher creates a new rate-limited HTTP fetcher.

func (*LimitedFetcher) FetchURL

func (f *LimitedFetcher) FetchURL(ctx context.Context, url string) ([]byte, error)

FetchURL fetches a URL with rate limiting.

func (*LimitedFetcher) FetchURLAsString

func (f *LimitedFetcher) FetchURLAsString(ctx context.Context, url string) (string, error)

FetchURLAsString fetches a URL as a string with rate limiting.

func (*LimitedFetcher) StreamURL added in v0.2.1

func (f *LimitedFetcher) StreamURL(ctx context.Context, url string, writer io.Writer) error

type RateLimiter

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

RateLimiter limits the rate of requests to a domain.

func NewRateLimiter

func NewRateLimiter(minInterval time.Duration) *RateLimiter

NewRateLimiter creates a new rate limiter with the given minimum interval between requests.

func (*RateLimiter) Wait

func (r *RateLimiter) Wait(ctx context.Context, rawURL string) error

Wait blocks until the rate limit allows a request to the given URL.

Jump to

Keyboard shortcuts

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