httpx

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &http.Client{
	Transport: DefaultTransport,
}

DefaultClient is similar to the default http.Client used by the package.

It is used for requests pooling.

View Source
var DefaultInsecureClient = &http.Client{
	Transport: DefaultInsecureTransport,
}

DefaultInsecureClient is the default http.Client used by the package, with TLS insecure skip verify.

It is used for requests pooling.

View Source
var DefaultInsecureTransport http.RoundTripper = Transport(TransportOptions().WithoutInsecureVerify())

DefaultInsecureTransport is the default http.DefaultTransport used by the package, with TLS insecure skip verify.

View Source
var DefaultResolver = &dnscache.Resolver{

	Timeout:  5 * time.Second,
	Resolver: net.DefaultResolver,
}

DefaultResolver is the default DNS resolver used by the package, which caches DNS lookups in memory.

View Source
var DefaultTransport http.RoundTripper = Transport()

DefaultTransport is similar to the default http.DefaultTransport used by the package.

Functions

func BodyBytes

func BodyBytes(resp *http.Response) []byte

BodyBytes returns the body of the http response as a byte slice.

func BodyString

func BodyString(resp *http.Response) string

BodyString returns the body of the http response as a string.

func Client

func Client(opts ...*ClientOption) *http.Client

Client returns a new http.Client with the given options, the result http.Client is used for fast-consuming requests.

If you want a requests pool management, use DefaultClient instead.

func Close

func Close(resp *http.Response)

Close closes the http response body without error.

func DNSCacheDialContext

func DNSCacheDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error)

func Do

func Do(cli *http.Client, req *http.Request, respFunc func(*http.Response) error) error

Do is a helper function to execute the given http request with the given http client, and execute the given function with the http response.

It is useful to avoid forgetting to close the http response body.

Do will return the error if failed to execute the http request or the given function.

func Error

func Error(rw http.ResponseWriter, code int)

Error is similar to http.Error, but it can get the error message by the given code.

func NewConnectRequest

func NewConnectRequest(uri string) (*http.Request, error)

NewConnectRequest returns a new http.MethodConnect request, which is saving your life from http.NewRequest.

func NewConnectRequestWithContext

func NewConnectRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewConnectRequestWithContext returns a new http.MethodConnect request with the given context, which is saving your life from http.NewRequestWithContext.

func NewDeleteRequest

func NewDeleteRequest(uri string) (*http.Request, error)

NewDeleteRequest returns a new http.MethodDelete request, which is saving your life from http.NewRequest.

func NewDeleteRequestWithContext

func NewDeleteRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewDeleteRequestWithContext returns a new http.MethodDelete request with the given context, which is saving your life from http.NewRequestWithContext.

func NewGetRequest

func NewGetRequest(uri string) (*http.Request, error)

NewGetRequest returns a new http.MethodGet request, which is saving your life from http.NewRequest.

func NewGetRequestWithContext

func NewGetRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewGetRequestWithContext returns a new http.MethodGet request, which is saving your life from http.NewRequestWithContext.

func NewHeadRequest

func NewHeadRequest(uri string) (*http.Request, error)

NewHeadRequest returns a new http.MethodHead request, which is saving your life from http.NewRequest.

func NewHeadRequestWithContext

func NewHeadRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewHeadRequestWithContext returns a new http.MethodHead request, which is saving your life from http.NewRequestWithContext.

func NewOptionsRequest

func NewOptionsRequest(uri string) (*http.Request, error)

NewOptionsRequest returns a new http.MethodOptions request, which is saving your life from http.NewRequest.

func NewOptionsRequestWithContext

func NewOptionsRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewOptionsRequestWithContext returns a new http.MethodOptions request with the given context, which is saving your life from http.NewRequestWithContext.

func NewPatchRequest

func NewPatchRequest(uri string, body io.Reader) (*http.Request, error)

NewPatchRequest returns a new http.MethodPatch request, which is saving your life from http.NewRequest.

func NewPatchRequestWithContext

func NewPatchRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)

NewPatchRequestWithContext returns a new http.MethodPatch request with the given context, which is saving your life from http.NewRequestWithContext.

func NewPostRequest

func NewPostRequest(uri string, body io.Reader) (*http.Request, error)

NewPostRequest returns a new http.MethodPost request, which is saving your life from http.NewRequest.

func NewPostRequestWithContext

func NewPostRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)

NewPostRequestWithContext returns a new http.MethodPost request with the given context, which is saving your life from http.NewRequestWithContext.

func NewPutRequest

func NewPutRequest(uri string, body io.Reader) (*http.Request, error)

NewPutRequest returns a new http.MethodPut request, which is saving your life from http.NewRequest.

