client

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxResponseBytes int64 = 32 << 20 // 32 MiB

DefaultMaxResponseBytes caps response body reads. Set via WithMaxResponseBytes; pass 0 to disable the cap entirely.

Variables

This section is empty.

Functions

func HTTPClient

func HTTPClient() *http.Client

Types

type Option added in v0.7.0

type Option func(*REST)

Option configures a REST client.

func WithBaseURL added in v0.7.0

func WithBaseURL(s string) Option

WithBaseURL sets a base URL that relative request URLs are resolved against. Absolute request URLs are passed through unchanged.

func WithHTTPClient added in v0.7.0

func WithHTTPClient(c *http.Client) Option

WithHTTPClient overrides the underlying *http.Client.

func WithHeader added in v0.7.0

func WithHeader(k, v string) Option

WithHeader sets a default header applied to every request. Per-request headers already set on the *http.Request take precedence.

func WithHeaders added in v0.7.0

func WithHeaders(h http.Header) Option

WithHeaders merges the given headers into the default header set.

func WithLogger added in v0.7.0

func WithLogger(l *slog.Logger) Option

WithLogger overrides the slog.Logger used for request logging.

func WithMaxResponseBytes added in v0.7.0

func WithMaxResponseBytes(n int64) Option

WithMaxResponseBytes caps the number of response body bytes read into memory. Pass 0 to disable the cap.

type REST added in v0.7.0

type REST struct {
	// contains filtered or unexported fields
}

REST is an embeddable HTTP client for REST APIs. Concrete clients embed *REST and expose typed methods that build an *http.Request and call Process.

func New added in v0.7.0

func New(opts ...Option) *REST

New creates a REST client with the given options. By default it uses HTTPClient() (retrying, OTel-instrumented), an empty header set, a discard logger, and a DefaultMaxResponseBytes response cap.

func (*REST) Process added in v0.7.0

func (c *REST) Process(ctx context.Context, req *http.Request, out any) error

Process sends req and writes the response into out, choosing how based on the type of out:

  • nil: response body is discarded.
  • *[]byte: raw bytes.
  • *string: raw string.
  • io.Writer: streamed via io.Copy.
  • anything else: JSON-decoded.

Non-2xx responses return an *errors.Error carrying the status code and body. Response body reads are capped by WithMaxResponseBytes (0 disables the cap). The response body is always closed.

Jump to

Keyboard shortcuts

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