reqx

package
v1.0.75 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// JsonApplicationContentType - JSON content type header
	JsonApplicationContentType string = "application/json"
	// FormUrlEncodedApplicationContentType - x-www-form-urlencoded content type header
	FormUrlEncodedApplicationContentType string = "application/x-www-form-urlencoded"
)

Variables

View Source
var (
	ErrResponseNil     = errors.New("response is nil")
	ErrResponseBodyNil = errors.New("response body is nil")
)
View Source
var (
	ErrInterceptorNotFound = fmt.Errorf("interceptor not found")
	ErrMaxRetriesExceeded  = fmt.Errorf("max interceptor retries exceeded")
)

Functions

func Bytes

func Bytes(resp *req.Response, err error) ([]byte, error)

Bytes extracts bytes from response - use for chaining: client.R().Get(url).Bytes()

func Stream

func Stream(resp *req.Response, err error) (io.ReadCloser, error)

Stream extracts stream from response

func String

func String(resp *req.Response, err error) (string, error)

String extracts string from response - use for chaining: client.R().Get(url).String()

Types

type Client

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

Client wraps req.Client and provides both authenticated and normal requests

func NewClient

func NewClient(opts Options, configs ...Config) *Client

NewClient creates a new wrapped client

func (*Client) AR

func (c *Client) AR(serviceLabel string) *req.Request

AR creates an authenticated request with automatic token refresh

func (*Client) IR

func (c *Client) IR(label string) *req.Request

IR creates an intercepted request with automatic recovery

func (*Client) R

func (c *Client) R() *req.Request

R creates a normal request builder

func (*Client) RegisterAuth

func (c *Client) RegisterAuth(serviceLabel string, identityReader cred.IdentityReader, refreshFunc func(*Client, cred.Identity) (string, error)) *Client

RegisterAuth registers an authentication provider using a refreshable token with the given label

func (*Client) RegisterInterceptor

func (c *Client) RegisterInterceptor(label string, interceptor Interceptor) *Client

RegisterInterceptor registers an interceptor with the given label

func (*Client) RegisterToken

func (c *Client) RegisterToken(serviceLabel string, token string) *Client

RegisterToken registers an authentication provider using a fixed token with the given label

func (*Client) UnregisterAuth

func (c *Client) UnregisterAuth(serviceLabel string)

UnregisterAuth unregisters an authentication provider with the given label

func (*Client) UnregisterInterceptor

func (c *Client) UnregisterInterceptor(label string)

UnregisterInterceptor unregisters an interceptor with the given label

type Config

type Config func(*req.Client)

Config is a function that configures the underlying req.Client (for advanced use cases)

type Impersonation

type Impersonation byte
const (
	None Impersonation = iota
	Chrome
	Firefox
	Safari
)

type Interceptor

type Interceptor interface {
	// ShouldIntercept returns true if this response/error should trigger recovery.
	// Both resp and err are provided so status codes can be checked (e.g. 429, 401)
	// even when they're wrapped as errors by the retry logic.
	ShouldIntercept(resp *req.Response, err error) bool

	// Recover performs the recovery action (refresh cookies, re-login, etc.)
	// This is called when ShouldIntercept returns true.
	// The implementation should store any state it needs for Apply.
	Recover(client *Client, resp *req.Response) error

	// Apply applies the current state to the request (set cookies, headers, etc.)
	// This is called before every request attempt to apply stored state.
	Apply(r *req.Request) *req.Request

	// MaxRetries returns the maximum number of recovery attempts per request.
	// Return 0 or negative to use the default (1).
	MaxRetries() int
}

Interceptor handles response interception and recovery.

Thread safety: Implementations do NOT need to handle synchronization. The interceptor system guarantees that Recover and Apply are never called concurrently.

Thundering herd prevention: When multiple concurrent requests trigger recovery, only one Recover call executes. Other goroutines waiting will skip recovery and use the newly recovered state.

type Options

type Options struct {
	RetryCount        int
	MinBackoff        time.Duration
	MaxBackoff        time.Duration
	Timeout           time.Duration
	AuthRefreshBuffer time.Duration
	EnableHttp3       bool
	AutoDecompress    bool
	AutoDecode        bool
	DisableKeepAlives bool
	Impersonation     Impersonation
}

Options settings to configure the retryable HTTP client

type RefreshTokenFunc

type RefreshTokenFunc func(client *Client, identity cred.Identity) (string, error)

RefreshTokenFunc is a function that refreshes the token for the given identity

Jump to

Keyboard shortcuts

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