Documentation
¶
Overview ¶
Package types provides shared type definitions used across the httpc library. This package eliminates interface duplication between public and internal layers, enabling compile-time type checking without runtime type assertions.
Package types provides shared type definitions used across the httpc library. This package eliminates interface duplication between public and internal layers, enabling compile-time type checking without runtime type assertions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileData ¶
type FileData struct {
// Filename is the name of the file as sent to the server.
Filename string
// Content is the raw file content.
Content []byte
// ContentType is the MIME type of the file (e.g., "image/png", "application/pdf").
ContentType string
}
FileData represents a file to be uploaded in a multipart form. It contains the filename, file content, and content type.
type FormData ¶
type FormData struct {
// Fields contains the text form fields.
Fields map[string]string
// Files contains the file uploads mapped by field name.
Files map[string]*FileData
}
FormData represents multipart form data for HTTP requests. It contains both text fields and file uploads.
Example:
form := &types.FormData{
Fields: map[string]string{
"username": "john",
"email": "john@example.com",
},
Files: map[string]*types.FileData{
"avatar": {
Filename: "avatar.png",
Content: imageData,
ContentType: "image/png",
},
},
}
client.Post("/upload", httpc.WithFormData(form))
type Handler ¶
type Handler func(ctx context.Context, req RequestMutator) (ResponseMutator, error)
Handler processes an HTTP request and returns a response. This is the core function signature for request processing in the middleware chain.
type MiddlewareFunc ¶
MiddlewareFunc wraps a Handler with additional functionality. Middleware can inspect/modify requests, handle responses, add logging, etc.
type RequestMutator ¶
type RequestMutator interface {
// contains filtered or unexported methods
}
RequestMutator provides read-write access to request data for middleware. It embeds requestReader and requestWriter for full access to request properties. Middleware can inspect and modify request properties before the request is sent.
type ResponseMutator ¶
type ResponseMutator interface {
ResponseReader
// contains filtered or unexported methods
}
ResponseMutator provides read-write access to response data for middleware. It embeds ResponseReader and responseWriter for full access to response properties. Middleware can inspect and modify response properties after the request completes. This is useful for:
- Response caching middleware
- Response transformation (e.g., JSON pretty-printing)
- Content encoding/decoding
- Response filtering
type ResponseReader ¶
type ResponseReader interface {
StatusCode() int
Status() string
Proto() string
Headers() http.Header
Body() string
RawBody() []byte
ContentLength() int64
Duration() time.Duration
Attempts() int
Cookies() []*http.Cookie
RedirectChain() []string
RedirectCount() int
RequestHeaders() http.Header
RequestURL() string
RequestMethod() string
}
ResponseReader provides read-only access to response data for middleware. Use this interface when middleware only needs to inspect response properties without modifying them.
type RetryPolicy ¶
type RetryPolicy interface {
// ShouldRetry determines if a request should be retried based on the
// response, error, and current attempt number (0-indexed).
// Return true to retry, false to stop.
ShouldRetry(resp ResponseReader, err error, attempt int) bool
// GetDelay returns the delay duration before the next retry attempt.
GetDelay(attempt int) time.Duration
// MaxRetries returns the maximum number of retry attempts.
MaxRetries() int
}
RetryPolicy defines the interface for custom retry behavior. Implementations can provide custom retry strategies beyond the default exponential backoff with jitter.
Example implementation:
type CustomRetryPolicy struct {
maxRetries int
}
func (p *CustomRetryPolicy) ShouldRetry(resp ResponseReader, err error, attempt int) bool {
if err != nil {
return true // Retry on errors
}
if resp.StatusCode() >= 500 && resp.StatusCode() < 600 {
return attempt < p.maxRetries
}
return false
}
func (p *CustomRetryPolicy) GetDelay(attempt int) time.Duration {
return time.Second * time.Duration(attempt+1)
}
func (p *CustomRetryPolicy) MaxRetries() int {
return p.maxRetries
}