Documentation
¶
Index ¶
- 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 DownloadOptions
- type DownloadProgressCallback
- type DownloadResult
- type FileData
- type FormData
- type HTTPError
- type Request
- 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 WithCookieValue(name, value string) RequestOption
- func WithCookies(cookies []*http.Cookie) RequestOption
- func WithFile(fieldName, filename string, content []byte) 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 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 Response
- func Delete(url string, options ...RequestOption) (*Response, error)
- func Get(url string, options ...RequestOption) (*Response, error)
- func Head(url string, options ...RequestOption) (*Response, error)
- func Options(url string, options ...RequestOption) (*Response, error)
- func Patch(url string, options ...RequestOption) (*Response, error)
- func Post(url string, options ...RequestOption) (*Response, error)
- func Put(url string, options ...RequestOption) (*Response, error)
- func (r *Response) GetCookie(name string) *http.Cookie
- func (r *Response) HasCookie(name string) bool
- func (r *Response) IsClientError() bool
- func (r *Response) IsRedirect() bool
- func (r *Response) IsServerError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) JSON(v any) error
- func (r *Response) SaveToFile(filePath string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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.
func FormatBytes ¶
FormatBytes formats bytes in human-readable format (e.g., "1.50 KB", "2.30 MB")
func FormatSpeed ¶
FormatSpeed formats speed in human-readable format (e.g., "1.50 KB/s", "2.30 MB/s")
func NewCookieJar ¶
func SetDefaultClient ¶
func ValidateConfig ¶ added in v1.1.0
ValidateConfig validates the configuration with reasonable limits.
Types ¶
type Client ¶
type Client interface {
// HTTP methods
Get(url string, options ...RequestOption) (*Response, error)
Post(url string, options ...RequestOption) (*Response, error)
Put(url string, options ...RequestOption) (*Response, error)
Patch(url string, options ...RequestOption) (*Response, error)
Delete(url string, options ...RequestOption) (*Response, error)
Head(url string, options ...RequestOption) (*Response, error)
Options(url string, options ...RequestOption) (*Response, error)
// Generic request method
Request(ctx context.Context, method, url string, options ...RequestOption) (*Response, error)
// File download
DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
// Client management
Close() error
}
Client represents the HTTP client interface. All methods are safe for concurrent use by multiple goroutines.
func NewMinimal ¶ added in v1.1.0
func NewPerformance ¶ added in v1.1.0
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
EnableHTTP2 bool
EnableCookies bool
}
Config defines the HTTP client configuration. Config should be treated as immutable after passing it to New().
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 DownloadOptions ¶
type DownloadOptions struct {
FilePath string
ProgressCallback DownloadProgressCallback
Overwrite bool
ResumeDownload bool
}
func DefaultDownloadOptions ¶
func DefaultDownloadOptions(filePath string) *DownloadOptions
type DownloadResult ¶
type DownloadResult struct {
FilePath string
BytesWritten int64
Duration time.Duration
AverageSpeed float64
StatusCode int
ContentLength int64
Resumed bool
}
func DownloadFile ¶
func DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
func DownloadWithOptions ¶
func DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
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 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 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 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 Response ¶
type Response struct {
StatusCode int
Status string
Headers http.Header // Deep-copied for thread safety
Body string
RawBody []byte
ContentLength int64
Duration time.Duration
Attempts int
Cookies []*http.Cookie
}
Response represents an HTTP response. Response objects are safe to read from multiple goroutines after they are returned. The Headers map is deep-copied to prevent concurrent access issues.
func Delete ¶
func Delete(url string, options ...RequestOption) (*Response, error)
Delete executes a DELETE request using the default client
func Get ¶
func Get(url string, options ...RequestOption) (*Response, error)
Get executes a GET request using the default client
func Head ¶
func Head(url string, options ...RequestOption) (*Response, error)
Head executes a HEAD request using the default client
func Options ¶
func Options(url string, options ...RequestOption) (*Response, error)
Options executes an OPTIONS request using the default client
func Patch ¶
func Patch(url string, options ...RequestOption) (*Response, error)
Patch executes a PATCH request using the default client
func Post ¶
func Post(url string, options ...RequestOption) (*Response, error)
Post executes a POST request using the default client
func Put ¶
func Put(url string, options ...RequestOption) (*Response, error)
Put executes a PUT request using the default client
func (*Response) GetCookie ¶
GetCookie returns the named cookie from the response or nil if not found
func (*Response) HasCookie ¶
HasCookie returns true if the response contains a cookie with the given name
func (*Response) IsClientError ¶
IsClientError returns true if the response status code indicates a client error (4xx)
func (*Response) IsRedirect ¶
IsRedirect returns true if the response status code indicates a redirect (3xx)
func (*Response) IsServerError ¶
IsServerError returns true if the response status code indicates a server error (5xx)
func (*Response) IsSuccess ¶
IsSuccess returns true if the response status code indicates success (2xx)