request

package
v1.0.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FailedToLoadBody

func FailedToLoadBody[T goservectx.Principal](ctx *goservectx.Request[T], _ error)

func FormValues

func FormValues(r *http.Request) url.Values

func GetRequestBody

func GetRequestBody[B any, T goservectx.Principal](
	ctx *goservectx.Request[T],
	target B,
	onSuccess OnSuccess[B, T],
	onError OnError[T],
)

GetRequestBody parses the JSON request body and executes the appropriate success or error handler. ctx is the request context containing headers and the request body. target is the variable to decode the request body into. onSuccess is invoked if the request body is successfully parsed or if Content-Type is unsupported. onError is invoked if JSON decoding fails or any other error occurs.

Types

type Config

type Config struct {
	Host               string
	Path               string
	Headers            map[string]string
	Query              map[string]string
	Body               any
	ExpectedStatusCode int
}

func Build

func Build(host string) *Config

func (*Config) WithBody

func (config *Config) WithBody(body any) *Config

func (*Config) WithExpectedStatusCode

func (config *Config) WithExpectedStatusCode(expectedStatusCode int) *Config

func (*Config) WithHeader

func (config *Config) WithHeader(name string, value any) *Config

func (*Config) WithHeaders

func (config *Config) WithHeaders(headers map[string]any) *Config

func (*Config) WithPath

func (config *Config) WithPath(path string) *Config

func (*Config) WithQueries

func (config *Config) WithQueries(queries map[string]any) *Config

func (*Config) WithQuery

func (config *Config) WithQuery(name string, value any) *Config

type FieldSource

type FieldSource string

FieldSource indicates where a field originated from

type OnError

type OnError[T goservectx.Principal] func(ctx *goservectx.Request[T], err error)

type OnSuccess

type OnSuccess[B any, T goservectx.Principal] func(ctx *goservectx.Request[T], body B)

type RequestError

type RequestError struct {
	Field   string      `json:"field"`      // The original field name from request
	Source  FieldSource `json:"source"`     // Where the field came from
	Message string      `json:"message"`    // Human-readable error message
	Code    int         `json:"statusCode"` // HTTP status code
}

RequestError represents a validation error with contextual information

func BindRequestParams

func BindRequestParams(r *http.Request, target interface{}) *RequestError

BindRequestParams extracts and binds request parameters such as query, form data, headers, or route vars into a target struct. It validates the target struct and returns a RequestError with details on validation failure, or nil on success.

func (*RequestError) Error

func (e *RequestError) Error() string

Error implements the error interface

type Service

type Service interface {
	// Get sends an HTTP GET request with the provided configuration and returns the HTTP response or an error.
	Get(config *Config) (*http.Response, error)

	// Post sends an HTTP POST request with the provided configuration and returns the HTTP response or an error.
	Post(config *Config) (*http.Response, error)

	// Put sends an HTTP PUT request with the provided configuration and returns the HTTP response or an error.
	Put(config *Config) (*http.Response, error)

	// Delete sends an HTTP DELETE request with the provided configuration and returns the HTTP response or an error.
	Delete(config *Config) (*http.Response, error)

	// Patch sends an HTTP PATCH request with the provided configuration and returns the HTTP response or an error.
	Patch(config *Config) (*http.Response, error)

	// Head sends an HTTP HEAD request with the provided configuration and returns the HTTP response or an error.
	Head(config *Config) (*http.Response, error)

	// Exec sends an HTTP request using the specified method and configuration and returns the HTTP response and an error.
	Exec(method string, config *Config) (*http.Response, error)

	// ToString converts the body of the last HTTP response into a string and returns it along with an error if any.
	ToString() (string, error)

	// BodyDecode decodes the body of the last HTTP response into the given target interface and returns an error if any.
	// Example:
	//   ...
	//	 data := MyData{}
	//   err := client.BodyDecode(&data)
	BodyDecode(target any) error

	// Close closes the response body of the last HTTP response to release resources.
	Close()
}

func NewService

func NewService() Service

Jump to

Keyboard shortcuts

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