func NewPutRequestWithContext

func NewPutRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)

NewPutRequestWithContext returns a new http.MethodPut request with the given context, which is saving your life from http.NewRequestWithContext.

func NewTraceRequest

func NewTraceRequest(uri string) (*http.Request, error)

NewTraceRequest returns a new http.MethodTrace request, which is saving your life from http.NewRequest.

func NewTraceRequestWithContext

func NewTraceRequestWithContext(ctx context.Context, uri string) (*http.Request, error)

NewTraceRequestWithContext returns a new http.MethodTrace request with the given context, which is saving your life from http.NewRequestWithContext.

func ProxyFromEnvironment

func ProxyFromEnvironment(r *http.Request) (*url.URL, error)

ProxyFromEnvironment is similar to http.ProxyFromEnvironment, but it also respects the NO_PROXY environment variable.

func Transport

func Transport(opts ...*TransportOption) *http.Transport

Transport returns a new http.Transport with the given options, the result http.Transport is used for constructing http.Client.

Types

type ClientOption

type ClientOption struct {
	*TransportOption
	// contains filtered or unexported fields
}

func ClientOptions

func ClientOptions() *ClientOption

func (*ClientOption) If

func (o *ClientOption) If(condition bool, then func(*ClientOption) *ClientOption) *ClientOption

If is a conditional option, which receives a boolean condition to trigger the given function or not.

func (*ClientOption) WithBasicAuth

func (o *ClientOption) WithBasicAuth(username, password string) *ClientOption

WithBasicAuth sets the basic authentication.

func (*ClientOption) WithBearerAuth

func (o *ClientOption) WithBearerAuth(token string) *ClientOption

WithBearerAuth sets the bearer token.

func (*ClientOption) WithDebug

func (o *ClientOption) WithDebug() *ClientOption

WithDebug sets the debug mode.

func (*ClientOption) WithHeader

func (o *ClientOption) WithHeader(key, value string) *ClientOption

WithHeader sets the header.

func (*ClientOption) WithHeaders

func (o *ClientOption) WithHeaders(headers map[string]string) *ClientOption

WithHeaders sets the headers.

func (*ClientOption) WithRetryBackoff

func (o *ClientOption) WithRetryBackoff(waitMin, waitMax time.Duration, attemptMax int) *ClientOption

WithRetryBackoff specifies the retry-backoff mechanism for request.

func (*ClientOption) WithRetryIf

func (o *ClientOption) WithRetryIf(retryIf func(resp *http.Response, err error) (retry bool)) *ClientOption

WithRetryIf specifies the if-condition of retry operation for request, or stops retrying if setting with `nil`.

func (*ClientOption) WithRoundTripper

func (o *ClientOption) WithRoundTripper(rt func(req *http.Request) error) *ClientOption

WithRoundTripper sets the round tripper.

func (*ClientOption) WithTimeout

func (o *ClientOption) WithTimeout(timeout time.Duration) *ClientOption

WithTimeout sets the request timeout.

This timeout controls the sum of [network dial], [tls handshake], [request], [response header reading] and [response body reading].

Use 0 to disable timeout.

func (*ClientOption) WithTransport

func (o *ClientOption) WithTransport(opt *TransportOption) *ClientOption

WithTransport sets the TransportOption.

func (*ClientOption) WithUserAgent

func (o *ClientOption) WithUserAgent(ua string) *ClientOption

WithUserAgent sets the user agent.

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter is copied from httpretty.JSONFormatter, but use our own json package.

func (*JSONFormatter) Format

func (j *JSONFormatter) Format(w io.Writer, src []byte) error

Format JSON content.

func (*JSONFormatter) Match

func (j *JSONFormatter) Match(mediatype string) bool

Match JSON media type.

type RoundTripperChain

type RoundTripperChain struct {
	Do   func(req *http.Request) error
	Next http.RoundTripper
}

func (RoundTripperChain) RoundTrip

func (c RoundTripperChain) RoundTrip(req *http.Request) (*http.Response, error)

type RoundTripperFunc

type RoundTripperFunc func(*http.Request) (*http.Response, error)

func (RoundTripperFunc) RoundTrip

