Documentation ¶
Index ¶
- Variables
- func BodyBytes(resp *http.Response) []byte
- func BodyString(resp *http.Response) string
- func Client(opts ...*ClientOption) *http.Client
- func Close(resp *http.Response)
- func DNSCacheDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error)
- func DefaultRetry(resp *http.Response, respErr error) bool
- func Do(cli *http.Client, req *http.Request, respFunc func(*http.Response) error) error
- func Error(rw http.ResponseWriter, code int)
- func NewConnectRequest(uri string) (*http.Request, error)
- func NewConnectRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func NewDeleteRequest(uri string) (*http.Request, error)
- func NewDeleteRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func NewGetRequest(uri string) (*http.Request, error)
- func NewGetRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func NewHeadRequest(uri string) (*http.Request, error)
- func NewHeadRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func NewOptionsRequest(uri string) (*http.Request, error)
- func NewOptionsRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func NewPatchRequest(uri string, body io.Reader) (*http.Request, error)
- func NewPatchRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)
- func NewPostRequest(uri string, body io.Reader) (*http.Request, error)
- func NewPostRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)
- func NewPutRequest(uri string, body io.Reader) (*http.Request, error)
- func NewPutRequestWithContext(ctx context.Context, uri string, body io.Reader) (*http.Request, error)
- func NewTraceRequest(uri string) (*http.Request, error)
- func NewTraceRequestWithContext(ctx context.Context, uri string) (*http.Request, error)
- func ProxyFromEnvironment(r *http.Request) (*url.URL, error)
- func Transport(opts ...*TransportOption) *http.Transport
- type ClientOption
- func (o *ClientOption) If(condition bool, then func(*ClientOption) *ClientOption) *ClientOption
- func (o *ClientOption) WithBasicAuth(username, password string) *ClientOption
- func (o *ClientOption) WithBearerAuth(token string) *ClientOption
- func (o *ClientOption) WithDebug() *ClientOption
- func (o *ClientOption) WithHeader(key, value string) *ClientOption
- func (o *ClientOption) WithHeaders(headers map[string]string) *ClientOption
- func (o *ClientOption) WithRetryBackoff(waitMin, waitMax time.Duration, attemptMax int) *ClientOption
- func (o *ClientOption) WithRetryIf(retryIf RetryFunc) *ClientOption
- func (o *ClientOption) WithRoundTripper(rt func(req *http.Request) error) *ClientOption
- func (o *ClientOption) WithTimeout(timeout time.Duration) *ClientOption
- func (o *ClientOption) WithTransport(opt *TransportOption) *ClientOption
- func (o *ClientOption) WithUserAgent(ua string) *ClientOption
- type JSONFormatter
- type RetryFunc
- type RoundTripperChain
- type RoundTripperFunc
- type SeekerFile
- type SeekerFileOption
- func (o *SeekerFileOption) If(condition bool, then func(*SeekerFileOption) *SeekerFileOption) *SeekerFileOption
- func (o *SeekerFileOption) WithBufferSize(bufSize int) *SeekerFileOption
- func (o *SeekerFileOption) WithSize(size int) *SeekerFileOption
- func (o *SeekerFileOption) WithoutRangeDownloadDetect() *SeekerFileOption
- type TransportOption
- func (o *TransportOption) Customize(fn func(*http.Transport)) *TransportOption
- func (o *TransportOption) If(condition bool, then func(*TransportOption) *TransportOption) *TransportOption
- func (o *TransportOption) TimeoutForDial(timeout time.Duration) *TransportOption
- func (o *TransportOption) TimeoutForIdleConn(timeout time.Duration) *TransportOption
- func (o *TransportOption) TimeoutForResponseHeader(timeout time.Duration) *TransportOption
- func (o *TransportOption) TimeoutForTLSHandshake(timeout time.Duration) *TransportOption
- func (o *TransportOption) WithDialer(dialer *net.Dialer) *TransportOption
- func (o *TransportOption) WithInsecureVerify() *TransportOption
- func (o *TransportOption) WithKeepalive(timeoutAndKeepalive ...time.Duration) *TransportOption
- func (o *TransportOption) WithProxy(proxy func(*http.Request) (*url.URL, error)) *TransportOption
- func (o *TransportOption) WithTLSClientConfig(config *tls.Config) *TransportOption
- func (o *TransportOption) WithoutDNSCache() *TransportOption
- func (o *TransportOption) WithoutInsecureVerify() *TransportOption
- func (o *TransportOption) WithoutKeepalive() *TransportOption
- func (o *TransportOption) WithoutProxy() *TransportOption
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = &http.Client{ Transport: DefaultTransport, }
DefaultClient is similar to the default http.Client used by the package.
It is used for requests pooling.
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.
var DefaultInsecureTransport http.RoundTripper = Transport(TransportOptions().WithoutInsecureVerify())
DefaultInsecureTransport is the default http.DefaultTransport used by the package, with TLS insecure skip verify.
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.
var DefaultTransport http.RoundTripper = Transport()
DefaultTransport is similar to the default http.DefaultTransport used by the package.
Functions ¶
func BodyString ¶
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 DNSCacheDialContext ¶
func DefaultRetry ¶ added in v0.6.1
DefaultRetry is the default retry condition, inspired by https://github.com/hashicorp/go-retryablehttp/blob/40b0cad1633fd521cee5884724fcf03d039aaf3f/client.go#L68-L86.
func Do ¶
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 ¶
NewConnectRequest returns a new http.MethodConnect request, which is saving your life from http.NewRequest.
func NewConnectRequestWithContext ¶
NewConnectRequestWithContext returns a new http.MethodConnect request with the given context, which is saving your life from http.NewRequestWithContext.
func NewDeleteRequest ¶
NewDeleteRequest returns a new http.MethodDelete request, which is saving your life from http.NewRequest.
func NewDeleteRequestWithContext ¶
NewDeleteRequestWithContext returns a new http.MethodDelete request with the given context, which is saving your life from http.NewRequestWithContext.
func NewGetRequest ¶
NewGetRequest returns a new http.MethodGet request, which is saving your life from http.NewRequest.
func NewGetRequestWithContext ¶
NewGetRequestWithContext returns a new http.MethodGet request, which is saving your life from http.NewRequestWithContext.
func NewHeadRequest ¶
NewHeadRequest returns a new http.MethodHead request, which is saving your life from http.NewRequest.
func NewHeadRequestWithContext ¶
NewHeadRequestWithContext returns a new http.MethodHead request, which is saving your life from http.NewRequestWithContext.
func NewOptionsRequest ¶
NewOptionsRequest returns a new http.MethodOptions request, which is saving your life from http.NewRequest.
func NewOptionsRequestWithContext ¶
NewOptionsRequestWithContext returns a new http.MethodOptions request with the given context, which is saving your life from http.NewRequestWithContext.
func NewPatchRequest ¶
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 ¶
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 ¶
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 ¶
NewTraceRequest returns a new http.MethodTrace request, which is saving your life from http.NewRequest.
func NewTraceRequestWithContext ¶
NewTraceRequestWithContext returns a new http.MethodTrace request with the given context, which is saving your life from http.NewRequestWithContext.
func ProxyFromEnvironment ¶
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 RetryFunc) *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 }
type RoundTripperFunc ¶
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
type SeekerFileOption ¶
type SeekerFileOption struct {
// contains filtered or unexported fields
}
func SeekerFileOptions ¶
func SeekerFileOptions() *SeekerFileOption
func (*SeekerFileOption) If ¶
func (o *SeekerFileOption) If(condition bool, then func(*SeekerFileOption) *SeekerFileOption) *SeekerFileOption
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.