Documentation ¶
Overview ¶
Package httpclient provides a flexible HTTP client with caching capabilities.
The package offers two main types of clients: 1. A basic HTTP client with simplified request methods 2. A cached client that supports automatic background updates
Basic usage:
client := httpclient.NewClient("https://api.example.com") resp, err := client.Get(context.Background(), "/users")
Cached client usage:
client := httpclient.NewCachedClient("https://api.example.com") defer client.Stop()
Index ¶
- type CacheConfig
- type CacheEntry
- type CachedClient
- func (c *CachedClient) GetCached(path string) (interface{}, error)
- func (c *CachedClient) GetCachedOrFetch(ctx context.Context, path string) (interface{}, error)
- func (c *CachedClient) SetupCachedEndpoint(ctx context.Context, config CacheConfig, result interface{}) error
- func (c *CachedClient) Stop()
- func (c *CachedClient) StopCacheUpdates(path string)
- type Client
- func (c *Client) Delete(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Get(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Post(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*http.Response, error)
- func (c *Client) Put(ctx context.Context, path string, body interface{}, opts ...RequestOption) (*http.Response, error)
- type Option
- type RequestOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheConfig ¶
type CacheConfig struct { Path string // API endpoint path CronSpec string // Cron specification for updates Expiration time.Duration // How long the cache is valid SkipInitialFetch bool // Skip the initial fetch on startup }
CacheConfig defines the configuration for a cached endpoint
type CacheEntry ¶
type CachedClient ¶
type CachedClient struct { *Client // contains filtered or unexported fields }
CachedClient extends our base HTTP client with caching capabilities
func NewCachedClient ¶
func NewCachedClient(baseURL string, opts ...Option) *CachedClient
func (*CachedClient) GetCached ¶
func (c *CachedClient) GetCached(path string) (interface{}, error)
GetCached retrieves data from cache if available and not expired
func (*CachedClient) GetCachedOrFetch ¶
func (c *CachedClient) GetCachedOrFetch(ctx context.Context, path string) (interface{}, error)
GetCachedOrFetch gets data from cache if available and not expired, otherwise fetches fresh data
func (*CachedClient) SetupCachedEndpoint ¶
func (c *CachedClient) SetupCachedEndpoint(ctx context.Context, config CacheConfig, result interface{}) error
SetupCachedEndpoint configures automatic updates for a specific endpoint
func (*CachedClient) Stop ¶
func (c *CachedClient) Stop()
func (*CachedClient) StopCacheUpdates ¶
func (c *CachedClient) StopCacheUpdates(path string)
StopCacheUpdates stops the cron job for a specific endpoint
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) Delete ¶
func (c *Client) Delete(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error)
Delete performs a DELETE request
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, path string, opts ...RequestOption) (*http.Response, error)
Get performs a GET request
type Option ¶
type Option func(*Client)
type RequestOption ¶ added in v1.0.4
RequestOption defines per-request configuration
func WithRequestHeader ¶ added in v1.0.4
func WithRequestHeader(key, value string) RequestOption
WithRequestHeader adds a header for a single request