Documentation
¶
Index ¶
- Variables
- func CloseDefaultClient() error
- func FormatBytes(bytes int64) string
- func FormatSpeed(bytesPerSecond float64) string
- func NewCookieJar() (http.CookieJar, error)
- func SetDefaultClient(client Client) error
- func ValidateConfig(cfg *Config) error
- type Client
- type Config
- type DomainClient
- func (dc *DomainClient) ClearCookies()
- func (dc *DomainClient) ClearHeaders()
- func (dc *DomainClient) Close() error
- func (dc *DomainClient) Delete(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) DeleteCookie(name string)
- func (dc *DomainClient) DeleteHeader(key string)
- func (dc *DomainClient) DownloadFile(path string, filePath string, options ...RequestOption) (*DownloadResult, error)
- func (dc *DomainClient) DownloadWithOptions(path string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
- func (dc *DomainClient) Get(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) GetCookie(name string) *http.Cookie
- func (dc *DomainClient) GetCookies() []*http.Cookie
- func (dc *DomainClient) GetHeaders() map[string]string
- func (dc *DomainClient) Head(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) Options(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) Patch(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) Post(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) Put(path string, options ...RequestOption) (*Result, error)
- func (dc *DomainClient) SetCookie(cookie *http.Cookie) error
- func (dc *DomainClient) SetCookies(cookies []*http.Cookie) error
- func (dc *DomainClient) SetHeader(key, value string) error
- func (dc *DomainClient) SetHeaders(headers map[string]string) error
- type DownloadOptions
- type DownloadProgressCallback
- type DownloadResult
- type FileData
- type FormData
- type HTTPError
- type Request
- type RequestInfo
- type RequestMeta
- type RequestOption
- func WithAccept(accept string) RequestOption
- func WithBasicAuth(username, password string) RequestOption
- func WithBearerToken(token string) RequestOption
- func WithBinary(data []byte, contentType ...string) RequestOption
- func WithBody(body any) RequestOption
- func WithContentType(contentType string) RequestOption
- func WithContext(ctx context.Context) RequestOption
- func WithCookie(cookie http.Cookie) RequestOption
- func WithCookieString(cookieString string) RequestOption
- func WithCookieValue(name, value string) RequestOption
- func WithCookies(cookies []http.Cookie) RequestOption
- func WithFile(fieldName, filename string, content []byte) RequestOption
- func WithFollowRedirects(follow bool) RequestOption
- func WithForm(data map[string]string) RequestOption
- func WithFormData(data *FormData) RequestOption
- func WithHeader(key, value string) RequestOption
- func WithHeaderMap(headers map[string]string) RequestOption
- func WithJSON(data any) RequestOption
- func WithJSONAccept() RequestOption
- func WithMaxRedirects(maxRedirects int) RequestOption
- func WithMaxRetries(maxRetries int) RequestOption
- func WithQuery(key string, value any) RequestOption
- func WithQueryMap(params map[string]any) RequestOption
- func WithText(content string) RequestOption
- func WithTimeout(timeout time.Duration) RequestOption
- func WithUserAgent(userAgent string) RequestOption
- func WithXML(data any) RequestOption
- func WithXMLAccept() RequestOption
- type ResponseInfo
- type Result
- func Delete(url string, options ...RequestOption) (*Result, error)
- func Get(url string, options ...RequestOption) (*Result, error)
- func Head(url string, options ...RequestOption) (*Result, error)
- func Options(url string, options ...RequestOption) (*Result, error)
- func Patch(url string, options ...RequestOption) (*Result, error)
- func Post(url string, options ...RequestOption) (*Result, error)
- func Put(url string, options ...RequestOption) (*Result, error)
- func (r *Result) Body() string
- func (r *Result) GetCookie(name string) *http.Cookie
- func (r *Result) GetRequestCookie(name string) *http.Cookie
- func (r *Result) HasCookie(name string) bool
- func (r *Result) HasRequestCookie(name string) bool
- func (r *Result) Html() string
- func (r *Result) IsClientError() bool
- func (r *Result) IsRedirect() bool
- func (r *Result) IsServerError() bool
- func (r *Result) IsSuccess() bool
- func (r *Result) JSON(v any) error
- func (r *Result) RawBody() []byte
- func (r *Result) RequestCookies() []*http.Cookie
- func (r *Result) ResponseCookies() []*http.Cookie
- func (r *Result) SaveToFile(filePath string) error
- func (r *Result) StatusCode() int
- func (r *Result) String() string
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClientClosed is returned when attempting to use a closed client. // This occurs after calling Close() on a client instance. ErrClientClosed = errors.New("client is closed") // ErrNilConfig is returned when a nil configuration is provided. // Always provide a valid Config or use DefaultConfig(). ErrNilConfig = errors.New("config cannot be nil") // ErrInvalidURL is returned when URL validation fails. // URLs must have a valid scheme (http/https) and host. ErrInvalidURL = errors.New("invalid URL") // ErrInvalidHeader is returned when header validation fails. // Headers must not contain control characters or exceed size limits. ErrInvalidHeader = errors.New("invalid header") // ErrInvalidTimeout is returned when timeout is negative or exceeds limits. // Timeout must be between 0 and 30 minutes. ErrInvalidTimeout = errors.New("invalid timeout") // ErrInvalidRetry is returned when retry configuration is invalid. // MaxRetries must be 0-10, BackoffFactor must be 1.0-10.0. ErrInvalidRetry = errors.New("invalid retry configuration") // ErrEmptyFilePath is returned when file path is empty. // Provide a valid file path for download operations. ErrEmptyFilePath = errors.New("file path cannot be empty") // ErrFileExists is returned when file already exists and Overwrite is false. // Set Overwrite=true or ResumeDownload=true in DownloadOptions. ErrFileExists = errors.New("file already exists") // ErrResponseBodyEmpty is returned when attempting to parse empty response body. // Check response.RawBody before calling JSON() or other parsing methods. ErrResponseBodyEmpty = errors.New("response body is empty") // ErrResponseBodyTooLarge is returned when response body exceeds size limit. // Increase MaxResponseBodySize in Config or reduce response size. ErrResponseBodyTooLarge = errors.New("response body too large") )
Functions ¶
func CloseDefaultClient ¶
func CloseDefaultClient() error
CloseDefaultClient closes the default client and resets it. After calling this, the next package-level function call will create a new client. This function is safe for concurrent use.
func FormatBytes ¶
FormatBytes formats bytes in human-readable format (e.g., "1.50 KB", "2.00 MB").
func FormatSpeed ¶
FormatSpeed formats speed in human-readable format (e.g., "1.50 KB/s", "2.00 MB/s").
func NewCookieJar ¶
func SetDefaultClient ¶
SetDefaultClient sets a custom client as the default for package-level functions. The previous default client is closed automatically. Returns an error if the client is nil or not created with httpc.New().
func ValidateConfig ¶ added in v1.1.0
Types ¶
type Client ¶
type Client interface {
Get(url string, options ...RequestOption) (*Result, error)
Post(url string, options ...RequestOption) (*Result, error)
Put(url string, options ...RequestOption) (*Result, error)
Patch(url string, options ...RequestOption) (*Result, error)
Delete(url string, options ...RequestOption) (*Result, error)
Head(url string, options ...RequestOption) (*Result, error)
Options(url string, options ...RequestOption) (*Result, error)
Request(ctx context.Context, method, url string, options ...RequestOption) (*Result, error)
DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
Close() error
}
func NewMinimal ¶ added in v1.1.0
NewMinimal creates a new client with minimal features and lightweight configuration.
func NewPerformance ¶ added in v1.1.0
NewPerformance creates a new client optimized for high-throughput scenarios.
type Config ¶
type Config struct {
Timeout time.Duration
MaxIdleConns int
MaxConnsPerHost int
ProxyURL string
TLSConfig *tls.Config
MinTLSVersion uint16
MaxTLSVersion uint16
InsecureSkipVerify bool
MaxResponseBodySize int64
AllowPrivateIPs bool
StrictContentLength bool
MaxRetries int
RetryDelay time.Duration
BackoffFactor float64
UserAgent string
Headers map[string]string
FollowRedirects bool
MaxRedirects int
EnableHTTP2 bool
EnableCookies bool
}
func DefaultConfig ¶
func DefaultConfig() *Config
func MinimalConfig ¶ added in v1.1.0
func MinimalConfig() *Config
MinimalConfig returns a lightweight configuration with minimal features. Use this for simple, one-off requests where you don't need retries or advanced features.
func PerformanceConfig ¶ added in v1.1.0
func PerformanceConfig() *Config
func SecureConfig ¶ added in v1.1.0
func SecureConfig() *Config
func TestingConfig ¶ added in v1.1.0
func TestingConfig() *Config
TestingConfig returns a configuration optimized for testing environments. WARNING: This config disables security features and should NEVER be used in production. Use this ONLY for local development and testing with localhost/private networks.
type DomainClient ¶ added in v1.3.0
type DomainClient struct {
// contains filtered or unexported fields
}
func NewDomain ¶ added in v1.3.0
func NewDomain(baseURL string, config ...*Config) (*DomainClient, error)
func (*DomainClient) ClearCookies ¶ added in v1.3.0
func (dc *DomainClient) ClearCookies()
func (*DomainClient) ClearHeaders ¶ added in v1.3.0
func (dc *DomainClient) ClearHeaders()
func (*DomainClient) Close ¶ added in v1.3.0
func (dc *DomainClient) Close() error
func (*DomainClient) Delete ¶ added in v1.3.0
func (dc *DomainClient) Delete(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) DeleteCookie ¶ added in v1.3.0
func (dc *DomainClient) DeleteCookie(name string)
func (*DomainClient) DeleteHeader ¶ added in v1.3.0
func (dc *DomainClient) DeleteHeader(key string)
func (*DomainClient) DownloadFile ¶ added in v1.3.5
func (dc *DomainClient) DownloadFile(path string, filePath string, options ...RequestOption) (*DownloadResult, error)
DownloadFile downloads a file from the given path to the specified file path. The path is relative to the domain's base URL or can be a full URL. Automatic state management (cookies/headers) is applied during download.
func (*DomainClient) DownloadWithOptions ¶ added in v1.3.5
func (dc *DomainClient) DownloadWithOptions(path string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
DownloadWithOptions downloads a file with custom download options. The path is relative to the domain's base URL or can be a full URL. Automatic state management (cookies/headers) is applied during download.
func (*DomainClient) Get ¶ added in v1.3.0
func (dc *DomainClient) Get(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) GetCookie ¶ added in v1.3.0
func (dc *DomainClient) GetCookie(name string) *http.Cookie
func (*DomainClient) GetCookies ¶ added in v1.3.0
func (dc *DomainClient) GetCookies() []*http.Cookie
func (*DomainClient) GetHeaders ¶ added in v1.3.0
func (dc *DomainClient) GetHeaders() map[string]string
func (*DomainClient) Head ¶ added in v1.3.0
func (dc *DomainClient) Head(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) Options ¶ added in v1.3.0
func (dc *DomainClient) Options(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) Patch ¶ added in v1.3.0
func (dc *DomainClient) Patch(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) Post ¶ added in v1.3.0
func (dc *DomainClient) Post(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) Put ¶ added in v1.3.0
func (dc *DomainClient) Put(path string, options ...RequestOption) (*Result, error)
func (*DomainClient) SetCookie ¶ added in v1.3.0
func (dc *DomainClient) SetCookie(cookie *http.Cookie) error
func (*DomainClient) SetCookies ¶ added in v1.3.0
func (dc *DomainClient) SetCookies(cookies []*http.Cookie) error
func (*DomainClient) SetHeader ¶ added in v1.3.0
func (dc *DomainClient) SetHeader(key, value string) error
func (*DomainClient) SetHeaders ¶ added in v1.3.0
func (dc *DomainClient) SetHeaders(headers map[string]string) error
type DownloadOptions ¶
type DownloadOptions struct {
FilePath string
ProgressCallback DownloadProgressCallback
Overwrite bool
ResumeDownload bool
}
DownloadOptions configures file download behavior.
func DefaultDownloadOptions ¶
func DefaultDownloadOptions(filePath string) *DownloadOptions
DefaultDownloadOptions creates download options with default settings. Overwrite and ResumeDownload are both false by default.
type DownloadProgressCallback ¶
DownloadProgressCallback is called during file download to report progress. Parameters: downloaded bytes, total bytes, current speed in bytes/second.
type DownloadResult ¶
type DownloadResult struct {
FilePath string
BytesWritten int64
Duration time.Duration
AverageSpeed float64
StatusCode int
ContentLength int64
Resumed bool
}
DownloadResult contains information about a completed download.
func DownloadFile ¶
func DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
DownloadFile downloads a file from the given URL to the specified file path using the default client. Returns DownloadResult with download statistics or an error if the download fails.
func DownloadWithOptions ¶
func DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
DownloadWithOptions downloads a file with custom download options using the default client. Returns DownloadResult with download statistics or an error if the download fails.
type Request ¶
type Request struct {
Method string
URL string
Headers map[string]string
QueryParams map[string]any
Body any
Timeout time.Duration
MaxRetries int
Context context.Context
Cookies []http.Cookie
FollowRedirects *bool // Override client's FollowRedirects setting (nil = use client default)
MaxRedirects *int // Override client's MaxRedirects setting (nil = use client default)
}
type RequestInfo ¶ added in v1.3.0
type RequestMeta ¶ added in v1.3.0
type RequestOption ¶
func WithAccept ¶
func WithAccept(accept string) RequestOption
func WithBasicAuth ¶
func WithBasicAuth(username, password string) RequestOption
func WithBearerToken ¶
func WithBearerToken(token string) RequestOption
func WithBinary ¶
func WithBinary(data []byte, contentType ...string) RequestOption
func WithBody ¶
func WithBody(body any) RequestOption
func WithContentType ¶
func WithContentType(contentType string) RequestOption
func WithContext ¶
func WithContext(ctx context.Context) RequestOption
func WithCookie ¶
func WithCookie(cookie http.Cookie) RequestOption
func WithCookieString ¶ added in v1.3.0
func WithCookieString(cookieString string) RequestOption
func WithCookieValue ¶
func WithCookieValue(name, value string) RequestOption
func WithCookies ¶
func WithCookies(cookies []http.Cookie) RequestOption
func WithFile ¶
func WithFile(fieldName, filename string, content []byte) RequestOption
func WithFollowRedirects ¶ added in v1.3.0
func WithFollowRedirects(follow bool) RequestOption
func WithForm ¶
func WithForm(data map[string]string) RequestOption
func WithFormData ¶
func WithFormData(data *FormData) RequestOption
func WithHeader ¶
func WithHeader(key, value string) RequestOption
func WithHeaderMap ¶
func WithHeaderMap(headers map[string]string) RequestOption
func WithJSON ¶
func WithJSON(data any) RequestOption
func WithJSONAccept ¶
func WithJSONAccept() RequestOption
func WithMaxRedirects ¶ added in v1.3.0
func WithMaxRedirects(maxRedirects int) RequestOption
func WithMaxRetries ¶
func WithMaxRetries(maxRetries int) RequestOption
func WithQuery ¶
func WithQuery(key string, value any) RequestOption
func WithQueryMap ¶
func WithQueryMap(params map[string]any) RequestOption
func WithText ¶
func WithText(content string) RequestOption
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RequestOption
func WithUserAgent ¶
func WithUserAgent(userAgent string) RequestOption
func WithXML ¶
func WithXML(data any) RequestOption
func WithXMLAccept ¶
func WithXMLAccept() RequestOption
type ResponseInfo ¶ added in v1.3.0
type Result ¶ added in v1.3.0
type Result struct {
Request *RequestInfo
Response *ResponseInfo
Meta *RequestMeta
}
func (*Result) GetRequestCookie ¶ added in v1.3.0
func (*Result) HasRequestCookie ¶ added in v1.3.0
func (*Result) IsClientError ¶ added in v1.3.0
IsClientError returns true if the response status code indicates a client error (4xx).
func (*Result) IsRedirect ¶ added in v1.3.0
IsRedirect returns true if the response status code indicates a redirect (3xx).
func (*Result) IsServerError ¶ added in v1.3.0
IsServerError returns true if the response status code indicates a server error (5xx).
func (*Result) IsSuccess ¶ added in v1.3.0
IsSuccess returns true if the response status code indicates success (2xx).
func (*Result) JSON ¶ added in v1.3.0
JSON unmarshals the response body into the provided interface. Returns ErrResponseBodyEmpty if the body is nil or empty. Returns ErrResponseBodyTooLarge if the body exceeds 50MB.