rest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package rest provides a REST client with resilience patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyRedactionMiddleware

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

BodyRedactionMiddleware redacts sensitive fields from request/response bodies.

func NewBodyRedactionMiddleware

func NewBodyRedactionMiddleware(fields []string) *BodyRedactionMiddleware

NewBodyRedactionMiddleware creates a new body redaction middleware.

func (*BodyRedactionMiddleware) HandleRequest

func (m *BodyRedactionMiddleware) HandleRequest(req *http.Request) error

HandleRequest is a no-op (redaction happens during logging).

func (*BodyRedactionMiddleware) HandleResponse

func (m *BodyRedactionMiddleware) HandleResponse(resp *Response) error

HandleResponse is a no-op (redaction happens during logging).

func (*BodyRedactionMiddleware) RedactJSON

func (m *BodyRedactionMiddleware) RedactJSON(data []byte) []byte

RedactJSON redacts sensitive fields from a JSON byte slice.

type CachingMiddleware

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

CachingMiddleware provides simple response caching.

func NewCachingMiddleware

func NewCachingMiddleware(maxAge time.Duration) *CachingMiddleware

NewCachingMiddleware creates a new caching middleware.

func (*CachingMiddleware) Get

func (m *CachingMiddleware) Get(key string) (*Response, bool)

Get retrieves a cached response.

func (*CachingMiddleware) HandleRequest

func (m *CachingMiddleware) HandleRequest(req *http.Request) error

HandleRequest is a no-op (cache lookup happens elsewhere).

func (*CachingMiddleware) HandleResponse

func (m *CachingMiddleware) HandleResponse(resp *Response) error

HandleResponse caches the response.

func (*CachingMiddleware) Set

func (m *CachingMiddleware) Set(key string, resp *Response)

Set caches a response.

type Client

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

Client is an HTTP client with resilience patterns.

func New

func New(config integration.Config) (*Client, error)

New creates a new REST client with the given configuration.

func (*Client) CircuitBreaker

func (c *Client) CircuitBreaker() *integration.CircuitBreaker

CircuitBreaker returns the circuit breaker for this client.

func (*Client) Config

func (c *Client) Config() integration.Config

Config returns the client configuration.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string, opts ...RequestOption) (*Response, error)

Delete performs a DELETE request.

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *Request) (*Response, error)

Do executes an HTTP request.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, opts ...RequestOption) (*Response, error)

Get performs a GET request.

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*Response, error)

Patch performs a PATCH request.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*Response, error)

Post performs a POST request.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*Response, error)

Put performs a PUT request.

func (*Client) Use

func (c *Client) Use(mw ...Middleware) *Client

Use adds middleware to the client.

type CompressionMiddleware

type CompressionMiddleware struct{}

CompressionMiddleware handles request/response compression.

func NewCompressionMiddleware

func NewCompressionMiddleware() *CompressionMiddleware

NewCompressionMiddleware creates a new compression middleware.

func (*CompressionMiddleware) HandleRequest

func (m *CompressionMiddleware) HandleRequest(req *http.Request) error

HandleRequest adds Accept-Encoding header.

func (*CompressionMiddleware) HandleResponse

func (m *CompressionMiddleware) HandleResponse(resp *Response) error

HandleResponse handles decompression (handled by http.Client automatically).

type HeaderMiddleware

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

HeaderMiddleware adds headers to requests.

func NewHeaderMiddleware

func NewHeaderMiddleware(headers map[string]string) *HeaderMiddleware

NewHeaderMiddleware creates a new header middleware.

func (*HeaderMiddleware) HandleRequest

func (m *HeaderMiddleware) HandleRequest(req *http.Request) error

HandleRequest adds headers to the request.

func (*HeaderMiddleware) HandleResponse

func (m *HeaderMiddleware) HandleResponse(resp *Response) error

HandleResponse is a no-op for header middleware.

type LoggingMiddleware

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

LoggingMiddleware logs request and response details.

func NewLoggingMiddleware

func NewLoggingMiddleware(redactFields []string) *LoggingMiddleware

NewLoggingMiddleware creates a new logging middleware.

func (*LoggingMiddleware) HandleRequest

