httputil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONResponse

func JSONResponse[RespType any](c HTTPClient, r *http.Request) (*http.Response, *RespType, error)

JSONResponse executes the given HTTP request using the provided HTTPClient, reads the response body, and unmarshal it into a value of type RespType.

func NewRequest

func NewRequest(ctx context.Context, method string, urlPath string, p Protocol, body any) (*http.Request, error)

NewRequest creates a new HTTP request with the given context, method, URL, protocol encoder, and request body.

Types

type DefaultHTTPClient

type DefaultHTTPClient struct {
	Client *http.Client
	Scheme string
	Host   string
}

DefaultHTTPClient is the default implementation of HTTPClient, which delegates to the standard library http.Client.

func (*DefaultHTTPClient) Do

Do executes the HTTP request using the embedded http.Client. It reads the entire response body into memory and returns both the *http.Response and the body as a byte slice.

The response body is also replaced with a reusable buffer so that it can be read again by the caller if needed.

Note: For very large responses, this may be memory intensive.

type FormProtocol

type FormProtocol struct{}

FormProtocol implements the Protocol interface for the application/x-www-form-urlencoded format.

func (*FormProtocol) Decode

func (p *FormProtocol) Decode(data []byte, v any) error

Decode deserializes application/x-www-form-urlencoded data into the provided struct or map.

func (*FormProtocol) Encode

func (p *FormProtocol) Encode(i any) ([]byte, error)

Encode serializes the given value into an application/x-www-form-urlencoded encoded byte slice.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, []byte, error)
}

HTTPClient defines a customizable HTTP executor interface. Implementing this interface allows users to provide their own HTTP execution logic (for example, to add retry, logging, or tracing).

type JSONProtocol

type JSONProtocol struct{}

JSONProtocol implements the Protocol interface using JSON encoding.

func (*JSONProtocol) Decode

func (p *JSONProtocol) Decode(data []byte, v any) error

Decode deserializes JSON data into the provided value.

func (*JSONProtocol) Encode

func (p *JSONProtocol) Encode(v any) ([]byte, error)

Encode serializes the given value into a JSON-encoded byte slice.

type Protocol

type Protocol interface {
	Encode(v any) ([]byte, error)
	Decode(data []byte, v any) error
}

Protocol defines a common interface for encoding and decoding data into different serialization formats.

var (
	// FORM provides an implementation of the Protocol interface using
	// application/x-www-form-urlencoded encoding.
	FORM Protocol = &FormProtocol{}

	// JSON provides an implementation of the Protocol interface using
	// JSON encoding.
	JSON Protocol = &JSONProtocol{}
)

Jump to

Keyboard shortcuts

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