Documentation
¶
Index ¶
- func WithApiHeaderFn[T ClientLike](fn ApiHeaderFn) option[T]
- func WithHttpClient[T ClientLike](client *http.Client) option[T]
- func WithRateLimit[T ClientLike](requestsPerMinute int) option[T]
- func WithRateLimiter[T ClientLike](limiter *rate.Limiter) option[T]
- func WithRetryPolicy[T ClientLike](maxRetries int, baseDelay time.Duration) option[T]
- type APIError
- type ApiHeaderFn
- type Client
- type ClientLike
- type HttpClient
- type HttpRateLimitedlient
- type RateLimitedClient
- type RetryPolicy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithApiHeaderFn ¶
func WithApiHeaderFn[T ClientLike](fn ApiHeaderFn) option[T]
WithApiHeaderFn returns an option that sets a function to add API headers to requests
func WithHttpClient ¶
func WithHttpClient[T ClientLike](client *http.Client) option[T]
WithHttpClient returns an option that sets the HTTP client used by the client
func WithRateLimit ¶ added in v3.1.0
func WithRateLimit[T ClientLike](requestsPerMinute int) option[T]
WithRateLimit returns an option that sets a custom rate limiter for RateLimitedClient This option only works with RateLimitedClient - it's a no-op for regular Client
func WithRateLimiter ¶ added in v3.1.0
func WithRateLimiter[T ClientLike](limiter *rate.Limiter) option[T]
WithRateLimiter returns an option that sets a custom rate limiter instance for RateLimitedClient This option only works with RateLimitedClient - it's a no-op for regular Client
func WithRetryPolicy ¶ added in v3.1.0
func WithRetryPolicy[T ClientLike](maxRetries int, baseDelay time.Duration) option[T]
WithRetryPolicy returns an option that sets a custom retry policy for RateLimitedClient. It configures how many times to retry failed requests and the base delay between retries. This option only works with RateLimitedClient - it's a no-op for regular Client.
The retry policy uses exponential backoff, where each retry waits longer than the previous one. The delay for retry n is calculated as: baseDelay * 2^n
For example, with baseDelay=2s: - First retry: 2s - Second retry: 4s - Third retry: 8s - Fourth retry: 16s - Fifth retry: 32s
Types ¶
type APIError ¶ added in v3.0.3
APIError is an error returned by the client when the API returns a non-200 status code
func NewAPIError ¶ added in v3.1.0
NewAPIError returns a new APIError
type ApiHeaderFn ¶
ApiHeaderFn is a function type that sets headers on HTTP requests
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP client that can make API requests with optional headers and rate limiting
type ClientLike ¶ added in v3.1.0
type ClientLike interface {
*Client | *RateLimitedClient
}
ClientLike is a constraint interface that defines the common behavior that both Client and RateLimitedClient should have
type HttpClient ¶ added in v3.1.0
type HttpClient interface {
// MakeReq makes a GET request to the given URL.
// It returns the response body as bytes and any error that occurred.
// The context can be used to cancel the request or set a timeout.
MakeReq(ctx context.Context, url string) ([]byte, error)
}
HttpClient defines the interface for making HTTP requests. It provides a method to make GET requests and return the response body as bytes.
type HttpRateLimitedlient ¶ added in v3.1.0
type HttpRateLimitedlient interface {
HttpClient
// GetRateLimitInfo returns the current rate limit configuration and state.
// It returns:
// - limit: The maximum number of requests allowed per second
// - burst: The maximum number of requests that can be made in a single burst
// - tokens: The current number of available tokens
GetRateLimitInfo() (limit rate.Limit, burst, tokens int)
}
HttpRateLimitedlient is an interface that extends HttpClient to provide rate limiting functionality. It adds the ability to query the current rate limit configuration and state.
type RateLimitedClient ¶ added in v3.1.0
type RateLimitedClient struct {
Client
// contains filtered or unexported fields
}
RateLimitedClient extends Client to add rate limiting functionality. It uses a token bucket rate limiter to control request frequency.
func NewRateLimitedClient ¶ added in v3.1.0
func NewRateLimitedClient(opts ...option[*RateLimitedClient]) *RateLimitedClient
NewRateLimitedClient creates a new RateLimitedClient with the given options By default, it applies CoinGecko's free tier rate limit of 30 requests per minute
func (*RateLimitedClient) GetRateLimitInfo ¶ added in v3.1.0
func (c *RateLimitedClient) GetRateLimitInfo() (limit rate.Limit, burst, tokens int)
GetRateLimitInfo returns information about the current rate limiter state
type RetryPolicy ¶ added in v3.1.0
type RetryPolicy struct {
// contains filtered or unexported fields
}
RetryPolicy defines the configuration for retrying failed requests. It specifies how many times to retry and the base delay between retries.