httpx

package
v0.0.2-beta.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbortWithJsonError

func AbortWithJsonError(ctx httpx.Context, err error)

AbortWithJsonError terminates the request with a JSON error response. It uses the configured error parser to extract error details and ensures the HTTP status code is valid (200-599 range).

func EndpointsToMatches

func EndpointsToMatches(base string, endpoints ...[][3]string) map[string]map[string]string

func Fs

func Fs(local string, files fs.FS, emPath string) (fs.FS, error)

Fs create a fs.FS from either a local directory or an embedded filesystem. Priority:

  1. local directory (if it exists)
  2. embedded filesystem + emPath

func MatchOperation

func MatchOperation(base string, endpoints [][3]string, operations ...string) func(ctx httpx.Context) bool

func ParseError

func ParseError(err error) (code int32, status int32, message string)

ParseError extracts error information from various error types. It recognizes StatusError, CodeError, and MessageError interfaces and falls back to defaults for unknown error types.

func SetDefaultErrorParser

func SetDefaultErrorParser(parser ErrorParser)

SetDefaultErrorParser sets the global error parser function for the package. This parser will be used by AbortWithJsonError when no specific parser is provided.

func Value

func Value[T any](ctx httpx.Context, key string) (T, bool)

Value retrieves a typed value from the Gin context. It returns the value and a boolean indicating whether the key exists and the type matches.

func WithFormFileBytes

func WithFormFileBytes[T any](handler func(ctx httpx.Context, file []byte, filename string) (*T, error), options ...WithFormOption) httpx.Handler

WithFormFileBytes creates a Gin handler that processes uploaded files as byte arrays. It reads the entire file content into memory and passes it to the handler function. This is convenient for smaller files but should be used carefully with large files.

func WithFormFileReader

func WithFormFileReader[T any](handler func(ctx httpx.Context, file io.ReadSeekCloser, filename string) (*T, error), options ...WithFormOption) httpx.Handler

WithFormFileReader creates a Gin handler that processes uploaded files as io.ReadSeekCloser. It validates file size, extension constraints, and passes the file content to the handler function. The handler receives the file as an io.Reader along with the original filename.

func WithJson

func WithJson[T any](handler func(ctx httpx.Context) (T, error)) httpx.Handler

WithJson creates a Gin handler that returns JSON responses for typed data. It automatically handles errors by calling AbortWithJsonError and wraps successful responses in a standardized DataResponse structure.

func WithRecover

func WithRecover(message string, handler func(ctx httpx.Context) error) httpx.Handler

WithRecover wraps a Gin handler with panic recovery. If a panic occurs, it logs the error and returns a standardized internal server error response.

func WithText

func WithText(handler func(ctx httpx.Context) (string, error)) httpx.Handler

WithText creates a Gin handler that returns plain text responses. It handles errors by calling AbortWithJsonError and returns successful string responses with HTTP 200 status.

Types

type DataResponse

type DataResponse[T any] struct {
	Success bool `json:"success" default:"true"`
	Code    int  `json:"code,omitempty" default:"0"`
	Data    T    `json:"data"`
}

DataResponse represents a successful API response containing typed data. It follows a standard structure for consistent API responses across the application.

type ErrorParser

type ErrorParser func(error) (int32, int32, string)

ErrorParser is a function type that extracts error information for HTTP responses. It returns the error code, HTTP status code, and user-friendly message from an error.

type ErrorResponse

type ErrorResponse struct {
	Success bool   `json:"success" default:"false"`
	Code    int    `json:"code" default:"0"`
	Error   string `json:"error,omitempty"`
	Message string `json:"message,omitempty"`
}

ErrorResponse represents an API error response with error details. It provides both error and message fields for different levels of error information.

type WithFormOption

type WithFormOption func(*WithFormOptions)

WithFormOption is a functional option for configuring file upload behavior.

func WithFormAllowExtensions

func WithFormAllowExtensions(extensions ...string) WithFormOption

WithFormAllowExtensions restricts file uploads to specific file extensions. Extensions are matched case-insensitively. If no extensions are provided, all file types are allowed.

func WithFormFileKey

func WithFormFileKey(key string) WithFormOption

WithFormFileKey sets the form field name for file uploads. The default field name is "file".

func WithFormMaxSize

func WithFormMaxSize(maxSize int64) WithFormOption

WithFormMaxSize sets the maximum file size allowed for uploads. The size is specified in bytes.

type WithFormOptions

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

WithFormOptions contains configuration for file upload handling via multipart forms.

Jump to

Keyboard shortcuts

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