httpx

package
v0.0.0-...-a852ce6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MockConnectionError = MockResponse{Status: 0, Headers: nil, Body: []byte{}, BodyIsString: true, BodyRepeat: 0}

MockConnectionError mocks a connection error

Functions

func DefaultShouldRetry

func DefaultShouldRetry(request *http.Request, response *http.Response, withDelay time.Duration) bool

DefaultShouldRetry is the default function for determining if a response should be retried

func DetectContentType

func DetectContentType(d []byte) string

DetectContentType is a drop in replacement for http.DetectContentType which leans on the github.com/gabriel-vasile/mimetype library

func Do

func Do(client *http.Client, request *http.Request, retries *RetryConfig, access *AccessConfig) (*http.Response, error)

Do makes the given HTTP request using the current requestor and retry config

func NewRequest

func NewRequest(method string, url string, body io.Reader, headers map[string]string) (*http.Request, error)

NewRequest is a convenience method to create a request with the given headers

func ParseRetryAfter

func ParseRetryAfter(value string) time.Duration

ParseRetryAfter parses value of Retry-After headers which can be date or delay in seconds see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After

func SetDebug

func SetDebug(enabled bool)

SetDebug enables debugging

func SetRequestor

func SetRequestor(requestor Requestor)

SetRequestor sets the requestor used by Request

Types

type AccessConfig

type AccessConfig struct {
	ResolveTimeout time.Duration
	DisallowedIPs  []net.IP
	DisallowedNets []*net.IPNet
}

AccessConfig configures what can be accessed

func NewAccessConfig

func NewAccessConfig(resolveTimeout time.Duration, disallowedIPs []net.IP, disallowedNets []*net.IPNet) *AccessConfig

NewAccessConfig creates a new access config

func (*AccessConfig) Allow

func (c *AccessConfig) Allow(request *http.Request) (bool, error)

Allow determines whether the given request should be allowed

type MockRequestor

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

MockRequestor is a requestor which can be mocked with responses for given URLs

func NewMockRequestor

func NewMockRequestor(mocks map[string][]MockResponse) *MockRequestor

NewMockRequestor creates a new mock requestor with the given mocks

func (*MockRequestor) Clone

func (r *MockRequestor) Clone() *MockRequestor

Clone returns a clone of this requestor

func (*MockRequestor) Do

func (r *MockRequestor) Do(client *http.Client, request *http.Request) (*http.Response, error)

Do returns the mocked reponse for the given request

func (*MockRequestor) HasUnused

func (r *MockRequestor) HasUnused() bool

HasUnused returns true if there are unused mocks leftover

func (*MockRequestor) MarshalJSON

func (r *MockRequestor) MarshalJSON() ([]byte, error)

func (*MockRequestor) UnmarshalJSON

func (r *MockRequestor) UnmarshalJSON(data []byte) error

type MockResponse

type MockResponse struct {
	Status       int
	Headers      map[string]string
	Body         []byte
	BodyIsString bool
	BodyRepeat   int
}

func NewMockResponse

func NewMockResponse(status int, headers map[string]string, body string) MockResponse

NewMockResponse creates a new mock response from a string

func (MockResponse) Make

func (m MockResponse) Make(request *http.Request) *http.Response

Make mocks making the given request and returning this as the response

func (*MockResponse) MarshalJSON

func (m *MockResponse) MarshalJSON() ([]byte, error)

func (*MockResponse) UnmarshalJSON

func (m *MockResponse) UnmarshalJSON(data []byte) error

type Recorder

type Recorder struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	// contains filtered or unexported fields
}

Recorder is a utility for creating traces of HTTP requests being handled

func NewRecorder

func NewRecorder(r *http.Request, w http.ResponseWriter) *Recorder

NewRecorder creates a new recorder for an HTTP request

func (*Recorder) End

func (r *Recorder) End() (*Trace, error)

End is called when the response has been written and generates the trace

func (*Recorder) SaveRequest

func (r *Recorder) SaveRequest() error

SaveRequest immediately saves the request and body for later use. This can be called to guarantee the body is available at a later time even if downstream users of the request do not clone the body

type Requestor

type Requestor interface {
	Do(*http.Client, *http.Request) (*http.Response, error)
}

Requestor is anything that can make an HTTP request with a client

var DefaultRequestor Requestor = defaultRequestor{}

DefaultRequestor is the default HTTP requestor

type RetryConfig

type RetryConfig struct {
	Backoffs    []time.Duration
	Jitter      float64
	ShouldRetry func(*http.Request, *http.Response, time.Duration) bool
}

RetryConfig configures if and how retrying of requests happens

func NewExponentialRetries

func NewExponentialRetries(initialBackoff time.Duration, count int, jitter float64) *RetryConfig

NewExponentialRetries creates a new retry config with the given delays

func NewFixedRetries

func NewFixedRetries(backoffs ...time.Duration) *RetryConfig

NewFixedRetries creates a new retry config with the given backoffs

func (*RetryConfig) Backoff

func (r *RetryConfig) Backoff(n int) time.Duration

Backoff gets the backoff time for the nth retry

func (*RetryConfig) MaxRetries

func (r *RetryConfig) MaxRetries() int

MaxRetries gets the maximum number of retries allowed

type Trace

type Trace struct {
	Request       *http.Request
	RequestTrace  []byte
	Response      *http.Response
	ResponseTrace []byte
	ResponseBody  []byte // response body stored separately
	StartTime     time.Time
	EndTime       time.Time
	Retries       int
}

Trace holds the complete trace of an HTTP request/response

func DoTrace

func DoTrace(client *http.Client, request *http.Request, retries *RetryConfig, access *AccessConfig, maxBodyBytes int) (*Trace, error)

DoTrace makes the given request saving traces of the complete request and response

func (*Trace) SanitizedResponse

func (t *Trace) SanitizedResponse(placeholder string) []byte

SanitizedResponse returns a valid UTF-8 string version of trace, substituting the body with a placeholder if it isn't valid UTF-8. It also strips any NULL characters as not all external dependencies can handle those.

func (*Trace) String

func (t *Trace) String() string

Jump to

Keyboard shortcuts

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