middleware

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware components for the anytype client

Package middleware provides HTTP middleware components for the anytype client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCustomRetry

func WithCustomRetry(config RetryConfig) func(HTTPDoer) HTTPDoer

WithCustomRetry returns a middleware function that applies retry logic with custom configuration

func WithRetry

func WithRetry() func(HTTPDoer) HTTPDoer

WithRetry returns a middleware function that applies retry logic with default configuration

func WithValidator

func WithValidator(ctx context.Context, validator Validator) context.Context

WithValidator adds a validator to the context

Types

type Chain

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

Chain represents a middleware chain

func NewChain

func NewChain(client HTTPDoer) *Chain

NewChain creates a new middleware chain with the given client

func (*Chain) Build

func (c *Chain) Build() HTTPDoer

Build constructs the final HTTP client with all middlewares applied

func (*Chain) Use

func (c *Chain) Use(middleware func(HTTPDoer) HTTPDoer) *Chain

Use adds a middleware to the chain

type DisconnectConfig

type DisconnectConfig struct {
	// ReconnectDelay is the delay before attempting to reconnect
	ReconnectDelay time.Duration
	// MaxReconnectAttempts is the maximum number of reconnect attempts
	MaxReconnectAttempts int
	// OnDisconnect is called when a disconnection is detected
	OnDisconnect func()
	// OnReconnect is called when a connection is re-established
	OnReconnect func()
}

DisconnectConfig configures the disconnect handling middleware

func DefaultDisconnectConfig

func DefaultDisconnectConfig() DisconnectConfig

DefaultDisconnectConfig provides sensible defaults for disconnect configuration

type DisconnectMiddleware

type DisconnectMiddleware struct {
	Next   HTTPDoer
	Config DisconnectConfig
	// contains filtered or unexported fields
}

DisconnectMiddleware handles temporary network disconnections

func NewDisconnectMiddleware

func NewDisconnectMiddleware(next HTTPDoer, config DisconnectConfig) *DisconnectMiddleware

NewDisconnectMiddleware creates a new disconnect handling middleware

func (*DisconnectMiddleware) Do

Do executes an HTTP request with disconnect handling

func (*DisconnectMiddleware) IsConnected

func (m *DisconnectMiddleware) IsConnected() bool

IsConnected returns the current connection status

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPDoer represents any client capable of executing HTTP requests

type RetryConfig

type RetryConfig struct {
	// MaxRetries is the maximum number of retries
	MaxRetries int
	// RetryDelay is the base delay between retries
	RetryDelay time.Duration
	// MaxRetryDelay is the maximum delay between retries
	MaxRetryDelay time.Duration
	// RetryableStatusCodes defines HTTP status codes that should trigger a retry
	RetryableStatusCodes []int
	// ShouldRetry is a custom function to determine if a request should be retried
	ShouldRetry func(*http.Response, error) bool
}

RetryConfig configures the retry middleware

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig provides sensible defaults for retry configuration

type RetryMiddleware

type RetryMiddleware struct {
	Next   HTTPDoer
	Config RetryConfig
}

RetryMiddleware implements a retry strategy for HTTP requests

func NewRetryMiddleware

func NewRetryMiddleware(next HTTPDoer, config RetryConfig) *RetryMiddleware

NewRetryMiddleware creates a new retry middleware with the given configuration

func (*RetryMiddleware) Do

func (m *RetryMiddleware) Do(req *http.Request) (*http.Response, error)

Do executes an HTTP request with retries according to the retry policy

type ValidationMiddleware

type ValidationMiddleware struct {
	Next HTTPDoer
}

ValidationMiddleware validates requests before they are sent

func NewValidationMiddleware

func NewValidationMiddleware(next HTTPDoer) *ValidationMiddleware

NewValidationMiddleware creates a new validation middleware

func (*ValidationMiddleware) Do

Do executes an HTTP request after validating any validator in the context

type Validator

type Validator interface {
	Validate() error
}

Validator is an interface for types that can validate themselves

func GetValidator

func GetValidator(ctx context.Context) (Validator, bool)

GetValidator retrieves a validator from the context

Jump to

Keyboard shortcuts

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