client

package
v0.0.0-...-32810c0 Latest Latest
Warning

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

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

Documentation

Overview

Package client provides HTTP and JSON-RPC client implementations for the Alchemy SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NextRequestID

func NextRequestID() uint64

NextRequestID returns the next request ID.

func WithRetry

func WithRetry(ctx context.Context, retrier *Retrier, fn RetryableFunc) error

WithRetry wraps a function to be executed with retries.

Types

type BatchCall

type BatchCall struct {
	Method string
	Params []interface{}
	Result interface{} // pointer to result type
}

BatchCall represents a single call in a batch request.

type BatchCallResponse

type BatchCallResponse struct {
	JSONRPC string               `json:"jsonrpc"`
	ID      uint64               `json:"id"`
	Result  json.RawMessage      `json:"result,omitempty"`
	Error   *errors.JSONRPCError `json:"error,omitempty"`
}

BatchCallResponse holds the response for a batch call item.

type BatchResult

type BatchResult struct {
	Error  error
	Result interface{}
}

BatchResult represents the result of a single call in a batch request.

type HTTPClient

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

HTTPClient is the HTTP client for making API requests.

func NewHTTPClient

func NewHTTPClient(cfg HTTPClientConfig) *HTTPClient

NewHTTPClient creates a new HTTPClient.

func (*HTTPClient) BaseURL

func (c *HTTPClient) BaseURL() string

BaseURL returns the base URL.

func (*HTTPClient) Do

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

Do executes an HTTP request with retry and middleware support.

func (*HTTPClient) Get

func (c *HTTPClient) Get(ctx context.Context, path string) ([]byte, error)

Get makes a GET request.

func (*HTTPClient) GetWithQuery

func (c *HTTPClient) GetWithQuery(ctx context.Context, path string, query map[string]string) ([]byte, error)

GetWithQuery makes a GET request with query parameters.

func (*HTTPClient) Post

func (c *HTTPClient) Post(ctx context.Context, path string, body interface{}) ([]byte, error)

Post makes a POST request with JSON body.

type HTTPClientConfig

type HTTPClientConfig struct {
	BaseURL       string
	APIKey        string
	Timeout       time.Duration
	MaxRetries    int
	RetryDelay    time.Duration
	RetryMaxDelay time.Duration
	HTTPClient    *http.Client
	Middlewares   []Middleware
	Debug         bool
}

HTTPClientConfig holds configuration for HTTPClient.

type Handler

type Handler func(ctx context.Context, req *http.Request) (*http.Response, error)

Handler is a function that processes an HTTP request.

type HeaderMiddleware

type HeaderMiddleware struct {
	Headers map[string]string
}

HeaderMiddleware adds custom headers to requests.

func NewHeaderMiddleware

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

NewHeaderMiddleware creates a new HeaderMiddleware.

func (*HeaderMiddleware) Wrap

func (m *HeaderMiddleware) Wrap(next Handler) Handler

Wrap implements Middleware.

type JSONRPCClient

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

JSONRPCClient is a client for making JSON-RPC calls.

func NewJSONRPCClient

func NewJSONRPCClient(httpClient *HTTPClient) *JSONRPCClient

NewJSONRPCClient creates a new JSONRPCClient.

func (*JSONRPCClient) BatchCall

func (c *JSONRPCClient) BatchCall(ctx context.Context, calls []BatchCall) ([]BatchResult, error)

BatchCall makes multiple JSON-RPC calls in a single HTTP request.

func (*JSONRPCClient) Call

func (c *JSONRPCClient) Call(ctx context.Context, method string, params []interface{}, result interface{}) error

Call makes a JSON-RPC call and unmarshals the result.

func (*JSONRPCClient) CallRaw

func (c *JSONRPCClient) CallRaw(ctx context.Context, method string, params []interface{}) (json.RawMessage, error)

CallRaw makes a JSON-RPC call and returns the raw result.

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string        `json:"jsonrpc"`
	Method  string        `json:"method"`
	Params  []interface{} `json:"params,omitempty"`
	ID      uint64        `json:"id"`
}

JSONRPCRequest represents a JSON-RPC request.

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string               `json:"jsonrpc"`
	ID      uint64               `json:"id"`
	Result  json.RawMessage      `json:"result,omitempty"`
	Error   *errors.JSONRPCError `json:"error,omitempty"`
}

JSONRPCResponse represents a JSON-RPC response.

type LoggingMiddleware

type LoggingMiddleware struct {
	Logger *slog.Logger
}

LoggingMiddleware logs HTTP requests and responses.

func NewLoggingMiddleware

func NewLoggingMiddleware(logger *slog.Logger) *LoggingMiddleware

NewLoggingMiddleware creates a new LoggingMiddleware.

func (*LoggingMiddleware) Wrap

func (m *LoggingMiddleware) Wrap(next Handler) Handler

Wrap implements Middleware.

type MetricsMiddleware

type MetricsMiddleware struct {
	// OnRequest is called before a request is made.
	OnRequest func(method, url string)
	// OnResponse is called after a response is received.
	OnResponse func(method, url string, statusCode int, duration time.Duration, err error)
}

MetricsMiddleware collects request metrics.

func NewMetricsMiddleware

func NewMetricsMiddleware(onRequest func(method, url string), onResponse func(method, url string, statusCode int, duration time.Duration, err error)) *MetricsMiddleware

NewMetricsMiddleware creates a new MetricsMiddleware.

func (*MetricsMiddleware) Wrap

func (m *MetricsMiddleware) Wrap(next Handler) Handler

Wrap implements Middleware.

type Middleware

type Middleware interface {
	// Wrap wraps a handler with middleware logic.
	Wrap(next Handler) Handler
}

Middleware is an interface for HTTP middleware.

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain chains multiple middlewares together.

type MiddlewareFunc

type MiddlewareFunc func(next Handler) Handler

MiddlewareFunc is a function that implements Middleware.

func (MiddlewareFunc) Wrap

func (f MiddlewareFunc) Wrap(next Handler) Handler

Wrap implements Middleware.

type Retrier

type Retrier struct {
	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int
	// InitialDelay is the initial delay between retries.
	InitialDelay time.Duration
	// MaxDelay is the maximum delay between retries.
	MaxDelay time.Duration
	// Multiplier is the factor by which the delay increases.
	Multiplier float64
	// Jitter adds randomness to the delay (0.0 to 1.0).
	Jitter float64
}

Retrier implements exponential backoff retry logic.

func DefaultRetrier

func DefaultRetrier() *Retrier

DefaultRetrier returns a Retrier with default settings.

func (*Retrier) Do

func (r *Retrier) Do(ctx context.Context, fn func() error) error

Do executes the given function with retries.

func (*Retrier) ShouldRetry

func (r *Retrier) ShouldRetry(err error) bool

ShouldRetry determines if an error is retryable.

type RetryableFunc

type RetryableFunc func() error

RetryableFunc is a function that can be retried.

type UserAgentMiddleware

type UserAgentMiddleware struct {
	UserAgent string
}

UserAgentMiddleware sets the User-Agent header.

func NewUserAgentMiddleware

func NewUserAgentMiddleware(userAgent string) *UserAgentMiddleware

NewUserAgentMiddleware creates a new UserAgentMiddleware.

func (*UserAgentMiddleware) Wrap

func (m *UserAgentMiddleware) Wrap(next Handler) Handler

Wrap implements Middleware.

Jump to

Keyboard shortcuts

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