helper

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package helper provides utility functions for HTTP request and response handling.

Index

Constants

View Source
const (
	// DefaultMaxBodySize is the default maximum size for auto-caching request bodies.
	// Bodies larger than this will cause NewRequestBodyHandler to return an error.
	// This prevents memory exhaustion when handling large file uploads.
	DefaultMaxBodySize = 4 * 1024 * 1024 // 4MB
)

Variables

This section is empty.

Functions

func IsClientError added in v0.1.0

func IsClientError(err error) bool

IsClientError returns whether the error is a 4xx client error.

func IsGatewayIssue

func IsGatewayIssue(err error) bool

IsGatewayIssue returns a boolean indicating whether the error is related to known gateway issues. It uses structured error checking first, falling back to string matching for compatibility.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout returns a boolean indicating whether the error is known to report that a timeout occurred.

Types

type HTTPStatusError added in v0.1.0

type HTTPStatusError struct {
	StatusCode int
	Status     string
}

HTTPStatusError represents an HTTP error with a specific status code. This allows for type-safe error checking and avoids string matching.

func NewHTTPStatusError added in v0.1.0

func NewHTTPStatusError(statusCode int, status string) *HTTPStatusError

NewHTTPStatusError creates a new HTTPStatusError.

func (*HTTPStatusError) Error added in v0.1.0

func (e *HTTPStatusError) Error() string

type RequestBodyHandler added in v0.1.0

type RequestBodyHandler struct {
	// contains filtered or unexported fields
}

RequestBodyHandler provides utilities for preserving and restoring HTTP request bodies. This is essential for retry logic, where the request body needs to be read multiple times.

func NewRequestBodyHandler added in v0.1.0

func NewRequestBodyHandler(req *http.Request) (*RequestBodyHandler, error)

NewRequestBodyHandler creates a new RequestBodyHandler from an HTTP request. It reads and caches the request body (if present) to enable multiple reads.

Important notes:

  • If req.GetBody is already set, it will be used directly (most efficient)
  • Otherwise, the body is read into memory and cached (up to DefaultMaxBodySize)
  • Bodies larger than DefaultMaxBodySize (4MB) will return an error
  • For large bodies (e.g., file uploads), you MUST set req.GetBody manually
  • After calling this, the original req.Body will be replaced with a new reader

Example:

handler, err := helper.NewRequestBodyHandler(req)
if err != nil {
    return err
}
// Later, to restore the body before retry:
if err := handler.RestoreBody(req); err != nil {
    return err
}

func (*RequestBodyHandler) RestoreBody added in v0.1.0

func (h *RequestBodyHandler) RestoreBody(req *http.Request) error

RestoreBody restores the request body to its original state. This should be called before each retry attempt.

type ResponseBodyRestorer added in v0.1.0

type ResponseBodyRestorer struct {
	// contains filtered or unexported fields
}

ResponseBodyRestorer helps preserve and restore HTTP response bodies. This is useful for debugging middleware that need to read the response.

func NewResponseBodyRestorer added in v0.1.0

func NewResponseBodyRestorer(resp *http.Response) (*ResponseBodyRestorer, error)

NewResponseBodyRestorer creates a new ResponseBodyRestorer by reading the response body. The original response body will be closed and replaced with a new reader.

func (*ResponseBodyRestorer) GetBytes added in v0.1.0

func (r *ResponseBodyRestorer) GetBytes() []byte

GetBytes returns a copy of the cached body bytes.

func (*ResponseBodyRestorer) RestoreBody added in v0.1.0

func (r *ResponseBodyRestorer) RestoreBody(resp *http.Response) error

RestoreBody restores the response body. This can be called multiple times.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL