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
- 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 interface{}) 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 interface{}) RequestOption
- func WithJSONAccept() RequestOption
- func WithMaxRetries(maxRetries int) RequestOption
- func WithQuery(key string, value interface{}) RequestOption
- func WithQueryMap(params map[string]interface{}) RequestOption
- func WithText(content string) RequestOption
- func WithTimeout(timeout time.Duration) RequestOption
- func WithUserAgent(userAgent string) RequestOption
- func WithXML(data interface{}) RequestOption
- func WithXMLAccept() RequestOption
- type Response
- func Delete(url string, options ...RequestOption) (*Response, error)
- func Do(ctx context.Context, method, 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 interface{}) error
- func (r *Response) SaveToFile(filePath string) error
- func (r *Response) XML(v interface{}) error
- type SecurityLevel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseDefaultClient ¶
func CloseDefaultClient() error
CloseDefaultClient closes the default client and releases its resources
func FormatBytes ¶
func FormatSpeed ¶
func NewCookieJar ¶
NewCookieJar creates a new cookie jar with default options
func SetDefaultClient ¶
SetDefaultClient sets the default client used by package-level functions
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 methods
DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
DownloadFileWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
// Client management
Close() error
}
Client represents the public HTTP client interface
type Config ¶
type Config struct {
// Network settings
Timeout time.Duration
DialTimeout time.Duration
KeepAlive time.Duration
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
IdleConnTimeout time.Duration
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
ProxyURL string
// Security settings
TLSConfig *tls.Config
MinTLSVersion uint16
MaxTLSVersion uint16
InsecureSkipVerify bool
MaxResponseBodySize int64
MaxConcurrentRequests int
ValidateURL bool
ValidateHeaders bool
AllowPrivateIPs bool // Allow requests to private/internal IPs (for testing)
// Retry settings
MaxRetries int
RetryDelay time.Duration
MaxRetryDelay time.Duration
BackoffFactor float64
Jitter bool
// Headers and features
UserAgent string
Headers map[string]string
FollowRedirects bool
EnableHTTP2 bool
// Cookie settings
CookieJar http.CookieJar // Custom cookie jar, if nil and EnableCookies is true, a default jar will be created
EnableCookies bool // Enable automatic cookie management
}
Config represents the unified client configuration
func ConfigPreset ¶
func ConfigPreset(level SecurityLevel) *Config
ConfigPreset creates a configuration with predefined security settings
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a configuration with secure and optimized defaults
type DownloadOptions ¶
type DownloadOptions struct {
// FilePath is the destination file path (required)
FilePath string
// ProgressCallback is called periodically to report download progress
ProgressCallback DownloadProgressCallback
// ProgressInterval is how often to call the progress callback (default: 500ms)
ProgressInterval time.Duration
// BufferSize is the size of the buffer used for copying data (default: 32KB)
BufferSize int
// CreateDirs creates parent directories if they don't exist (default: true)
CreateDirs bool
// Overwrite allows overwriting existing files (default: false)
Overwrite bool
// ResumeDownload attempts to resume partial downloads using Range requests (default: false)
ResumeDownload bool
// FileMode is the permission mode for the created file (default: 0644)
FileMode os.FileMode
}
DownloadOptions configures file download behavior
func DefaultDownloadOptions ¶
func DefaultDownloadOptions(filePath string) *DownloadOptions
type DownloadProgressCallback ¶
DownloadProgressCallback is called periodically during download to report progress
type DownloadResult ¶
type DownloadResult struct {
FilePath string
BytesWritten int64
Duration time.Duration
AverageSpeed float64 // bytes per second
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)
func DownloadWithOptions ¶
func DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)
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 // Cookies to send with this request
}
Request represents an HTTP request configuration
type RequestOption ¶
type RequestOption func(*Request)
RequestOption defines a function that modifies a request
func WithBasicAuth ¶
func WithBasicAuth(username, password string) RequestOption
WithBasicAuth sets basic authentication
func WithBearerToken ¶
func WithBearerToken(token string) RequestOption
WithBearerToken sets bearer token authentication
func WithBinary ¶
func WithBinary(data []byte, contentType ...string) RequestOption
WithBinary sets the request body as binary data with optional content type
func WithContentType ¶
func WithContentType(contentType string) RequestOption
WithContentType sets the Content-Type header
func WithContext ¶
func WithContext(ctx context.Context) RequestOption
WithContext sets the request context
func WithCookie ¶
func WithCookie(cookie *http.Cookie) RequestOption
WithCookie adds a cookie to the request
func WithCookieValue ¶
func WithCookieValue(name, value string) RequestOption
WithCookieValue adds a simple cookie with the given name and value to the request. This is a convenience method for simple cookies without additional attributes. For cookies with attributes (Path, Domain, Secure, etc.), use WithCookie instead.
Example:
client.Get(url, httpc.WithCookieValue("session_id", "abc123"))
func WithCookies ¶
func WithCookies(cookies []*http.Cookie) RequestOption
WithCookies adds multiple cookies to the request
func WithFile ¶
func WithFile(fieldName, filename string, content []byte) RequestOption
WithFile uploads a single file
func WithForm ¶
func WithForm(data map[string]string) RequestOption
WithForm sets the request body as form data and sets appropriate Content-Type
func WithFormData ¶
func WithFormData(data *FormData) RequestOption
WithFormData sets the request body as multipart form data
func WithHeaderMap ¶
func WithHeaderMap(headers map[string]string) RequestOption
WithHeaderMap sets multiple request headers
func WithJSON ¶
func WithJSON(data interface{}) RequestOption
WithJSON sets the request body as JSON and sets appropriate Content-Type
func WithJSONAccept ¶
func WithJSONAccept() RequestOption
WithJSONAccept sets Accept header to application/json
func WithMaxRetries ¶
func WithMaxRetries(maxRetries int) RequestOption
WithMaxRetries sets the maximum number of retries
func WithQuery ¶
func WithQuery(key string, value interface{}) RequestOption
WithQuery sets a single query parameter
func WithQueryMap ¶
func WithQueryMap(params map[string]interface{}) RequestOption
WithQueryMap sets multiple query parameters from a map
func WithText ¶
func WithText(content string) RequestOption
WithText sets the request body as plain text and sets appropriate Content-Type
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RequestOption
WithTimeout sets the request timeout
func WithUserAgent ¶
func WithUserAgent(userAgent string) RequestOption
WithUserAgent sets the User-Agent header
func WithXML ¶
func WithXML(data interface{}) RequestOption
WithXML sets the request body as XML and sets appropriate Content-Type
func WithXMLAccept ¶
func WithXMLAccept() RequestOption
WithXMLAccept sets Accept header to application/xml
type Response ¶
type Response struct {
StatusCode int
Status string
Headers http.Header
Body string
RawBody []byte
ContentLength int64
Proto string
Duration time.Duration
Attempts int
Request interface{} // *http.Request
Response interface{} // *http.Response
Cookies []*http.Cookie
}
Response represents an HTTP response with enhanced functionality
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)
func (*Response) SaveToFile ¶
SaveToFile saves the response body to a file.
type SecurityLevel ¶
type SecurityLevel int
SecurityLevel defines the security level for client configuration
const ( // SecurityLevelPermissive allows more relaxed security settings SecurityLevelPermissive SecurityLevel = iota // SecurityLevelBalanced provides a balance between security and compatibility SecurityLevelBalanced // SecurityLevelStrict enforces strict security settings SecurityLevelStrict )
Directories
¶
| Path | Synopsis |
|---|---|
|
01_quickstart
command
|
|
|
02_core_features
command
|
|
|
03_advanced
command
|
|
|
04_real_world
command
|
|
|
internal
|
|