httpclient

package module
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 8 Imported by: 1

README

http-client

Fluent, streaming‑first HTTP client for Go — zero external dependencies.

Features

  • Fluent API (method chaining)
  • Path variables /users/{id}
  • Efficient streaming via io.Pipe (no full buffering)
  • Multipart/form-data with streamed file parts
  • JSON / raw / form / query / headers / cookies helpers
  • Type-safe parameter setters:
    • Path: PathParam, PathInt, PathBool, PathFloat
    • Query (standard requests): Param, Int, Bool, Float
    • Multipart form: Param, Int, Bool, Float, File
  • Middleware: retry, rate limit, circuit breaker, logging
  • Context, timeouts, cancellation baked in
  • No third‑party deps

Install

go get github.com/nativebpm/http-client

Quick Start

client, _ := httpclient.NewClient(http.Client{}, "https://api.example.com")

resp, err := client.GET(ctx, "/users").Send()

resp, err = client.GET(ctx, "/users/{id}").
    PathInt("id", 123).
    Send()

resp, err = client.POST(ctx, "/users").
    JSON(map[string]string{"name": "John"}).
    Send()

resp, err = client.Multipart(ctx, "/upload").
    File("document", "file.pdf", fileReader).
    Send()

// With cookies
resp, err = client.GET(ctx, "/profile").
    Cookie("session", "abc123").
    Send()

Streaming (end‑to‑end)

Move data between servers without loading the whole file in memory:

src, err := http.Get("http://server1/file")
if err != nil { /* handle */ }
defer src.Body.Close()

dst, err := client.Multipart(ctx, "/upload").
    File("file", "data.txt", src.Body).
    Send()
if err != nil { /* handle */ }
defer dst.Body.Close()

Middleware Example

resp, err := client.GET(ctx, "/reports").
    Retry(3).
    RateLimit(10).
    CircuitBreaker("reports", 5, time.Minute).
    Send()

Examples

See the examples/ directory.

When to Use

Pick this when you need streaming, concise request building, minimal allocations, and built‑in resilience patterns. If you want absolute low‑level control, stick with net/http directly.

License

MIT

Documentation

Overview

Package httpclient provides a convenient HTTP client with request builders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CircuitBreakerMiddleware added in v1.8.1

func CircuitBreakerMiddleware(cb *CircuitBreaker) func(http.RoundTripper) http.RoundTripper

CircuitBreakerMiddleware returns a middleware that implements circuit breaker

func LoggingMiddleware added in v1.8.1

func LoggingMiddleware(logger Logger) func(http.RoundTripper) http.RoundTripper

LoggingMiddleware returns a middleware that logs requests and responses

func RateLimitMiddleware added in v1.8.1

func RateLimitMiddleware(limiter *RateLimiter) func(http.RoundTripper) http.RoundTripper

RateLimitMiddleware returns a middleware that enforces rate limiting

func RetryMiddleware added in v1.8.1

func RetryMiddleware(config RetryConfig) func(http.RoundTripper) http.RoundTripper

RetryMiddleware returns a middleware that retries requests based on the config

Types

type CircuitBreaker added in v1.8.1

type CircuitBreaker = middleware.CircuitBreaker

CircuitBreaker implements a simple circuit breaker

func NewCircuitBreaker added in v1.8.1

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

type CircuitBreakerConfig added in v1.8.1

type CircuitBreakerConfig = middleware.CircuitBreakerConfig

CircuitBreakerConfig holds configuration for circuit breaker middleware

func DefaultCircuitBreakerConfig added in v1.8.1

func DefaultCircuitBreakerConfig() CircuitBreakerConfig

DefaultCircuitBreakerConfig returns a default circuit breaker configuration

type CircuitBreakerState added in v1.8.1

type CircuitBreakerState = middleware.CircuitBreakerState

CircuitBreakerState represents the state of the circuit breaker

type Client

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

