httputils

package
v0.2.15 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(r *http.Request, w http.ResponseWriter, msg string, err error, code int)

func LoggingMiddleware added in v0.2.6

func LoggingMiddleware(rt http.RoundTripper) http.RoundTripper

LoggingMiddleware wraps a transport with LoggingRoundTripper (method, URL, status).

func NewClient added in v0.2.6

func NewClient(opts ClientOpts) *http.Client

NewClient returns a configured *http.Client. Middlewares are applied in order, wrapping the base transport.

func NewDefaultClient added in v0.2.6

func NewDefaultClient(ctx context.Context) *http.Client

NewDefaultClient returns an *http.Client with LoggingRoundTripper, 60s timeout, and default TLS settings. It does NOT carry Grafana bearer tokens — callers must set auth headers per request.

Reads context for configuration:

  • PayloadLogging(ctx): when true, adds RequestResponseLoggingMiddleware for full request/response body dumps (includes headers — may expose tokens).

func NewDefaultClientWithTLS added in v0.2.11

func NewDefaultClientWithTLS(ctx context.Context, tlsConfig *tls.Config) *http.Client

NewDefaultClientWithTLS is like NewDefaultClient but accepts an optional *tls.Config for mTLS or custom CA scenarios. When tlsConfig is nil it behaves identically to NewDefaultClient.

func NewTransport

func NewTransport(tlsConfig *tls.Config) *http.Transport

NewTransport returns an *http.Transport with sensible defaults. If tlsConfig is nil, a default TLS config (TLS 1.2+, verify enabled) is used.

func PayloadLogging added in v0.2.6

func PayloadLogging(ctx context.Context) bool

PayloadLogging returns the --log-http-payload flag value from the context.

func RequestResponseLoggingMiddleware added in v0.2.6

func RequestResponseLoggingMiddleware(rt http.RoundTripper) http.RoundTripper

RequestResponseLoggingMiddleware wraps a transport with RequestResponseLoggingRoundTripper (full request/response body dump via httputil.DumpRequest/DumpResponse).

func WithPayloadLogging added in v0.2.6

func WithPayloadLogging(ctx context.Context, enabled bool) context.Context

WithPayloadLogging returns a context that carries the --log-http-payload flag value.

func Write

func Write(r *http.Request, w http.ResponseWriter, content []byte)

func WriteJSON

func WriteJSON(r *http.Request, w http.ResponseWriter, content any)

Types

type ClientOpts added in v0.2.6

type ClientOpts struct {
	TLSConfig   *tls.Config
	Timeout     time.Duration // default: 60s
	Middlewares []Middleware  // default: []Middleware{LoggingMiddleware}
}

ClientOpts configures NewClient. See NewDefaultClient for common usage.

type LoggingRoundTripper added in v0.2.6

type LoggingRoundTripper struct {
	Base http.RoundTripper
}

LoggingRoundTripper logs HTTP method, URL, and response status at appropriate levels.

Successful responses (2xx/3xx) and client errors (4xx) are logged at Debug, visible with -vvv. Server errors (5xx) and transport failures are logged at Warn, visible with -v.

func (*LoggingRoundTripper) RoundTrip added in v0.2.6

func (t *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type Middleware added in v0.2.6

type Middleware func(http.RoundTripper) http.RoundTripper

Middleware wraps an http.RoundTripper, e.g. for logging or tracing.

type RequestResponseLoggingRoundTripper added in v0.2.6

type RequestResponseLoggingRoundTripper struct {
	DecoratedTransport http.RoundTripper
}

RequestResponseLoggingRoundTripper logs full HTTP request and response bodies at Debug level via httputil.DumpRequest / httputil.DumpResponse (includes headers — may expose tokens). Enabled when --log-http-payload is set.

func (RequestResponseLoggingRoundTripper) RoundTrip added in v0.2.6

type UserAgentTransport added in v0.2.6

type UserAgentTransport struct {
	Base http.RoundTripper
}

UserAgentTransport injects the gcx User-Agent header into every outgoing request.

func (*UserAgentTransport) RoundTrip added in v0.2.6

func (t *UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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