httpx

package
v0.0.0-...-4b578d8 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const BlankURL = "about:blank"

Variables

This section is empty.

Functions

func EmailOrE164

func EmailOrE164(fl validator.FieldLevel) bool

EmailOrE164 is a custom validator function that returns true if the field value is either a valid email or a valid E.164 phone number.

func Error

func Error(w http.ResponseWriter, status int, msg string)

Error sends an error response in the "application/problem+json" format. If problem is nil, a default problem detail is generated.

func GetProblemTypeURL

func GetProblemTypeURL(errorType string) string

GetProblemTypeURL get the full problem type URL based on the error type.

If the error type is not found in the predefined paths, it returns a default unknown error path.

Parameters: - errorType: The unique key identifying the error type (e.g., "validation_error").

Example usage:

problemTypeURL := GetProblemTypeURL("validation_error")

func NewValidator

func NewValidator() error

func ParseRequest

func ParseRequest[T any](w http.ResponseWriter, r *http.Request) (T, error)
func (r *GetUserRequest) Validate() error {
    if r.UserID < 1 {
        return errors.New("user ID must be positive")
    }
    return nil
}

func PgNumericGt

func PgNumericGt(fl validator.FieldLevel) bool

func SetProblemBaseURL

func SetProblemBaseURL(baseURL string)

SetProblemBaseURL configures the base URL used in the "type" field for ProblemDetails.

This function allows applications using httpx to provide a custom domain and structure for error documentation URLs. By setting this base URL, the library can generate meaningful and discoverable problem types.

Parameters: - baseURL: The base URL where error documentation is hosted (e.g., "https://api.mycompany.com").

Example usage:

httpx.SetProblemBaseURL("https://api.mycompany.com")

Once configured, generated ProblemDetails will include a "type" such as:

"https://api.mycompany.com/errors/validation-error"

If the base URL is not set, the default value for the "type" field will be "about:blank".

func SetProblemErrorTypePath

func SetProblemErrorTypePath(errorType, path string)

SetProblemErrorTypePath sets or updates the path for a specific error type.

This allows applications to define custom paths for error documentation.

Parameters: - errorType: The unique key identifying the error type (e.g., "validation_error"). - path: The path under the base URL where the error documentation is located.

Example usage:

httpx.SetProblemErrorTypePath("validation_error", "/errors/validation-error")

After setting this path, the generated problem type for "validation_error" will be:

"https://api.mycompany.com/errors/validation-error"

func SetProblemErrorTypePaths

func SetProblemErrorTypePaths(paths map[string]string)

SetProblemErrorTypePaths sets or updates multiple paths for different error types.

This allows applications to define multiple custom paths at once.

Parameters: - paths: A map of error types to paths (e.g., {"validation_error": "/errors/validation-error"}).

Example usage:

paths := map[string]string{
    "validation_error":  "/errors/validation-error",
    "not_found_error":   "/errors/not-found",
}
httpx.SetProblemErrorTypePaths(paths)

This method overwrites any existing paths with the same keys.

func Success

func Success(w http.ResponseWriter, code int, msg string, data interface{}, meta *Meta)

Success sends a successful JSON response to the client. It encodes the response directly to the response writer.

Types

type Meta

type Meta struct {
	Page       int `json:"page,omitempty"`
	PageSize   int `json:"page_size,omitempty"`
	TotalPages int `json:"total_pages,omitempty"`
	TotalItems int `json:"total_items,omitempty"`
}

Meta provides additional response metadata (e.g., pagination).

type ProblemDetails

type ProblemDetails struct {
	Type       string                 `json:"type"`                 // A URI reference identifying the problem type.
	Title      string                 `json:"title"`                // A short, human-readable summary of the problem.
	Status     int                    `json:"status"`               // The HTTP status code.
	Detail     string                 `json:"detail,omitempty"`     // Detailed explanation of the problem.
	Instance   string                 `json:"instance,omitempty"`   // A URI reference identifying the specific instance of the problem.
	Extensions map[string]interface{} `json:"extensions,omitempty"` // Custom fields for additional details.
}

ProblemDetails conforms to RFC 9457, providing a standard format for describing errors in HTTP APIs.

func IsRequestValid

func IsRequestValid(request any) *ProblemDetails

IsRequestValid validates the provided request struct using the go-playground/validator package. It returns a ProblemDetails instance if validation fails, or nil if the request is valid.

func NewProblemDetails

func NewProblemDetails(status int, detail, title, problemType string) *ProblemDetails

NewProblemDetails creates a ProblemDetails instance with standard fields.

func NewValidationProblemDetails

func NewValidationProblemDetails(err error) *ProblemDetails

NewValidationProblemDetails creates a ProblemDetails instance based on validation errors. It maps field-specific validation errors into structured details.

type RequestParamSource

type RequestParamSource int
const (
	SourceURL   RequestParamSource = iota // URL path parameters
	SourceQuery                           // URL query parameters
)

type RequestParamTag

type RequestParamTag struct {
	Source   RequestParamSource
	Name     string
	Required bool
}

RequestParamTag defines the struct tag format for parameter binding

type Response

type Response struct {
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
	Meta    *Meta       `json:"meta,omitempty"`
}

Response represents a successful HTTP response.

type ValidationErrorDetail

type ValidationErrorDetail struct {
	Field   string `json:"field"`   // The name of the field that failed validation.
	Message string `json:"message"` // A human-readable message describing the error.
}

ValidationErrorDetail provides structured details about a single validation error.

Jump to

Keyboard shortcuts

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