request

package
v0.0.71 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTooManyAttempts = errors.New("too many attempts")

Functions

This section is empty.

Types

type ConfigOpt

type ConfigOpt func(opts *configOpts)

func WithBackoff

func WithBackoff(backoff RetryBackoff) ConfigOpt

WithBackoff sets the backoff strategy for the http client.

func WithBackoffDuration

func WithBackoffDuration(dur time.Duration) ConfigOpt

WithBackoffDuration sets the backoff duration for the http client.

func WithDNS

func WithDNS(dns dns.DNS) ConfigOpt

WithDNS sets the dns resolver for the http client.

func WithMaxAttempts

func WithMaxAttempts(max uint) ConfigOpt

WithMaxAttempts sets the max number of attempts for the http client.

func WithMaxConcurrency

func WithMaxConcurrency(max uint64) ConfigOpt

WithMaxConcurrency sets the max number of concurrent requests.

func WithRecorder

func WithRecorder(recorder Recorder) ConfigOpt

WithRecorder sets the recorder for the http client.

func WithTimeout

func WithTimeout(timeout time.Duration) ConfigOpt

WithTimeout sets the timeout for the http client.

type HTTPRequest

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

func (*HTTPRequest) Headers

func (r *HTTPRequest) Headers() map[string]string

func (*HTTPRequest) Method

func (r *HTTPRequest) Method() string

func (*HTTPRequest) Payload

func (r *HTTPRequest) Payload() []byte

func (*HTTPRequest) URL

func (r *HTTPRequest) URL() string

type Http

type Http interface {
	// Deliver sends a request and returns a response.
	Deliver(ctx context.Context, request Request) (*Response, error)
}

Http is an interface for making HTTP requests.

func New

func New(opts ...ConfigOpt) Http

New returns a new HTTP implementation.

type Recorder

type Recorder interface {
	OnResponse(ctx context.Context, req Request, resp *Response)
}

Recorder is an interface for recording request / responses.

type Request

type Request interface {
	// Method returns the HTTP method.
	Method() string
	// URL returns the URL.
	URL() string
	// Headers returns the headers.
	Headers() map[string]string
	// Payload returns the payload.
	Payload() []byte
}

Request is an interface for an HTTP request.

func NewHTTPGetRequest

func NewHTTPGetRequest(url string, headers map[string]string) Request

NewHTTPGetRequest creates a new HTTPRequest that implements the Request interface for GET requests.

func NewHTTPPostRequest

func NewHTTPPostRequest(url string, headers map[string]string, payload []byte) Request

NewHTTPPostRequest creates a new HTTPRequest that implements the Request interface for POST requests.

func NewHTTPRequest

func NewHTTPRequest(method string, url string, headers map[string]string, payload []byte) Request

NewHTTPRequest creates a new HTTPRequest that implements the Request interface.

type Response

type Response struct {
	StatusCode int               `json:"statusCode"`
	Body       []byte            `json:"body,omitempty"`
	Headers    map[string]string `json:"headers"`
	Attempts   uint              `json:"attempts"`
	Latency    time.Duration     `json:"latency"`
}

Response is the response from an HTTP request.

type RetryBackoff

type RetryBackoff interface {
	// BackOff returns the duration to wait before retrying.
	BackOff(attempt uint) time.Duration
}

RetryBackoff is an interface for retrying a request with a backoff.

func NewMinMaxBackoff

func NewMinMaxBackoff(min time.Duration, max time.Duration) RetryBackoff

NewMinMaxBackoff creates a new RetryBackoff that retries with a backoff of min * 2^attempt.

Jump to

Keyboard shortcuts

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