Documentation
¶
Overview ¶
Package client provides HTTP and JSON-RPC client implementations for the Alchemy SDK.
Index ¶
- func NextRequestID() uint64
- func WithRetry(ctx context.Context, retrier *Retrier, fn RetryableFunc) error
- type BatchCall
- type BatchCallResponse
- type BatchResult
- type HTTPClient
- func (c *HTTPClient) BaseURL() string
- func (c *HTTPClient) Do(ctx context.Context, req *http.Request) (*http.Response, error)
- func (c *HTTPClient) Get(ctx context.Context, path string) ([]byte, error)
- func (c *HTTPClient) GetWithQuery(ctx context.Context, path string, query map[string]string) ([]byte, error)
- func (c *HTTPClient) Post(ctx context.Context, path string, body interface{}) ([]byte, error)
- type HTTPClientConfig
- type Handler
- type HeaderMiddleware
- type JSONRPCClient
- func (c *JSONRPCClient) BatchCall(ctx context.Context, calls []BatchCall) ([]BatchResult, error)
- func (c *JSONRPCClient) Call(ctx context.Context, method string, params []interface{}, result interface{}) error
- func (c *JSONRPCClient) CallRaw(ctx context.Context, method string, params []interface{}) (json.RawMessage, error)
- type JSONRPCRequest
- type JSONRPCResponse
- type LoggingMiddleware
- type MetricsMiddleware
- type Middleware
- type MiddlewareFunc
- type Retrier
- type RetryableFunc
- type UserAgentMiddleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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) GetWithQuery ¶
func (c *HTTPClient) GetWithQuery(ctx context.Context, path string, query map[string]string) ([]byte, error)
GetWithQuery makes a GET request with query parameters.
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 HeaderMiddleware ¶
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 ¶
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 ¶
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) ShouldRetry ¶
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.