utils

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultTimeout = 10 * time.Second

DefaultTimeout is the default per-request timeout.

View Source
const MaxResponseBody = 64 * 1024

MaxResponseBody limits how much of a response body is read for errors/decoding.

Variables

This section is empty.

Functions

func IsTransient added in v0.21.0

func IsTransient(err error) bool

IsTransient returns true if err is a transient error.

func ReadBodyLimited added in v0.21.0

func ReadBodyLimited(r io.Reader) ([]byte, error)

ReadBodyLimited reads at most MaxResponseBody bytes from r.

func RedactSecrets added in v0.21.0

func RedactSecrets(s string) string

RedactSecrets removes obvious secrets (tokens, query params) from a string.

func Wrap added in v0.21.0

func Wrap(kind ErrorKind, op string, err error) error

Wrap annotates err with kind and op.

Types

type Error added in v0.21.0

type Error struct {
	Kind ErrorKind
	Op   string
	Err  error
}

Error wraps an error with a kind and operation.

func (*Error) Error added in v0.21.0

func (e *Error) Error() string

func (*Error) Unwrap added in v0.21.0

func (e *Error) Unwrap() error

type ErrorKind added in v0.21.0

type ErrorKind string

ErrorKind identifies whether an error is transient or permanent.

const (
	ErrorTransient ErrorKind = "transient"
	ErrorPermanent ErrorKind = "permanent"
)

func KindFromStatus added in v0.21.0

func KindFromStatus(status int) ErrorKind

KindFromStatus maps HTTP status to error kind.

type HTTPDoer

type HTTPDoer interface {
	// DoRequest sends an HTTP request using the given parameters.
	//
	// Parameters:
	//   - ctx: A context controlling request timeout and cancellation.
	//   - method: The HTTP method to use (e.g. "GET", "POST").
	//   - url: The target URL of the HTTP request.
	//   - body: The request payload, typically JSON-encoded.
	//
	// Returns:
	//   - *http.Response: The raw HTTP response object.
	//   - error: Any error encountered during request creation or execution.
	DoRequest(ctx context.Context, method, url string, body []byte) (*http.Response, error)
}

HTTPDoer defines the interface required to perform an HTTP request.

It allows consumers to inject custom HTTP clients for testability and flexibility.

type HttpClient

type HttpClient struct {
	Headers      map[string]string // Headers are added to each outbound HTTP request.
	SkipInsecure bool              // SkipInsecure disables TLS certificate validation when true.
	Timeout      time.Duration     // Timeout is the per-request timeout.
}

HttpClient implements the Doer interface with support for custom headers and TLS options.

func NewHttpClient

func NewHttpClient(headers map[string]string, skipInsecure bool) *HttpClient

NewHttpClient creates a configured HTTP client for issuing requests.

Parameters:

  • headers: A map of key-value headers to attach to each request.
  • skipInsecure: If true, TLS verification is disabled.

Returns:

  • *HttpClient: A client ready to send requests via DoRequest.

func (*HttpClient) DoRequest

func (hc *HttpClient) DoRequest(ctx context.Context, method, url string, body []byte) (*http.Response, error)

DoRequest builds and sends an HTTP request with the given payload and configuration.

Parameters:

  • ctx: The request-scoped context for cancellation and timeout.
  • method: The HTTP method to use (e.g. "POST").
  • url: The request destination.
  • body: The payload to include in the request body.

Returns:

  • *http.Response: The HTTP response from the remote server.
  • error: An error if the request fails to be built or sent.

Jump to

Keyboard shortcuts

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