Client wraps http.Client and provides request builders for different HTTP methods.

func NewClient

func NewClient(client http.Client, baseURL string) (*Client, error)

NewClient creates a new HTTP client with the given base URL. Returns an error if the base URL is invalid.

func (*Client) DELETE added in v1.6.0

func (c *Client) DELETE(ctx context.Context, path string) *request.Request

DELETE creates a DELETE request builder.

func (*Client) GET added in v1.6.0

func (c *Client) GET(ctx context.Context, path string) *request.Request

GET creates a GET request builder.

func (*Client) Multipart added in v1.6.0

func (c *Client) Multipart(ctx context.Context, path string) *formdata.Multipart

Multipart creates a multipart/form-data POST request builder.

func (*Client) MultipartRequest added in v1.8.1

func (c *Client) MultipartRequest(ctx context.Context, method, path string) *formdata.Multipart

MultipartRequest creates a multipart/form-data request builder with HTTP method.

func (*Client) PATCH added in v1.6.0

func (c *Client) PATCH(ctx context.Context, path string) *request.Request

PATCH creates a PATCH request builder.

func (*Client) POST added in v1.6.0

func (c *Client) POST(ctx context.Context, path string) *request.Request

POST creates a POST request builder.

func (*Client) PUT added in v1.6.0

func (c *Client) PUT(ctx context.Context, path string) *request.Request

PUT creates a PUT request builder.

func (*Client) Request added in v1.6.0

func (c *Client) Request(ctx context.Context, method, path string) *request.Request

Request creates a standard HTTP request builder.

func (*Client) SetTransport added in v1.8.1

func (c *Client) SetTransport(rt http.RoundTripper) *Client

SetTransport sets a custom base RoundTripper. This will be wrapped by middlewares.

func (*Client) Use added in v1.8.1

func (c *Client) Use(middleware func(http.RoundTripper) http.RoundTripper) *Client

Use adds a middleware to the client. Middlewares are applied in the order they are added. Each middleware is a function that takes a RoundTripper and returns a wrapped RoundTripper.

func (*Client) WithCircuitBreaker added in v1.8.1

func (c *Client) WithCircuitBreaker() *Client

WithCircuitBreaker adds circuit breaker middleware with default config

func (*Client) WithCircuitBreakerConfig added in v1.8.1

func (c *Client) WithCircuitBreakerConfig(config CircuitBreakerConfig) *Client

WithCircuitBreakerConfig adds circuit breaker middleware with custom config

func (*Client) WithLogger added in v1.8.2

func (c *Client) WithLogger(logger Logger) *Client

WithLogger adds logging middleware with custom logger

func (*Client) WithRateLimit added in v1.8.1

func (c *Client) WithRateLimit(capacity int, refillRate time.Duration) *Client

WithRateLimit adds rate limiting middleware

func (*Client) WithRetry added in v1.8.1

func (c *Client) WithRetry() *Client

WithRetry adds retry middleware with default config

func (*Client) WithRetryConfig added in v1.8.1

func (c *Client) WithRetryConfig(config RetryConfig) *Client

WithRetryConfig adds retry middleware with custom config

type Logger added in v1.8.2

type Logger = middleware.Logger

Logger interface for universal logging support

type Multipart added in v1.1.0

type Multipart = formdata.Multipart

Public types re-exports

type RateLimiter added in v1.8.1

type RateLimiter = middleware.RateLimiter

RateLimiter implements a basic token bucket rate limiter

func NewRateLimiter added in v1.8.2

func NewRateLimiter(capacity int, refillRate time.Duration) *RateLimiter

NewRateLimiter creates a new rate limiter

type Request

type Request = request.Request

type RetryConfig added in v1.8.1

type RetryConfig = middleware.RetryConfig

RetryConfig holds configuration for retry middleware

func DefaultRetryConfig added in v1.8.1

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a default retry configuration

Jump to

Keyboard shortcuts

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