httpclient

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package httpclient provides a configurable HTTP client with sensible defaults and helper methods for making HTTP requests.

Index

Constants

View Source
const (
	// DefaultTimeout is the default timeout for general network operations.
	DefaultTimeout = 10 * time.Second

	// DefaultTLSHandshakeTimeout is the default timeout for TLS handshake operations.
	DefaultTLSHandshakeTimeout = 5 * time.Second

	// DefaultDialerTimeout is the default timeout for establishing new connections.
	DefaultDialerTimeout = 5 * time.Second
)

Variables

View Source
var (
	// ErrWrongStatusCode is returned when the server responds with an unexpected HTTP status code.
	ErrWrongStatusCode = errors.New("wrong status code")

	// ErrEmptyResponse is returned when the server response is nil and error is nil.
	ErrEmptyResponse = errors.New("empty response")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	http.Client
}

Client wraps http.Client to provide additional functionality and configuration. It embeds the standard http.Client to expose all its methods while adding custom behavior.

func New

func New(cfg *Config) *Client

New creates and returns a new Client instance configured with the given settings. The configuration includes timeouts, transport settings, and dialer parameters.

func (*Client) SendRequest

func (c *Client) SendRequest(ctx context.Context, method, uri string, body io.Reader) ([]byte, error)

SendRequest executes an HTTP request with the given method, URI, and optional body. It handles the full request lifecycle including context cancellation, error handling, and response processing.

Parameters:

  • ctx: Context for cancellation and timeout control
  • method: HTTP method (GET, POST, etc.)
  • uri: Target URL for the request
  • body: Request body content (can be nil)

Returns:

  • Response body as byte slice if successful
  • Error if request fails, response status is not 200 OK, or body read fails.

type Config

type Config struct {
	// Timeout specifies the maximum duration for the entire connection
	// process. Zero means no timeout.
	Timeout time.Duration `json:"timeout" yaml:"timeout"`

	// TLSHandshakeTimeout specifies the maximum duration to wait for
	// a TLS handshake to complete. Zero means no timeout.
	TLSHandshakeTimeout time.Duration `json:"tls_handshake_timeout" yaml:"tls_handshake_timeout"`

	// Dialer contains configuration specific to the connection dialer.
	Dialer struct {
		// Timeout is the maximum duration for dialing a connection.
		// This includes name resolution if required.
		Timeout time.Duration `json:"timeout" yaml:"timeout"`

		// Deadline specifies an absolute time point after which
		// dial operations will fail.
		// Zero means no deadline.
		Deadline time.Time `json:"deadline" yaml:"deadline"`

		// FallbackDelay specifies the length of time to wait before
		// spawning a fallback connection, when dual-stack IPv4/IPv6
		// is enabled.
		FallbackDelay time.Duration `json:"fallback_delay" yaml:"fallback_delay"`

		// KeepAlive specifies the keep-alive period for network
		// connections. If zero, keep-alives are not enabled.
		KeepAlive time.Duration `json:"keep_alive" yaml:"keep_alive"`
	} `json:"dialer" yaml:"dialer"`
}

Config represents configuration settings for network connections. It includes timeouts and dialer-specific parameters that can be unmarshalled from either JSON or YAML formats.

func (*Config) SetDefaults

func (cfg *Config) SetDefaults()

SetDefaults initializes the configuration with default values for any unset fields. If Timeout, TLSHandshakeTimeout, or Dialer.Timeout are zero (unset), they will be populated with their respective default values.

This ensures the configuration is always valid and prevents zero-values from causing unexpected behavior in network operations.

Jump to

Keyboard shortcuts

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