http

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

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

type APIError struct {
	StatusCode int
	Body       []byte
}

APIError is an error returned by the client when the API returns a non-200 status code

func NewAPIError added in v3.1.0

func NewAPIError(status int, body []byte) *APIError

NewAPIError returns a new APIError

func (*APIError) Error added in v3.0.3

func (e *APIError) Error() string

type ApiHeaderFn

type ApiHeaderFn func(*http.Request)

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

func NewClient

func NewClient(opts ...option[*Client]) *Client

NewClient creates a new Client with the given options

func (*Client) MakeReq

func (c *Client) MakeReq(ctx context.Context, url string) ([]byte, error)

MakeReq makes a GET request to the given URL with the configured client It returns the response body as bytes or an error if the request fails

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

func (*RateLimitedClient) MakeReq added in v3.1.0

func (c *RateLimitedClient) MakeReq(ctx context.Context, url string) ([]byte, error)

MakeReq makes a GET request to the given URL with the configured client It respects the configured rate limit before making the request It returns the response body as bytes or an error if the request fails

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.

Jump to

Keyboard shortcuts

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