func (fn RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

type SeekerFile

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

func OpenSeekerFile

func OpenSeekerFile(cli *http.Client, req *http.Request, opts ...*SeekerFileOption) (*SeekerFile, error)

OpenSeekerFile tries the GET http.Request as a SeekerFile, and returns a SeekerFile, or an error if any.

func (*SeekerFile) Close

func (f *SeekerFile) Close() error

func (*SeekerFile) Len

func (f *SeekerFile) Len() int64

func (*SeekerFile) Read

func (f *SeekerFile) Read(p []byte) (int, error)

func (*SeekerFile) ReadAt

func (f *SeekerFile) ReadAt(p []byte, off int64) (int, error)

type SeekerFileOption

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

func SeekerFileOptions

func SeekerFileOptions() *SeekerFileOption

func (*SeekerFileOption) If

If is a conditional option, which receives a boolean condition to trigger the given function or not.

func (*SeekerFileOption) WithBufferSize

func (o *SeekerFileOption) WithBufferSize(bufSize int) *SeekerFileOption

WithBufferSize sets the size of the buffer to read the file,

Default is 4mb.

func (*SeekerFileOption) WithSize

func (o *SeekerFileOption) WithSize(size int) *SeekerFileOption

WithSize sets the size of the file to read,

If the size is greater than the content size of the file, it will return an error.

func (*SeekerFileOption) WithoutRangeDownloadDetect

func (o *SeekerFileOption) WithoutRangeDownloadDetect() *SeekerFileOption

WithoutRangeDownloadDetect disables range download detection.

Usually, OpenSeekerFile sends a "HEAD" HTTP request to destination to get the content size from the "Content-Length" header, and confirms whether supports range download via the "Accept-Ranges" header. However, some servers may not support the "HEAD" method, or the "Accept-Ranges" header is not set correctly.

With this option, OpenSeekerFile sends "GET" HTTP request to get the content size as usual, and does not confirm whether supports range download. But during the seeking read, it still uses the "Range" header to read the file.

type TransportOption

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

func TransportOptions

func TransportOptions() *TransportOption

func (*TransportOption) Customize

func (o *TransportOption) Customize(fn func(*http.Transport)) *TransportOption

Customize sets the transport.

func (*TransportOption) If

func (o *TransportOption) If(condition bool, then func(*TransportOption) *TransportOption) *TransportOption

If is a conditional option, which receives a boolean condition to trigger the given function or not.

func (*TransportOption) TimeoutForDial

func (o *TransportOption) TimeoutForDial(timeout time.Duration) *TransportOption

TimeoutForDial sets the timeout for network dial.

This timeout controls the [network dial] only.

Use 0 to disable timeout.

func (*TransportOption) TimeoutForIdleConn

func (o *TransportOption) TimeoutForIdleConn(timeout time.Duration) *TransportOption

TimeoutForIdleConn sets the timeout for idle connection.

This timeout controls the [idle connection lifetime] only.

Use 0 to disable timeout.

func (*TransportOption) TimeoutForResponseHeader

func (o *TransportOption) TimeoutForResponseHeader(timeout time.Duration) *TransportOption

TimeoutForResponseHeader sets the timeout for response header.

This timeout controls the [response header reading] only.

Use 0 to disable timeout.

func (*TransportOption) TimeoutForTLSHandshake

func (o *TransportOption) TimeoutForTLSHandshake(timeout time.Duration) *TransportOption

TimeoutForTLSHandshake sets the timeout for tls handshake.

This timeout controls the [tls handshake] only.

Use 0 to disable timeout.

func (*TransportOption) WithDialer

func (o *TransportOption) WithDialer(dialer *net.Dialer) *TransportOption

WithDialer sets the dialer.

func (*TransportOption) WithInsecureVerify

func (o *TransportOption) WithInsecureVerify() *TransportOption

WithInsecureVerify verifies the insecure connection.

func (*TransportOption) WithKeepalive

func (o *TransportOption) WithKeepalive(timeoutAndKeepalive ...time.Duration) *TransportOption

WithKeepalive sets the keepalive.

func (*TransportOption) WithProxy

func (o *TransportOption) WithProxy(proxy func(*http.Request) (*url.URL, error)) *TransportOption

WithProxy sets the proxy.

func (*TransportOption) WithTLSClientConfig

func (o *TransportOption) WithTLSClientConfig(config *tls.Config) *TransportOption

WithTLSClientConfig sets the tls.Config.

func (*TransportOption) WithoutDNSCache

func (o *TransportOption) WithoutDNSCache() *TransportOption

WithoutDNSCache disables the dns cache.

func (*TransportOption) WithoutInsecureVerify

func (o *TransportOption) WithoutInsecureVerify() *TransportOption

WithoutInsecureVerify skips the insecure connection verify.

func (*TransportOption) WithoutKeepalive

func (o *TransportOption) WithoutKeepalive() *TransportOption

WithoutKeepalive disables the keepalive.

func (*TransportOption) WithoutProxy

func (o *TransportOption) WithoutProxy() *TransportOption

WithoutProxy disables the proxy.

Jump to

Keyboard shortcuts

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