Documentation
¶
Index ¶
- func DoWithData[T any](client *Client, ctx context.Context, req *Request, opts ...Option) (*T, error)
- func Get[T any](client *Client, ctx context.Context, url string, opts ...Option) (*T, error)
- func Post[T any](client *Client, ctx context.Context, url string, data interface{}, ...) (*T, error)
- func Put[T any](client *Client, ctx context.Context, url string, data interface{}, ...) (*T, error)
- type BreakerManager
- type Client
- func (c *Client) Delete(ctx context.Context, url string, opts ...Option) (*Response, error)
- func (c *Client) Do(ctx context.Context, req *Request, opts ...Option) (*Response, error)
- func (c *Client) Get(ctx context.Context, url string, opts ...Option) (*Response, error)
- func (c *Client) Post(ctx context.Context, url string, opts ...Option) (*Response, error)
- func (c *Client) Put(ctx context.Context, url string, opts ...Option) (*Response, error)
- type Option
- func DisableBreaker() Option
- func DisableRetry() Option
- func WithAfterResponse(fn func(*Response) error) Option
- func WithBaseURL(baseURL string) Option
- func WithBeforeRequest(fn func(*http.Request) error) Option
- func WithBody(reader io.Reader) Option
- func WithBodyString(s string) Option
- func WithBreaker(manager BreakerManager) Option
- func WithBreakerFallback(fallback func(ctx context.Context, err error) (*Response, error)) Option
- func WithBreakerResource(resource string) Option
- func WithContext(ctx context.Context) Option
- func WithCookieJar(jar http.CookieJar) Option
- func WithForm(data map[string]string) Option
- func WithHeader(key, value string) Option
- func WithHeaders(headers map[string]string) Option
- func WithInsecureSkipVerify() Option
- func WithJSON(data interface{}) Option
- func WithQueries(queries url.Values) Option
- func WithQuery(key, value string) Option
- func WithRetry(opts ...retry.Option) Option
- func WithRetryDefaults() Option
- func WithTimeout(duration time.Duration) Option
- func WithTransport(transport *http.Transport) Option
- type Request
- func (r *Request) Clone() *Request
- func (r *Request) WithBody(body io.Reader) *Request
- func (r *Request) WithForm(data map[string]string) *Request
- func (r *Request) WithHeader(key, value string) *Request
- func (r *Request) WithJSON(data interface{}) *Request
- func (r *Request) WithQuery(key, value string) *Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoWithData ¶
func DoWithData[T any](client *Client, ctx context.Context, req *Request, opts ...Option) (*T, error)
DoWithData executes the request and automatically deserializes (generic)
Types ¶
type BreakerManager ¶
type BreakerManager interface {
// Execute the protected operation
Execute(ctx context.Context, req *breaker.Request) (interface{}, error)
// Check if the circuit breaker is enabled
IsEnabled() bool
// GetState Retrieve the current state of the resource
GetState(resource string) breaker.State
}
BreakerManager circuit breaker management interface (for decoupling)
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client HTTP client
type Option ¶
type Option func(*config)
Option configuration option type
func WithAfterResponse ¶
Sets the post-response hook
func WithBeforeRequest ¶
WithBeforeRequest set request pre-hook
func WithBreakerFallback ¶
WithBreakerFallback set circuit breaker fallback logic
func WithBreakerResource ¶
WithBreakerResource sets the circuit breaker resource name (defaults to URL)
func WithHeaders ¶
WithHeaders set multiple headers
func WithInsecureSkipVerify ¶
func WithInsecureSkipVerify() Option
WithInsecureSkipVerify skip TLS verification (insecure, for development environment only)
func WithJSON ¶
func WithJSON(data interface{}) Option
WithJSON sets the JSON Body (automatically sets Content-Type)
func WithQueries ¶
WithQueries set multiple Query parameters
func WithTransport ¶
WithTransport set custom Transport
type Request ¶
type Request struct {
Method string
URL string
Headers map[string]string
Query url.Values
Body io.Reader
// contains filtered or unexported fields
}
HTTP request encapsulation
type Response ¶
type Response struct {
StatusCode int
Status string
Headers http.Header
Body []byte
RawResponse *http.Response
// Extend fields
Duration time.Duration // Total request duration
Attempts int // Number of retries
}
Encapsulate HTTP response
func (*Response) IsClientError ¶
determines if it is a client error (4xx)
func (*Response) IsServerError ¶
Check if it is a server error (5xx)