func (m *LoggingMiddleware) HandleRequest(req *http.Request) error

HandleRequest logs the outgoing request.

func (*LoggingMiddleware) HandleResponse

func (m *LoggingMiddleware) HandleResponse(resp *Response) error

HandleResponse logs the incoming response.

type MetricsMiddleware

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

MetricsMiddleware records request metrics.

func NewMetricsMiddleware

func NewMetricsMiddleware(serviceName string) *MetricsMiddleware

NewMetricsMiddleware creates a new metrics middleware.

func (*MetricsMiddleware) HandleRequest

func (m *MetricsMiddleware) HandleRequest(req *http.Request) error

HandleRequest starts timing the request.

func (*MetricsMiddleware) HandleResponse

func (m *MetricsMiddleware) HandleResponse(resp *Response) error

HandleResponse records the response metrics.

type Middleware

type Middleware interface {
	HandleRequest(req *http.Request) error
	HandleResponse(resp *Response) error
}

Middleware is an interface for request/response middleware.

type RateLimitMiddleware

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

RateLimitMiddleware handles rate limiting.

func NewRateLimitMiddleware

func NewRateLimitMiddleware() *RateLimitMiddleware

NewRateLimitMiddleware creates a new rate limit middleware.

func (*RateLimitMiddleware) HandleRequest

func (m *RateLimitMiddleware) HandleRequest(req *http.Request) error

HandleRequest is a no-op for rate limit middleware.

func (*RateLimitMiddleware) HandleResponse

func (m *RateLimitMiddleware) HandleResponse(resp *Response) error

HandleResponse checks for rate limit headers.

type Request

type Request struct {
	Method      string
	Path        string
	Query       url.Values
	Headers     http.Header
	Body        interface{}
	Timeout     time.Duration
	SkipRetry   bool
	SkipCircuit bool
}

Request represents an HTTP request.

type RequestIDMiddleware

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

RequestIDMiddleware adds a request ID to requests.

func NewRequestIDMiddleware

func NewRequestIDMiddleware(generator func() string) *RequestIDMiddleware

NewRequestIDMiddleware creates a new request ID middleware.

func (*RequestIDMiddleware) HandleRequest

func (m *RequestIDMiddleware) HandleRequest(req *http.Request) error

HandleRequest adds a request ID to the request.

func (*RequestIDMiddleware) HandleResponse

func (m *RequestIDMiddleware) HandleResponse(resp *Response) error

HandleResponse is a no-op for request ID middleware.

type RequestOption

type RequestOption func(*Request)

RequestOption configures a request.

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader adds a header.

func WithHeaders

func WithHeaders(headers http.Header) RequestOption

WithHeaders adds multiple headers.

func WithQuery

func WithQuery(params url.Values) RequestOption

WithQuery adds query parameters.

func WithSkipCircuit

func WithSkipCircuit() RequestOption

WithSkipCircuit skips circuit breaker for this request.

func WithSkipRetry

func WithSkipRetry() RequestOption

WithSkipRetry skips retry for this request.

func WithTimeout

func WithTimeout(timeout time.Duration) RequestOption

WithTimeout sets the request timeout.

type Response

type Response struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
	Duration   time.Duration
}

Response represents an HTTP response.

func (*Response) IsClientError

func (r *Response) IsClientError() bool

IsClientError returns true if the response has a 4xx status code.

func (*Response) IsServerError

func (r *Response) IsServerError() bool

IsServerError returns true if the response has a 5xx status code.

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the response has a 2xx status code.

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(v interface{}) error

UnmarshalJSON unmarshals the response body into the given value.

type RetryMiddleware

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

RetryMiddleware handles retry logic.

func NewRetryMiddleware

func NewRetryMiddleware() *RetryMiddleware

NewRetryMiddleware creates a new retry middleware.

func (*RetryMiddleware) HandleRequest

func (m *RetryMiddleware) HandleRequest(req *http.Request) error

HandleRequest is a no-op for retry middleware.

func (*RetryMiddleware) HandleResponse

func (m *RetryMiddleware) HandleResponse(resp *Response) error

HandleResponse is a no-op for retry middleware.

Jump to

Keyboard shortcuts

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