httputil

package
v1.39.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httputil provides a thin JSON HTTP client shared by promclient, jaegerclient, and dozorclient. Each of those packages previously rolled its own http.Client.Do + json.Decode + status-check + error-wrap; this package consolidates the pattern.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a thin wrapper around http.Client for JSON GET/POST.

func New

func New(baseURL string, opts ...Option) *Client

New creates a Client targeting baseURL. Trailing slashes are stripped. Default timeout is 30 s if not overridden via WithTimeout.

func NewWithHTTPClient

func NewWithHTTPClient(baseURL string, hc *http.Client, opts ...Option) *Client

NewWithHTTPClient creates a Client that reuses an existing *http.Client. This preserves connection pooling when the caller already manages an http.Client instance (e.g. promclient, jaegerclient, dozorclient).

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, path string, dst any) error

GetJSON issues GET <base><path> and decodes a successful JSON response into dst (may be nil to discard body). Non-2xx status returns a wrapped error including the status code and up to 256 bytes of body for diagnostics.

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, path string, body, dst any) error

PostJSON issues POST <base><path> with body marshalled as JSON and decodes the response into dst (may be nil).

func (*Client) Timeout added in v1.23.3

func (c *Client) Timeout() time.Duration

Timeout returns the underlying http.Client timeout. It is exposed for callers (e.g. client tests) that need to verify the timeout configured at construction time.

type Option

type Option func(*Client)

Option configures a Client.

func WithHeader

func WithHeader(k, v string) Option

WithHeader returns an Option that adds a fixed header to every request.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout returns an Option that sets the HTTP client timeout.

Jump to

Keyboard shortcuts

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