Documentation
¶
Index ¶
- func Connect(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func Delete(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func Get(ctx context.Context, url string, opts ...OptionFunc) (*http.Response, error)
- func GetClient() *http.Client
- func Head(ctx context.Context, url string, opts ...OptionFunc) (*http.Response, error)
- func Options(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func Post(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func PostForm(ctx context.Context, url string, form url.Values, opts ...OptionFunc) (*http.Response, error)
- func PostJSON(ctx context.Context, url string, payload any, opts ...OptionFunc) (*http.Response, error)
- func PostMForm(ctx context.Context, url string, data FormData, opts ...OptionFunc) (*http.Response, error)
- func Put(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func Request(ctx context.Context, method, urlStr string, body []byte, opts ...OptionFunc) (*http.Response, error)
- func SetClient(client *http.Client)
- func Trace(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
- type BackoffFunc
- type File
- type FormData
- type HTTPError
- type Middleware
- type Option
- type OptionFunc
- func WithBackoff(f BackoffFunc) OptionFunc
- func WithForceRetry() OptionFunc
- func WithHeaders(h map[string]string) OptionFunc
- func WithIdleConnTimeout(d time.Duration) OptionFunc
- func WithMaxConnsPerHost(n int) OptionFunc
- func WithMaxIdleConns(n int) OptionFunc
- func WithMiddleware(mw Middleware) OptionFunc
- func WithRetries(n int) OptionFunc
- func WithTimeout(timeout time.Duration) OptionFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
Connect request
func Delete ¶
func Delete(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
Delete request
func Options ¶
func Options(ctx context.Context, url string, body []byte, opts ...OptionFunc) (*http.Response, error)
Options request
func Post ¶
Post wraps a standard POST request ctx: Context for timeout control or cancellation url: Request URL body: Request body byte data opts: Optional configurations (retry, Headers, Middleware, etc.)
func PostForm ¶
func PostForm(ctx context.Context, url string, form url.Values, opts ...OptionFunc) (*http.Response, error)
PostForm sends a form request with application/x-www-form-urlencoded content type
func PostJSON ¶
func PostJSON(ctx context.Context, url string, payload any, opts ...OptionFunc) (*http.Response, error)
PostJSON sends a JSON request Automatically serializes payload and sets Content-Type to application/json
func PostMForm ¶
func PostMForm(ctx context.Context, url string, data FormData, opts ...OptionFunc) (*http.Response, error)
PostMForm supports multipart/form-data uploads, including files and form fields
func Request ¶
func Request(ctx context.Context, method, urlStr string, body []byte, opts ...OptionFunc) (*http.Response, error)
Request core request function, supports retry, backoff, middleware, buffer pool ctx: context, can be used for cancellation or timeout method: HTTP method, such as GET, POST, PUT, etc. urlStr: request URL body: request body content, byte slice opts: optional configuration, including retry count, backoff strategy, headers, middleware, etc.
Types ¶
type BackoffFunc ¶
BackoffFunc defines retry backoff function
type File ¶
type File struct {
FieldName string // Form field name
FileName string // File name, sent to server
File io.Reader // File content, can be *os.File or other io.Reader
}
File defines a single uploaded file structure
type HTTPError ¶
HTTPError custom request error type, contains status code, method, URL and response body
type Middleware ¶
type Middleware func(next func(*http.Request) (*http.Response, error)) func(*http.Request) (*http.Response, error)
Middleware defines middleware type that can execute logic before/after requests
type Option ¶
type Option struct {
Retries int // Maximum number of retries
Backoff BackoffFunc // Retry backoff strategy
Headers map[string]string // Custom request headers
ForceRetry bool // Whether to force retry for all methods
Middlewares []Middleware // Middleware chain
}
Option request configuration structure
type OptionFunc ¶
type OptionFunc func(*Option)
OptionFunc functional configuration type
func WithHeaders ¶
func WithHeaders(h map[string]string) OptionFunc
WithHeaders adds request headers
func WithIdleConnTimeout ¶
func WithIdleConnTimeout(d time.Duration) OptionFunc
WithIdleConnTimeout sets idle connection timeout
func WithMaxConnsPerHost ¶
func WithMaxConnsPerHost(n int) OptionFunc
WithMaxConnsPerHost sets maximum connections per host
func WithMaxIdleConns ¶
func WithMaxIdleConns(n int) OptionFunc
WithMaxIdleConns sets global maximum idle connections
func WithTimeout ¶
func WithTimeout(timeout time.Duration) OptionFunc
WithTimeout sets client timeout